-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompilationEngine.java
495 lines (475 loc) · 17.1 KB
/
CompilationEngine.java
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import java.io.File;
import java.util.regex.Pattern;
/**
* The compilation engine gets its input from a {@link JackTokenizer} and emits its output to an output file. The output
* is generated by a series of <code>compile</code><i>xxx</i> routines, each designed to handle the compilation of a
* specific Jack language construct <i>xxx</i>. The contract between routines is that each <code>compile</code><i>xxx</i>
* routine should get from the input, and handle, all the tokens that make up <i>xxx</i>, advance the tokenizer exactly
* beyond these tokens, and output the parsing of <i>xxx</i>. As a rule, each <code>compile</code><i>xxx</i> is called
* only if the current token is <i>xxx</i>.
*
* @author Maarten Derks
*/
class CompilationEngine {
private final JackTokenizer jt;
private final SymbolTable cst, sst;
private final VMWriter vmw;
private String className, subroutineName, subroutineType;
private int ifIndex, whileIndex;
/**
* Creates a new compilation engine with the given input and output.
*
* @param in Input stream/file
* @param out Output stream/file
*/
CompilationEngine(File in, File out) throws Exception {
jt = new JackTokenizer(in);
vmw = new VMWriter(out);
cst = new SymbolTable();
sst = new SymbolTable();
jt.advance();
}
/**
* Compiles a complete class.
*/
void compileClass() {
process("class");
className = jt.identifier();
jt.advance();
process("{");
while (jt.tokenType() == TokenType.KEYWORD &&
(jt.keyWord() == Keyword.STATIC || jt.keyWord() == Keyword.FIELD)) {
compileClassVarDec();
}
while (jt.tokenType() == TokenType.KEYWORD &&
(jt.keyWord() == Keyword.CONSTRUCTOR || jt.keyWord() == Keyword.FUNCTION || jt.keyWord() == Keyword.METHOD)) {
compileSubroutine();
}
process("}");
vmw.close();
}
/**
* Compiles a static variable declaration, or a field declaration.
*/
private void compileClassVarDec() {
String kind = process("static|field");
String type = jt.identifier();
jt.advance();
cst.define(jt.identifier(), type, Kind.valueOf(kind.toUpperCase()));
jt.advance();
while (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == ',') {
process(",");
cst.define(jt.identifier(), type, Kind.valueOf(kind.toUpperCase()));
jt.advance();
}
process(";");
}
/**
* Compiles a complete method, function, or constructor.
*/
private void compileSubroutine() {
sst.reset();
ifIndex = whileIndex = 0;
subroutineType = process("constructor|function|method");
if (Pattern.matches("method", subroutineType)) {
sst.define("this", className, Kind.ARG);
}
process("void|int|char|boolean|[a-zA-Z_][a-zA-Z0-9_]*");
subroutineName = jt.identifier();
jt.advance();
process("(");
compileParameterList();
process(")");
compileSubroutineBody();
}
/**
* Compiles a (possibly empty) parameter list. Does not handle the enclosing parentheses tokens ( and ).
*/
private void compileParameterList() {
if ((jt.tokenType() == TokenType.KEYWORD && (jt.keyWord() == Keyword.INT || jt.keyWord() == Keyword.BOOLEAN)) || (jt.tokenType() == TokenType.IDENTIFIER)) {
String type = jt.identifier();
jt.advance();
sst.define(jt.identifier(), type, Kind.ARG);
jt.advance();
}
while (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == ',') {
process(",");
String type = jt.identifier();
jt.advance();
sst.define(jt.identifier(), type, Kind.ARG);
jt.advance();
}
}
/**
* Compiles a subroutine's body.
*/
private void compileSubroutineBody() {
process("{");
while (jt.tokenType() == TokenType.KEYWORD && jt.keyWord() == Keyword.VAR) {
compileVarDec();
}
vmw.writeFunction(className + "." + subroutineName, sst.varCount(Kind.VAR));
switch (subroutineType) {
case "constructor":
vmw.writePush(Segment.CONSTANT, cst.varCount(Kind.FIELD));
vmw.writeCall("Memory.alloc", 1);
vmw.writePop(Segment.POINTER, 0);
break;
case "method":
vmw.writePush(Segment.ARGUMENT, 0);
vmw.writePop(Segment.POINTER, 0);
break;
}
compileStatements();
process("}");
}
/**
* Compiles a <code>var</code> declaration.
*/
private void compileVarDec() {
process("var");
String type = jt.identifier();
jt.advance();
sst.define(jt.identifier(), type, Kind.VAR);
jt.advance();
while (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == ',') {
process(",");
sst.define(jt.identifier(), type, Kind.VAR);
jt.advance();
}
process(";");
}
/**
* Compiles a sequence of statements. Does not handle the enclosing curly bracket tokens { and }.
*/
private void compileStatements() {
while (jt.tokenType() == TokenType.KEYWORD && (jt.keyWord() == Keyword.LET || jt.keyWord() == Keyword.IF || jt.keyWord() == Keyword.WHILE || jt.keyWord() == Keyword.DO || jt.keyWord() == Keyword.RETURN)) {
switch (jt.keyWord()) {
case LET: compileLet(); break;
case IF: compileIf(); break;
case WHILE: compileWhile(); break;
case DO: compileDo(); break;
case RETURN: compileReturn(); break;
}
}
}
/**
* Compiles a <code>let</code> statement.
*/
private void compileLet() {
process("let");
String var = jt.identifier();
jt.advance();
if (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == '[') {
process("[");
compileExpression();
process("]");
switch (sst.kindOf(var)) {
case ARG:
vmw.writePush(Segment.ARGUMENT, sst.indexOf(var));
break;
case VAR:
vmw.writePush(Segment.LOCAL, sst.indexOf(var));
break;
}
vmw.writeArithmetic(Command.ADD);
process("=");
compileExpression();
vmw.writePop(Segment.TEMP, 0);
vmw.writePop(Segment.POINTER,1);
vmw.writePush(Segment.TEMP, 0);
vmw.writePop(Segment.THAT, 0);
} else {
process("=");
compileExpression();
switch (sst.kindOf(var)) {
case VAR:
vmw.writePop(Segment.LOCAL, sst.indexOf(var));
break;
case ARG:
vmw.writePop(Segment.ARGUMENT, sst.indexOf(var));
case NONE:
switch (cst.kindOf(var)) {
case FIELD:
vmw.writePop(Segment.THIS, cst.indexOf(var));
break;
case STATIC:
vmw.writePop(Segment.STATIC, cst.indexOf(var));
break;
}
break;
}
}
process(";");
}
/**
* Compiles an <code>if</code> statement, possibly with a trailing <code>else</code> clause.
*/
private void compileIf() {
int i = ifIndex++;
process("if");
process("(");
compileExpression();
vmw.writeIf("IF_TRUE" + i);
vmw.writeGoto("IF_FALSE" + i);
vmw.writeLabel("IF_TRUE" + i);
process(")");
process("{");
compileStatements();
process("}");
if (jt.tokenType() == TokenType.KEYWORD && jt.keyWord() == Keyword.ELSE) {
vmw.writeGoto("IF_END" + i);
vmw.writeLabel("IF_FALSE" + i);
process("else");
process("{");
compileStatements();
process("}");
vmw.writeLabel("IF_END" + i);
} else {
vmw.writeLabel("IF_FALSE" + i);
}
}
/**
* Compiles a <code>while</code> statement.
*/
private void compileWhile() {
int i = whileIndex;
vmw.writeLabel("WHILE_EXP" + i);
process("while");
process("(");
compileExpression();
process(")");
vmw.writeArithmetic(Command.NOT);
vmw.writeIf("WHILE_END" + i);
process("{");
whileIndex++;
compileStatements();
vmw.writeGoto("WHILE_EXP" + i);
process("}");
vmw.writeLabel("WHILE_END" + i);
}
/**
* Compiles a <code>do</code> statement.
*/
private void compileDo() {
process("do");
compileExpression();
process(";");
vmw.writePop(Segment.TEMP, 0);
}
/**
* Compiles a <code>return</code> statement.
*/
private void compileReturn() {
process("return");
if (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == ';') {
vmw.writePush(Segment.CONSTANT, 0);
} else {
compileExpression();
}
process(";");
vmw.writeReturn();
}
/**
* Compiles an expression.
*/
private void compileExpression() {
compileTerm();
while (jt.tokenType() == TokenType.SYMBOL && (
jt.symbol() == '+' ||
jt.symbol() == '-' ||
jt.symbol() == '*' ||
jt.symbol() == '/' ||
jt.symbol() == '&' ||
jt.symbol() == '|' ||
jt.symbol() == '<' ||
jt.symbol() == '>' ||
jt.symbol() == '='
)) {
char op = jt.symbol();
jt.advance();
compileTerm();
switch (op) {
case '+':
vmw.writeArithmetic(Command.ADD);
break;
case '-':
vmw.writeArithmetic(Command.SUB);
break;
case '*':
vmw.writeCall("Math.multiply", 2);
break;
case '/':
vmw.writeCall("Math.divide", 2);
break;
case '<':
vmw.writeArithmetic(Command.LT);
break;
case '>':
vmw.writeArithmetic(Command.GT);
break;
case '=':
vmw.writeArithmetic(Command.EQ);
break;
case '&':
vmw.writeArithmetic(Command.AND);
break;
case '|':
vmw.writeArithmetic(Command.OR);
break;
}
}
}
/**
* Compiles a (possibly empty) comma-separated list of expressions.
*
* @return the number of expressions in the list
*/
private int compileExpressionList() {
int numberOfExpressions = 0;
while (jt.symbol() != ')') {
compileExpression();
numberOfExpressions++;
while (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == ',') {
process(",");
compileExpression();
numberOfExpressions++;
}
}
return numberOfExpressions;
}
/**
* Compiles a <i>term</i>. If the current token is an <i>identifier</i>, the routine must distinguish between a
* <i>variable</i>, an <i>array entry</i>, or a <i>subroutine call</i>. A single look-ahead token, which may be one
* of "[", "(", or ".", suffices to distinguish between the possibilities. Any other token is not part of this term
* and should not be advanced over.
*/
private void compileTerm() {
switch (jt.tokenType()) {
case INT_CONST:
vmw.writePush(Segment.CONSTANT, jt.intVal());
jt.advance();
break;
case STRING_CONST:
vmw.writePush(Segment.CONSTANT, jt.stringVal().length());
vmw.writeCall("String.new", 1);
for (int i = 0; i < jt.stringVal().length(); i++) {
vmw.writePush(Segment.CONSTANT, jt.stringVal().charAt(i));
vmw.writeCall("String.appendChar", 2);
}
jt.advance();
break;
case KEYWORD:
switch (jt.keyWord()) {
case TRUE:
vmw.writePush(Segment.CONSTANT, 0);
vmw.writeArithmetic(Command.NOT);
break;
case FALSE:
case NULL:
vmw.writePush(Segment.CONSTANT, 0);
break;
case THIS:
vmw.writePush(Segment.POINTER, 0);
break;
}
jt.advance();
break;
case IDENTIFIER:
String name = jt.identifier();
jt.advance();
if (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == '[') {
process("[");
compileExpression();
process("]");
vmw.writePush(Segment.LOCAL, sst.indexOf(name));
vmw.writeArithmetic(Command.ADD);
vmw.writePop(Segment.POINTER, 1);
vmw.writePush(Segment.THAT, 0);
} else if (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == '.') {
String type;
int nVars = 0;
if (sst.kindOf(name) != Kind.NONE) {
type = sst.typeOf(name);
vmw.writePush(Segment.LOCAL, sst.indexOf(name));
nVars++;
} else if (cst.kindOf(name) != Kind.NONE) {
type = cst.typeOf(name);
vmw.writePush(Segment.THIS, cst.indexOf(name));
nVars++;
} else if (Character.isUpperCase(name.charAt(0))) {
type = name;
} else {
type = className;
}
process(".");
name = type + "." + jt.identifier();
jt.advance();
process("(");
nVars = nVars + compileExpressionList();
process(")");
vmw.writeCall(name, nVars);
} else if (jt.tokenType() == TokenType.SYMBOL && jt.symbol() == '(') {
vmw.writePush(Segment.POINTER, 0);
process("(");
int nVars = compileExpressionList();
process(")");
vmw.writeCall(className + "." + name, 1 + nVars);
} else {
switch (sst.kindOf(name)) {
case ARG:
vmw.writePush(Segment.ARGUMENT, sst.indexOf(name));
break;
case VAR:
vmw.writePush(Segment.LOCAL, sst.indexOf(name));
break;
case NONE:
switch (cst.kindOf(name)) {
case FIELD:
vmw.writePush(Segment.THIS, cst.indexOf(name));
break;
case STATIC:
vmw.writePush(Segment.STATIC, cst.indexOf(name));
break;
}
break;
}
}
break;
case SYMBOL:
switch (jt.symbol()) {
case '(':
process("(");
compileExpression();
process(")");
break;
case '-':
case '~':
char symbol = jt.symbol();
jt.advance();
compileTerm();
vmw.writeArithmetic(symbol == '-' ? Command.NEG : Command.NOT);
break;
}
break;
}
}
/**
* A helper routine that handles the current token, and advances to get the next token.
*
* @param str
*/
private String process(String str) {
String token = jt.identifier();
switch (str) {
case "{":
case "(":
case ")":
case "[":
str = "\\".concat(str);
}
if (Pattern.matches(str, token));
if (jt.hasMoreTokens()) jt.advance();
return token;
}
}