aboutsummaryrefslogtreecommitdiff
path: root/parse.c
blob: a34063ca35738a52d1dfacaa7f8bad6236624a96 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "parse.h"
#include "tokens.h"
#include "atom.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct atom *parse_token(struct token *token)
{
    switch (token->type)
    {
    case TOKEN_INT:
        return atom_new_int(strtol(token->s, NULL, 10));

    case TOKEN_STR:
        return atom_new_str(token->s, token->len);

    case TOKEN_SYMBOL:
        if (strncmp(token->s, "#t", 2) == 0)
            return &true_atom;
        else if (strncmp(token->s, "#f", 2) == 0)
            return &false_atom;
        else
            return atom_new_sym(token->s, token->len);

    default:
        return NULL;
    }

    return NULL;
}

void parse_quote(const char *src, int *pos, struct atom **result)
{
    struct atom *value = parse(src, pos);
    struct atom *q = atom_new_sym("quote", 5);
    struct atom *form = atom_new_list_empty();

    LIST_INSERT_HEAD(form->list, q, entries);
    LIST_INSERT_AFTER(q, value, entries);

    *result = form;
}

int parse_list(const char *src, int *pos, struct atom **result)
{
    struct token token;
    int rc;
    struct list *list;
    struct atom *last = NULL;

    list = calloc(1, sizeof(*list));
    LIST_INIT(list);

    while ((rc = get_next_token(src, pos, &token)))
    {
        struct atom *atom;

        if (rc < 0)
            break;

        switch (token.type)
        {
        case TOKEN_LPAREN:
            if (parse_list(src, pos, &atom) < 0)
                return -1;
            break;

        case TOKEN_RPAREN:
            goto out;
            break;

        case TOKEN_QUOTE:
            parse_quote(src, pos, &atom);
            break;

        default:
            atom = parse_token(&token);
            break;
        }

        if (!last)
            LIST_INSERT_HEAD(list, atom, entries);
        else
            LIST_INSERT_AFTER(last, atom, entries);

        last = atom;
    }

out:

    if (rc < 0)
        return rc;

    if (LIST_EMPTY(list))
    {
        free(list);
        *result = &nil_atom;
        return 1;
    }

    *result = atom_new_list(list);
    return 1;
}

struct atom *parse(const char *src, int *pos)
{
    struct token token;
    struct atom *atom = NULL;
    int rc;

    rc = get_next_token(src, pos, &token);

    if (rc < 0)
        return NULL;

    switch (token.type)
    {
    case TOKEN_LPAREN:
        if (parse_list(src, pos, &atom) < 0)
            atom = NULL;
        break;

    case TOKEN_RPAREN:
        printf("syntax error: unexpected ')'\n");
        break;

    case TOKEN_QUOTE:
        parse_quote(src, pos, &atom);
        break;

    default:
        atom = parse_token(&token);
        break;
    }

    return atom;
}

#ifdef BUILD_TEST

#include "test_util.h"

static const char *test_src_fact =
    "(define fact\n"
    "    ;; Factorial function\n"
    "    (lambda (n)\n"
    "        (if (eq n 0)\n"
    "            1 ; Factorial of 0 is 1\n"
    "            (* n (fact (- n 1))))))\n"
    "(fact 5) ;; this should evaluate to 120\n";

#define ASSERT_SYM(ATOM, SYM) \
    ASSERT_TRUE(IS_SYM(ATOM)); \
    ASSERT_STREQ(SYM, (ATOM)->str.str)

TEST(test_parse)
{
    int pos = 0;
    struct atom *list;

    list = parse(test_src_fact, &pos);

    ASSERT_TRUE(list != NULL);
    ASSERT_TRUE(IS_LIST(list));

    struct atom *a = CAR(list->list);
    ASSERT_TRUE(a != NULL);
    ASSERT_SYM(a, "define");

    a = CDR(a);

    ASSERT_TRUE(a != NULL);
    ASSERT_SYM(a, "fact");

    a = CDR(a);

    ASSERT_TRUE(a != NULL);
}

TEST(parse_quote)
{
    int pos = 0;

    struct atom *result = parse("'foobar", &pos);
    ASSERT_TRUE(result != NULL);
    ASSERT_EQ(ATOM_LIST, result->type);

    struct atom *op = CAR(result->list);
    ASSERT_TRUE(op != NULL);
    ASSERT_EQ(ATOM_SYMBOL, op->type);
    ASSERT_STREQ("quote", op->str.str);

    struct atom *a = CDR(op);
    ASSERT_TRUE(a != NULL);
    ASSERT_EQ(ATOM_SYMBOL, a->type);
    ASSERT_STREQ("foobar", a->str.str);
}

#endif