aboutsummaryrefslogtreecommitdiff
path: root/parse.c
blob: b5101e133916927438386c3e9b43884839d89b8a (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
#include "parse.h"
#include "tokens.h"
#include "list.h"
#include "atom.h"

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

void print_list(struct list *list, int level)
{
    while (list)
    {
        struct atom *atom = LIST_GET_ATOM(list);

        if (IS_TRUE(list))
        {
            printf("#t");
        }
        else if (IS_FALSE(list))
        {
            printf("#f");
        }
        else if (IS_NIL(list))
        {
            printf("nil");
        }
        else
        {
            switch (atom->type)
            {
                case ATOM_SYMBOL:
                    printf("%.*s", atom->str.len, atom->str.str);
                    break;

                case ATOM_LIST:
                    printf("(");
                    print_list(atom->list, level+1);
                    printf(")");
                    break;

                case ATOM_STR:
                    printf("\"%.*s\"", atom->str.len, atom->str.str);
                    break;

                case ATOM_INT:
                    printf("%ld", atom->l);
            }
        }

        if (list->next)
            printf(" ");

        list = list->next;
    }

    if (level == 0)
        printf("\n");
}

int parse_next(const char *src, int *pos, struct atom **result)
{
    struct token token;
    int rc;

    if ((rc = get_next_token(src, pos, &token)) > 0)
    {
        switch (token.type)
        {
            case TOKEN_INT:
                *result = atom_new_int(strtol(token.s, NULL, 10));
                break;

            case TOKEN_STR:
                *result = atom_new_str(token.s, token.len);
                break;

            case TOKEN_SYMBOL:
                if (strncmp(token.s, "#t", 2) == 0)
                    *result = &true_atom;
                else if (strncmp(token.s, "#f", 2) == 0)
                    *result = &false_atom;
                else
                    *result = atom_new_sym(token.s, token.len);
                break;

            case TOKEN_LPAREN:
            {
                struct list *l = parse(src, pos);
                *result = atom_new_list(l);
                break;
            }

            case TOKEN_RPAREN:
                return -2;

            case TOKEN_QUOTE:
            {
                struct atom *quoted = NULL;

                parse_next(src, pos, &quoted);

                struct list *qlist = list_append(NULL,
                    atom_new_sym("quote", 5));

                list_append(qlist, quoted);

                *result = atom_new_list(qlist);

                break;
            }
        }
    }

    return rc;
}

struct list *parse(const char *src, int *pos)
{
    int rc;
    struct atom *atom;
    struct list root, *last = &root;

    list_init(last);

    while ((rc = parse_next(src, pos, &atom)))
    {
        if (rc < 0)
            break;

        last = list_append(last, atom);
    }

    if (rc < 0 && rc != -2)
        return NULL;

    return root.next;
}

#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(LIST, SYM) \
    ASSERT_TRUE(IS_SYM(LIST)); \
    ASSERT_STREQ(SYM, LIST_GET_ATOM(LIST)->str.str)

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

    list = parse(test_src_fact, &pos);

    ASSERT_TRUE(list != NULL);

    ASSERT_TRUE(IS_LIST(list));

    list = LIST_GET_ATOM(list)->list;

    ASSERT_TRUE(list != NULL);
    ASSERT_SYM(list, "define");

    list = list->next;

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

    list = list->next;

    ASSERT_TRUE(list != NULL);
}

#endif