-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.c
300 lines (282 loc) · 6.25 KB
/
compile.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
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
#include "getSource.h"
#include "codegen.h"
#ifndef TBL
#define TBL
#include "table.h"
#endif
#define FIRSTADDR 2
static Token token;
static void block(int nameTableIndex);
static void constDecl();
static void varDecl();
static void funcDecl();
static void statement();
static void expression();
static void term();
static void factor();
static void condition();
static int isStBeginKey(Token t);
void compile()
{
initSource();
token = nextToken();
blockBegin(FIRSTADDR);
block(0);
listCode();
}
void block(int nameTableIndex)
{
int backPatchIndex;
backPatchIndex = genCodeWithValue(jmp, 0);
while(1) {
switch (token.kind) {
case Const:
token = nextToken();
constDecl();
continue;
case Var:
token = nextToken();
varDecl();
continue;
case Func:
token = nextToken();
funcDecl();
continue;
default:
break;
}
break;
}
backPatch(backPatchIndex);
changeV(nameTableIndex, nextCode());
genCodeWithValue(ict, LocalAddr());
statement();
genCodeReturn();
blockEnd();
}
void constDecl()
{
Token prev_token;
while(1) {
prev_token = token;
token = checkGet(nextToken(), Equal);
if (token.kind == Num) enterTconst(prev_token.u.id, token.u.value);
token = nextToken();
if (token.kind != Comma) break;
token = nextToken();
}
token = checkGet(token, Semicolon);
}
void varDecl()
{
while(1) {
enterTvar(token.u.id);
token = nextToken();
if (token.kind != Comma) break;
token = nextToken();
}
token = checkGet(token, Semicolon);
}
void funcDecl()
{
int funcIndex;
funcIndex = enterTfunc(token.u.id, nextCode());
token = checkGet(nextToken(), Lparen);
blockBegin(FIRSTADDR);
while (1) {
if (token.kind != Id) break;
enterTpar(token.u.id);
token = nextToken();
if (token.kind != Comma) break;
token = nextToken();
}
token = checkGet(token, Rparen);
endFunctionParam();
block(funcIndex);
token = checkGet(token, Semicolon);
}
void statement()
{
int tableIndex;
int backPatchIfIndex, backPatchLoopExitIndex, backPatchLoopStartIndex;
while(1) {
switch(token.kind) {
case Id:
tableIndex = searchT(token.u.id);
token = checkGet(nextToken(), Assign);
expression();
genCodeWithAddr(sto, tableIndex);
return;
case If:
token = nextToken();
condition();
token = checkGet(token, Then);
backPatchIfIndex = genCodeWithValue(jpc, 0);
statement();
backPatch(backPatchIfIndex);
return;
case Ret:
token = nextToken();
expression();
genCodeReturn();
return;
case Begin:
token = nextToken();
while(1) {
statement();
while(1) {
if (token.kind == Semicolon) {
token = nextToken();
break;
}
if (token.kind == End) {
token = nextToken();
return;
}
if (isStBeginKey(token)) break;
token = nextToken();
}
}
case While:
token = nextToken();
backPatchLoopStartIndex = nextCode();
condition();
token = checkGet(token, Do);
backPatchLoopExitIndex = genCodeWithValue(jpc, 0);
statement();
genCodeWithValue(jmp, backPatchLoopStartIndex);
backPatch(backPatchLoopExitIndex);
return;
case Write:
token = nextToken();
expression();
genCodeOperator(wrt);
return;
case Writeln:
token = nextToken();
genCodeOperator(wrl);
return;
case End: case Semicolon:
return;
default:
token = nextToken();
continue;
}
}
}
void expression()
{
KeyId k;
k = token.kind;
if (k == Plus || k == Minus){
token = nextToken();
term();
if (k == Minus) genCodeOperator(neg);
} else {
term();
}
k = token.kind;
while (k == Plus || k == Minus){
token = nextToken();
term();
if (k == Minus) genCodeOperator(sub);
else genCodeOperator(add);
k = token.kind;
}
}
void term()
{
KeyId k;
factor();
k = token.kind;
while (k == Mult || k == Div){
token = nextToken();
factor();
if (k==Mult) genCodeOperator(mul);
else genCodeOperator(div);
k = token.kind;
}
}
void factor()
{
int tIndex, i;
KindT k;
if (token.kind == Id) {
tIndex = searchT(token.u.id);
k = kindT(tIndex);
switch (k) {
case varId: case parId:
genCodeWithAddr(lod, tIndex);
token = nextToken();
break;
case constId:
genCodeWithValue(lit, val(tIndex));
token = nextToken();
break;
case funcId:
token = nextToken();
i=0;
token = nextToken();
if (token.kind != Rparen) {
for (; ; ) {
expression();
i++;
if (token.kind == Comma) {
token = nextToken();
continue;
}
token = checkGet(token, Rparen);
break;
}
} else token = nextToken();
genCodeWithAddr(cal, tIndex);
break;
default:
break;
}
} else if (token.kind == Num) {
genCodeWithValue(lit, token.u.value);
token = nextToken();
} else if (token.kind == Lparen) {
token = nextToken();
expression();
token = checkGet(token, Rparen);
}
switch (token.kind){
case Id: case Num: case Lparen:
factor();
default:
return;
}
}
void condition()
{
KeyId k;
if (token.kind == Odd) {
token = nextToken();
expression();
genCodeOperator(odd);
} else {
expression();
k = token.kind;
token = nextToken();
expression();
switch (k) {
case Equal: genCodeOperator(eq); break;
case Lss: genCodeOperator(ls); break;
case Gtr: genCodeOperator(gr); break;
case NotEq: genCodeOperator(neq); break;
case LssEq: genCodeOperator(lseq); break;
case GtrEq: genCodeOperator(greq); break;
default: break;
}
}
}
int isStBeginKey(Token t)
{
switch(t.kind) {
case If: case Begin: case Ret: case While: case Write: case Writeln:
return 1;
default: return 0;
}
}