-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
250 lines (206 loc) · 4.74 KB
/
parser.c
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
#include "parser.h"
#include <stdio.h>
#include <stdlib.h>
enum {
T_POP = VAL_POP,
T_PEEK = VAL_PEEK,
T_PUSH = VAL_PUSH,
T_SP = VAL_SP,
T_PC = VAL_PC,
T_O = VAL_O,
T_JSR,
T_IDENT, T_INT, T_COMMA, T_PLUS,
T_LBRACKET, T_RBRACKET,
T_COLON, T_NL,
T_A, T_B, T_C,
T_X, T_Y, T_Z,
T_I, T_J,
T_EOF
};
typedef struct parser {
int token, line;
char const* cur;
char const* tok_data;
char const* tok_data_end;
u16 tok_int_data;
u16* dest;
u16* dest_end;
} parser;
#define KW4(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
#define KW3(a,b,c) (((a)<<16)+((b)<<8)+(c))
#define KW2(a,b) (((a)<<8)+(b))
static void lex(parser* P) {
char c;
repeat:
c = *P->cur;
if(!c) {
P->token = T_EOF;
return;
}
switch(c) {
case '\0':
P->token = T_EOF;
return;
case '[': ++P->cur; P->token = T_LBRACKET; break;
case ']': ++P->cur; P->token = T_RBRACKET; break;
case ':': ++P->cur; P->token = T_COLON; break;
case ',': ++P->cur; P->token = T_COMMA; break;
case '+': ++P->cur; P->token = T_PLUS; break;
case '\n': ++P->cur; P->token = T_NL; ++P->line; break;
case '\r': case ' ': case '\t':
++P->cur;
goto repeat;
default: {
if((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z')) {
u32 kw = 0;
P->tok_data = P->cur;
do {
kw = (kw << 8) + c;
c = *++P->cur;
} while((c >= 'a' && c <= 'z')
|| (c >= 'A' && c <= 'Z'));
P->tok_data_end = P->cur;
P->token = T_IDENT;
if((P->tok_data_end - P->tok_data) <= 4) {
switch(kw) {
case KW3('S','E','T'): P->token = OP_SET; break;
case KW3('A','D','D'): P->token = OP_ADD; break;
case KW3('S','U','B'): P->token = OP_SUB; break;
case KW3('J','S','R'): P->token = T_JSR; break;
case KW3('I','F','E'): P->token = OP_IFE; break;
case KW3('I','F','G'): P->token = OP_IFG; break;
case KW4('P','U','S','H'): P->token = T_PUSH; break;
case KW4('P','E','E','K'): P->token = T_PEEK; break;
case KW3('P','O','P'): P->token = T_POP; break;
case KW2('P','C'): P->token = T_PC; break;
case KW2('S','P'): P->token = T_SP; break;
case 'O': P->token = T_O; break;
case 'A': P->token = T_A; break;
case 'B': P->token = T_B; break;
case 'C': P->token = T_C; break;
case 'X': P->token = T_X; break;
case 'Y': P->token = T_Y; break;
case 'Z': P->token = T_Z; break;
case 'I': P->token = T_I; break;
case 'J': P->token = T_J; break;
}
}
} else if(c >= '0' && c <= '9') {
u16 i = 0;
P->tok_data = P->cur;
while(c >= '0' && c <= '9') {
i = (i * 10) + (c - '0');
c = *++P->cur;
}
P->tok_data_end = P->cur;
P->tok_int_data = i;
P->token = T_INT;
}
}
}
}
#define W16(w) { *P->dest++ = (w); }
static void parse_error(parser* P) {
// TODO: Error
printf("Error on line %d\n", P->line);
abort();
}
static int test(parser* P, int tok) {
if(P->token == tok) {
lex(P);
return 1;
}
return 0;
}
static void expect(parser* P, int tok) {
if(P->token != tok) {
parse_error(P);
return;
}
lex(P);
}
static u16 r_operand(parser* P) {
u16 v = (u16)P->token;
switch(v) {
case T_INT: {
u16 i = (u16)P->tok_int_data;
lex(P);
if(i < 0x20) {
return i + 0x20;
} else {
W16(i);
return VAL_LIT;
}
}
case T_POP: case T_PEEK: case T_PUSH:
case T_SP: case T_PC: case T_O:
lex(P);
return v;
case T_LBRACKET: {
int used = 0;
lex(P);
v = 0x1e;
do {
if(!(used & 1) && P->token >= T_A && P->token <= T_J) {
used |= 1;
v = 0x8 + P->token - T_A;
lex(P);
} else if(!(used & 2) && P->token == T_INT) {
used |= 2;
W16((u16)P->tok_int_data);
lex(P);
} else {
parse_error(P);
}
} while(test(P, T_PLUS));
expect(P, T_RBRACKET);
return (used == 3) ? v + 0x8 : v;
}
default: {
if(v >= T_A && v <= T_J) {
lex(P);
return v - T_A;
} else {
printf("Invalid operand\n");
parse_error(P);
return 0;
}
}
}
}
void r_file(parser* P) {
while(P->token < 16 || P->token == T_NL || P->token == T_COLON || P->token == T_JSR) {
if(P->token < 16) {
// Instruction
u16* first = P->dest++;
u16 instr = P->token;
lex(P);
instr |= r_operand(P) << 4;
expect(P, T_COMMA);
instr |= r_operand(P) << 10;
*first = instr;
} else if(P->token == T_JSR) {
u16* first = P->dest++;
u16 instr = (OP_EXT_JSR << 4);
lex(P);
instr |= r_operand(P) << 10;
*first = instr;
} else if(P->token == T_NL) {
// Ignore
lex(P);
} else {
parse_error(P);
}
}
expect(P, T_EOF);
}
void parse(context* C, char const* str) {
parser P;
P.line = 1;
P.cur = str;
P.dest = C->data;
P.dest_end = C->data + 0x10000;
lex(&P);
r_file(&P);
}