aboutsummaryrefslogtreecommitdiff
path: root/src/as_parser.c
blob: a68fbba1c87cb687e5cb2ef0ff241c583caab597 (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
#include "as_parser.h"
#include "as_tokenizer.h"
#include "utils.h"

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

// BNF
// ===
//
// <prog> ::= <stmts>
// <stmts> ::= <stmt> newline | <stmt> newline <stmts>
// <stmt> ::= <label> <instr> | <instr> | <label>
// <instr> ::= <op> | <op> arg | <op> tag
// <label> ::= tag ":"
// <op> ::= "add" | "sub" | "mul" | "div" | "mod" | "eq" | ...

struct result parse_prog(struct allocator * alct, struct token_stream * ts) {
    struct prog * p = allocate(alct, sizeof(struct prog));
    p->stmts = unwrap(parse_stmts(alct, ts));
    return ok(p);
}

struct result parse_stmts(struct allocator * alct, struct token_stream * ts) {
    struct token *token;
    struct stmts * ss = allocate(alct, sizeof(struct stmts));
    struct stmt * s;

    ss->stmts = allocate(alct, sizeof(struct stmt *));
    ss->stmts[0] = NULL;
    int capacity = 0;
    int len = 0;

    while (1) {
        token = unwrap(peek_token(alct, ts));
        if (token->type == TK_ENDOFFILE) {
            break;
        }

        s = unwrap(parse_stmt(alct, ts));
        if (s == NULL) continue;
        if (len == capacity) {
            size_t new_capacity = capacity * 2 + 1;
            void* new_stmts = allocate(alct, sizeof(struct stmt **) * new_capacity);
            memcpy(new_stmts, ss->stmts, sizeof(struct stmt **) * capacity);
            ss->stmts = new_stmts;
            capacity = new_capacity;
        }
        // expect newline
        token = unwrap(peek_token(alct, ts));
        if (token->type == TK_NEWLINE) {
            unwrap(next_token(alct, ts));
        } else {
            return err(safe_sprintf(alct, "%d:%d expect newline.\n", token->line, token->col));
        }
        ss->stmts[len] = s;
        len++;
    }
    ss->stmts[len] = NULL;
    return ok(ss);
}

struct result parse_label(struct allocator * alct, struct token_stream * ts) {
    struct token * t;
    t = unwrap(next_token(alct, ts));
    if (t->type != TK_TAG) {
        return err(safe_sprintf(alct, "%d:%d expect label.\n", t->line, t->col));
    }
    struct label * l = allocate(alct, sizeof(struct label *));
    l->name = t->sval;
    t = unwrap(next_token(alct, ts));
    if (t->type != TK_COLON) {
        return err(safe_sprintf(alct, "%d:%d expect colon.\n", t->line, t->col));
    }
    return ok(l);
}

struct result parse_stmt(struct allocator * alct, struct token_stream * ts) {
    const char *errmsg;
    struct token * token;
    token = unwrap(peek_token(alct, ts));
    struct stmt * stmt = allocate(alct, sizeof(struct stmt));
    stmt->label = NULL;
    stmt->instr = NULL;
    if (token->type == TK_TAG) {
        stmt->label = unwrap(parse_label(alct, ts));
        token = unwrap(peek_token(alct, ts));
        if (token->type == TK_NEWLINE) {
            return ok(stmt);
        }
        token = unwrap(peek_token(alct, ts));
    }
    if (token->type == TK_OP) {
        stmt->instr = unwrap(parse_instr(alct, ts));
        token = unwrap(peek_token(alct, ts));
        if (token->type == TK_NEWLINE) {
            return ok(stmt);
        }
    }
    if (token->type == TK_NEWLINE) {
        return ok(NULL);
    }
    return err(safe_sprintf(alct, "%d:%d expect lable + instruction, lable, or instruction.\n", token->line, token->col));
}

struct result parse_op(struct allocator * alct, struct token_stream * ts) {
    struct token * t;
    t = unwrap(next_token(alct, ts));
    enum op op;
    if (t->type == TK_OP) {
        op = str2op(t->sval);
        if (op == OP_END) {
            return err(safe_sprintf(alct, "%d:%d invalid op.\n", t->line, t->col));
        }
    } else {
        return err(safe_sprintf(alct, "%d:%d expect op.\n", t->line, t->col));
    }
    return ok((void*)op);
}

struct result parse_instr(struct allocator * alct, struct token_stream * ts) {
    struct token * t;
    t = unwrap(peek_token(alct, ts));
    struct instr * i = allocate(alct, sizeof(struct instr));
    i->tag_name = NULL;
    i->arg = NULL;
    i->op = OP_END;
    if (t->type == TK_OP) {
        i->op = (enum op)unwrap(parse_op(alct, ts));
        t = unwrap(peek_token(alct, ts));
        if (t->type == TK_ARG) {
            struct arg * a = allocate(alct, sizeof(struct arg));
            a->ival = t->ival;
            a->fval = t->fval;
            i->arg = a;
            unwrap(next_token(alct, ts));
        } else if (t->type == TK_TAG) {
            i->tag_name = t->sval;
            unwrap(next_token(alct, ts));
        }
    }
    return ok(i);
}