summaryrefslogtreecommitdiff
path: root/parser.c
blob: f9c7047b320dd491d9923c44b21769088ecd5fd2 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "parser.h"
#include "lexer.h"
#include "scope.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define PARSER_PANIC(format, ...) {\
    fprintf(\
        stderr,\
        "ccc: parse error: %s: line %lu, column %lu: " format "\n",\
        tok.PATH,\
        tok.LINE,\
        tok.COL __VA_OPT__(,)\
        __VA_ARGS__);\
    exit(1);\
}

static struct token tok;
static struct scope* scope;

static void* protected_alloc(size_t sz) {
    void* ptr = calloc(1, sz);
    if (ptr == NULL) {
        fprintf(stderr, "ccc: out of memory\n");
        exit(1);
    }
    return ptr;
}

static void unexpected_token(enum token_type expected) {
    /* TODO: print what token was expected */
    PARSER_PANIC("unexpected token");
}

/* TODO: reorganize the lexer to make peek cheaper */
static void peek_or_panic() {
    if (!lexer_peek(&tok))
        PARSER_PANIC("unexpected EOF");
}

static void expect(enum token_type expected) {
    if (!lexer_pop(&tok))
        PARSER_PANIC("unexpected EOF");

    if (tok.type != expected) unexpected_token(expected);
}

static void expect_kw(const char* kw) {
    if (!lexer_pop(&tok))
        PARSER_PANIC("unexpected EOF, expected %s", kw);

    if (tok.type != TK_IDENT)
        PARSER_PANIC("unexpected token, expected %s", kw);

    if (strcmp(kw, tok.data.ident) != 0)
        PARSER_PANIC(
            "unexpected identifier %s, expected %s", tok.data.ident, kw);

    /* string won't go in the AST, discard it */
    free(tok.data.ident);
    tok.data.ident = NULL;
}

static void parse_type(struct type_node* p_node) {
    /* TODO: modifiers, void rules, arrays, etc. */
    /* TODO: struct, union, enum */
    expect(TK_IDENT);
    struct type_def type_def;
    if (!scope_get_type(scope, &type_def, tok.data.ident))
        PARSER_PANIC("unknown type name: %s", tok.data.ident);

    p_node->def = type_def;

    peek_or_panic();
    p_node->ptr_level = 0;
    while (tok.type == TK_STAR) {
        expect(TK_STAR);
        p_node->ptr_level++;
        peek_or_panic();
    }
}

static void parse_expr(struct expr_node* p_node);

static void parse_literal(struct expr_node* p_node) {
    peek_or_panic();

    switch (tok.type) {
        case TK_INT_LIT:
            expect(TK_INT_LIT);
            p_node->type = EXPR_INT_LIT;
            p_node->as._int_lit.val = tok.data.int_lit;
            break;
        case TK_CHAR_LIT:
            expect(TK_CHAR_LIT);
            p_node->type = EXPR_CHAR_LIT;
            p_node->as._char_lit.val = tok.data.char_lit;
            break;
        default:
            PARSER_PANIC("invalid literal type");
    }
}

static void parse_var_ref(struct var_ref_node* p_node) {
    expect(TK_IDENT);
    p_node->ident = tok.data.ident;
}

static void expr_to_lval(struct lval_node* l_node, struct expr_node* e_node) {
    switch (e_node->type) {
        case EXPR_VAR_REF:
            *l_node = (struct lval_node) {
                .type = LVAL_VAR_REF,
                .as._var_ref = e_node->as._var_ref,
            };
            return;
        default:
            PARSER_PANIC("expression is not assignable");
    }
}

static void parse_expr_assign(struct expr_node* p_node) {
    expr_to_lval(&p_node->as._assign.lval, p_node);
    p_node->type = EXPR_ASSIGN;
    p_node->as._assign.rval = protected_alloc(sizeof(struct expr_node));

    expect(TK_ASSIGN);
    parse_expr(p_node->as._assign.rval);
}

static void parse_arg_evals(struct expr_node** pp_arg) {
    for (;;) {
        *pp_arg = protected_alloc(sizeof(struct expr_node));
        parse_expr(*pp_arg);
        pp_arg = &((*pp_arg)->next);

        peek_or_panic();
        if (tok.type == TK_RPAREN) break;
        expect(TK_COMMA);
    }
}

static void parse_expr_call(struct expr_node* p_node) {
    switch (p_node->type) {
        case EXPR_VAR_REF:
            struct var_def var_def;
            if (!scope_get_var(scope, &var_def, p_node->as._var_ref.ident))
                PARSER_PANIC(
                    "%s is not a known function", p_node->as._var_ref.ident);

            if (var_def.loc.type != STO_FN)
                PARSER_PANIC("called object is not a function");

            p_node->as._call.called_fn = var_def.loc.decl;
            break;
        default:
            PARSER_PANIC("expression is not callable");
    }

    p_node->type = EXPR_CALL;
    p_node->as._call.args_head = NULL;

    expect(TK_LPAREN);
    peek_or_panic();
    if (tok.type != TK_RPAREN) parse_arg_evals(&p_node->as._call.args_head);
    expect(TK_RPAREN);
}

