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
|
#include "as_parser.h"
#include "as_tokenizer.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.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"
Prog parseProg(Allocator alct, TokenStream ts) {
Prog p = allocate(alct, sizeof(struct prog));
p->stmts = parseStmts(alct, ts);
return p;
}
Stmts parseStmts(Allocator alct, TokenStream ts) {
Stmts ss = allocate(alct, sizeof(struct stmts));
ss->stmts = allocate(alct, sizeof(Stmt));
ss->stmts[0] = NULL;
int capacity = 0;
int len = 0;
while (peekToken(alct, ts)->type != ENDOFFILE) {
Stmt s = parseStmt(alct, ts);
if (s == NULL) continue;
if (len == capacity) {
capacity = capacity * 2 + 1;
ss->stmts = realloc(ss->stmts, sizeof(Stmt*) * capacity);
}
// expect newline
if (peekToken(alct, ts)->type == NEWLINE) {
nextToken(alct, ts);
} else {
fprintf(stderr, "%d:%d Expect NEWLINE.\n", peekToken(alct, ts)->line, peekToken(alct, ts)->col);
}
ss->stmts[len] = s;
len++;
}
ss->stmts[len] = NULL;
return ss;
}
Label parseLabel(Allocator alct, TokenStream ts) {
Token t = nextToken(alct, ts);
if (t->type != TAG) {
fprintf(stderr, "%d:%d Expect LABEL.\n", t->line, t->col);
exit(-1);
}
Label l = allocate(alct, sizeof(Label));
l->name = t->sval;
t = nextToken(alct, ts);
if (t->type != COLON) {
fprintf(stderr, "%d:%d Expect COLON.\n", t->line, t->col);
exit(-1);
}
return l;
}
Stmt parseStmt(Allocator alct, TokenStream ts) {
Token t = peekToken(alct, ts);
Stmt stmt = allocate(alct, sizeof(struct stmt));
stmt->label = NULL;
stmt->instr = NULL;
if (t->type == TAG) {
stmt->label = parseLabel(alct, ts);
if (peekToken(alct, ts)->type == NEWLINE) {
return stmt;
}
}
if (t->type == OP) {
stmt->instr = parseInstr(alct, ts);
if (peekToken(alct, ts)->type == NEWLINE) {
return stmt;
}
}
if (t->type == NEWLINE) {
return NULL;
}
fprintf(stderr, "%d:%d Expect lable + instruction, lable, or instruction.", t->line, t->col);
exit(-1);
}
enum op parseOp(Allocator alct, TokenStream ts) {
Token t = nextToken(alct, ts);
enum op op;
if (t->type == OP) {
op = str2op(t->sval);
if (op == OPEND) {
fprintf(stderr, "%d:%d Invalid OP.\n", t->line, t->col);
exit(-1);
}
} else {
fprintf(stderr, "%d:%d Expect OP.\n", t->line, t->col);
exit(-1);
}
return op;
}
Instr parseInstr(Allocator alct, TokenStream ts) {
Token t = nextToken(alct, ts);
Instr i = allocate(alct, sizeof(struct instr));
i->tagName = NULL;
if (t->type == OP) {
i->op = parseOp(alct, ts);
t = peekToken(alct, ts);
if (t->type == ARG) {
Arg a = allocate(alct, sizeof(struct arg));
a->ival = t->ival;
a->fval = t->fval;
i->arg = a;
nextToken(alct, ts);
} else if (t->type == TAG) {
i->tagName = t->sval;
nextToken(alct, ts);
}
}
return i;
}
|