static void parse_expr(struct expr_node* p_node) {
    peek_or_panic();
    switch (tok.type) {
        case TK_LPAREN:
            expect(TK_LPAREN);
            parse_expr(p_node);
            expect(TK_RPAREN);
            break;
        case TK_INT_LIT:
        case TK_CHAR_LIT:
        case TK_FLOAT_LIT:
        case TK_STR_LIT:
            parse_literal(p_node);
            break;
        case TK_IDENT:
            p_node->type = EXPR_VAR_REF;
            parse_var_ref(&p_node->as._var_ref);
            break;
        default:
            PARSER_PANIC("expected expression");
    }

    peek_or_panic();
    if (tok.type == TK_ASSIGN)
        parse_expr_assign(p_node);
    else if (tok.type == TK_LPAREN)
        parse_expr_call(p_node);
}

static void parse_var_decl(struct var_decl_node* p_node) {
    parse_type(&p_node->type);
    expect(TK_IDENT);
    p_node->ident = tok.data.ident;
}

static void parse_stmt(struct stmt_node* p_node);

static void parse_return(struct return_node* p_node) {
    expect_kw("return");

    peek_or_panic();
    if (tok.type == TK_SEMI) {
        p_node->ret_val = NULL;
        return;
    }

    p_node->ret_val = protected_alloc(sizeof(struct expr_node));
    parse_expr(p_node->ret_val);
}

static void parse_group(struct group_node* p_node) {
    expect(TK_LCURLY);

    struct stmt_node** pp_node = &p_node->body_head;
    for (;;) {
        peek_or_panic();
        if (tok.type == TK_RCURLY) break;

        *pp_node = protected_alloc(sizeof(struct stmt_node));
        parse_stmt(*pp_node);
        pp_node = &((*pp_node)->next);
    }

    expect(TK_RCURLY);
}

static void parse_stmt_assign(struct stmt_node* p_node) {
    peek_or_panic();
    if (tok.type != TK_ASSIGN) return;

    switch (p_node->type) {
        case STMT_VAR_DECL:
            p_node->as._expr.as._assign.lval = (struct lval_node) {
                .type = LVAL_VAR_DECL,
                .as._var_decl = p_node->as._var_decl,
            };
            break;
        default:
            return;
    }

    p_node->type = STMT_EXPR;
    p_node->as._expr.type = EXPR_ASSIGN;
    p_node->as._expr.as._assign.rval =
        protected_alloc(sizeof(struct expr_node));

    expect(TK_ASSIGN);
    parse_expr(p_node->as._expr.as._assign.rval);
}

static void parse_stmt(struct stmt_node* p_node) {
    peek_or_panic();
    switch (tok.type) {
        case TK_SEMI:
            p_node->type = STMT_EMPTY;
            break;
        case TK_LCURLY:
            p_node->type = STMT_GROUP;
            parse_group(&p_node->as._group);
            return;
        case TK_IDENT:
            if (strcmp(tok.data.ident, "return") == 0) {
                p_node->type = STMT_RETURN;
                parse_return(&p_node->as._return);
                break;
            } else if (scope_get_type(scope, NULL, tok.data.ident)) {
                p_node->type = STMT_VAR_DECL;
                parse_var_decl(&p_node->as._var_decl);
                break;
            }
        default:
            p_node->type = STMT_EXPR;
            parse_expr(&p_node->as._expr);
    }

    parse_stmt_assign(p_node);
    expect(TK_SEMI);
}

static void parse_arg_decls(struct var_decl_node** pp_arg) {
    for (;;) {
        *pp_arg = protected_alloc(sizeof(struct var_decl_node));
        parse_var_decl(*pp_arg);
        pp_arg = &((*pp_arg)->next);

        peek_or_panic();
        if (tok.type == TK_RPAREN) break;
        expect(TK_COMMA);
    }
}

static void parse_fn_decl(struct fn_decl_node* p_node) {
    parse_type(&p_node->return_type);

    expect(TK_IDENT);
    p_node->name = tok.data.ident;

    expect(TK_LPAREN);

    peek_or_panic();
    if (tok.type != TK_RPAREN) parse_arg_decls(&p_node->args_head);

    expect(TK_RPAREN);

    parse_group(&p_node->body);

    scope_define_var(scope, (struct var_def) {
        .name = p_node->name,
        .loc = {
            .type = STO_FN,
            .decl = p_node,
        },
    });
}

static bool parse_root(struct root_node* p_node) {
    if (!lexer_peek(&tok)) return false;

    p_node->type = ROOT_FN_DECL;
    parse_fn_decl(&p_node->as._fn_decl);
    return true;
}

struct root_node* parse(const char* path) {
    lexer_load(path);
    scope_push(&scope);
    scope_install_default_types(scope);

    struct root_node* root;
    struct root_node** p_node = &root;

    for (;;) {
        *p_node = protected_alloc(sizeof(struct root_node));
        if (!parse_root(*p_node)) {
            free(*p_node);
            *p_node = NULL;
            break;
        }
        p_node = &((*p_node)->next);
    }

    scope_pop(&scope);
    lexer_close();
    return root;
}