-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompilationEngine.cs
645 lines (615 loc) · 20.9 KB
/
CompilationEngine.cs
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
using System;
using System.Collections.Generic;
namespace JackCompiler
{
/// <summary>
/// Generates the compiler's output.
/// </summary>
class CompilationEngine
{
List<Token> tokens;
Token current;
VMWriter vmWriter;
SymbolTable symbolTable;
string className;
int labelIndex = 0;
/// <summary>
/// Creates a new compilation engine with the given input and output.
/// The next routine called must be CompileClass.
/// </summary>
public CompilationEngine(List<Token> tokenList, VMWriter writer)
{
tokens = tokenList;
current = tokens[0];
vmWriter = writer;
symbolTable = new SymbolTable();
CompileClass();
}
/// <summary>
/// Compiles a complete class.
/// </summary>
void CompileClass()
{
//class
Advance();
//class name
className = current.Identifier;
Advance();
//'{'
Advance();
//classVarDec*
CompileClassVarDec();
//subroutineDec*
CompileSubroutineDec();
//'}'
Advance();
}
/// <summary>
/// Compiles a static variable declaration or a field declaration.
/// </summary>
void CompileClassVarDec()
{
while(current.Keyword == Keyword.STATIC || current.Keyword == Keyword.FIELD)
{
//'static'|'field'
SymbolKind kind;
if(current.Keyword == Keyword.STATIC)
kind = SymbolKind.STATIC;
else
kind = SymbolKind.FIELD;
Advance();
//type
string type = CompileType();
//varName
symbolTable.Define(current.Identifier, type, kind);
Advance();
//(, varName)*
while(current.Type == TokenType.SYMBOL && current.Symbol == ',')
{
Advance();
symbolTable.Define(current.Identifier, type, kind);
Advance();
}
//';'
Advance();
}
}
/// <summary>
/// Compiles a complete method, function or constructor.
/// </summary>
void CompileSubroutineDec()
{
while(current.Keyword == Keyword.FUNCTION || current.Keyword == Keyword.METHOD ||
current.Keyword == Keyword.CONSTRUCTOR)
{
symbolTable.StartSubroutine();
Keyword keyword = current.Keyword;
//first argument of a method is always this
if(current.Keyword == Keyword.METHOD)
symbolTable.Define("this", className, SymbolKind.ARG);
//'constructor'|'function'|'method'
Advance();
//'void|type'
string type;
if(current.Keyword == Keyword.VOID)
{
type = "void";
Advance();
}
else
type = CompileType();
//subroutineName
string name = className + "." + current.Identifier;
Advance();
//'('parameterList')'
Advance();
CompileParameterList();
Advance();
//subroutineBody
CompileSubroutineBody(keyword, name);
}
}
/// <summary>
/// Compiles a (possibly empty) parameter list. Does not handle the enclosing "()".
/// </summary>
void CompileParameterList()
{
if(current.Type == TokenType.SYMBOL && current.Symbol == ')')
return;
//type varName
string type = CompileType();
symbolTable.Define(current.Identifier, type, SymbolKind.ARG);
Advance();
//(',' type varName)*
while(current.Type == TokenType.SYMBOL && current.Symbol == ',')
{
Advance();
type = CompileType();
symbolTable.Define(current.Identifier, type, SymbolKind.ARG);
Advance();
}
}
/// <summary>
/// Compiles a subroutine's body.
/// </summary>
void CompileSubroutineBody(Keyword keyword, string functionName)
{
//'{'
Advance();
//varDec*
while(current.Keyword == Keyword.VAR)
CompileVarDec();
//VM function declaration
vmWriter.WriteFunction(functionName, symbolTable.VarCount(SymbolKind.VAR));
//METHOD and CONSTRUCTOR need to load this pointer
if (keyword == Keyword.METHOD)
{
//A Jack method with k arguments is compiled into a VM function that operates on k + 1 arguments.
//The first argument always refers to 'this'.
vmWriter.WritePush(Segment.ARG, 0);
vmWriter.WritePop(Segment.POINTER, 0);
}
else if (keyword == Keyword.CONSTRUCTOR){
//A Jack function or constructor with k arguments is compiled into a VM function that operates on k arguments.
vmWriter.WritePush(Segment.CONST, symbolTable.VarCount(SymbolKind.FIELD));
vmWriter.WriteCall("Memory.alloc", 1);
vmWriter.WritePop(Segment.POINTER, 0);
}
//statements
CompileStatements();
//'}'
Advance();
}
/// <summary>
/// Compiles a var declaration.
/// </summary>
void CompileVarDec()
{
//var
Advance();
//type
string type = CompileType();
//varName (, varName)*
symbolTable.Define(current.Identifier, type, SymbolKind.VAR);
Advance();
while(current.Type == TokenType.SYMBOL && current.Symbol == ',')
{
Advance();
symbolTable.Define(current.Identifier, type, SymbolKind.VAR);
Advance();
}
//';'
Advance();
}
/// <summary>
/// Compiles a sequence of statements. Does not handle the enclosing "()".
/// </summary>
void CompileStatements()
{
while(current.Keyword == Keyword.LET ||
current.Keyword == Keyword.IF ||
current.Keyword == Keyword.WHILE ||
current.Keyword == Keyword.DO ||
current.Keyword == Keyword.RETURN)
{
if(current.Keyword == Keyword.LET)
CompileLet();
else if(current.Keyword == Keyword.IF)
CompileIf();
else if(current.Keyword == Keyword.WHILE)
CompileWhile();
else if(current.Keyword == Keyword.DO)
CompileDo();
else if(current.Keyword == Keyword.RETURN)
CompileReturn();
}
}
/// <summary>
/// Compiles a let statement.
/// </summary>
void CompileLet()
{
bool isArray = false;
//'let'
Advance();
//varName
string name = current.Identifier;
Advance();
//'['expression']'
if(current.Symbol == '[')
{
isArray = true;
Advance();
//push base address of array variable into stack
vmWriter.WritePush(symbolTable.SegmentOf(name),symbolTable.IndexOf(name));
CompileExpression();
Advance();
//add offset to base
vmWriter.WriteArithmetic(Command.ADD);
}
//'='
Advance();
//expression
CompileExpression();
if(isArray)
{
//pop expression value to temp
vmWriter.WritePop(Segment.TEMP, 0);
//pop base + index to that
vmWriter.WritePop(Segment.POINTER, 1);
//pop expression value to *(base + index)
vmWriter.WritePush(Segment.TEMP, 0);
vmWriter.WritePop(Segment.THAT, 0);
}
else
{
//pop expression value
vmWriter.WritePop(symbolTable.SegmentOf(name), symbolTable.IndexOf(name));
}
//';'
Advance();
}
/// <summary>
/// Compiles an if statement, possibly with a trailing else clause.
/// </summary>
void CompileIf()
{
string elseLabel = NewLabel("IF");
string endLabel = NewLabel("IF");
//if
Advance();
//'('
Advance();
//expression
CompileExpression();
//')'
Advance();
//if ~condition goto else
vmWriter.WriteArithmetic(Command.NOT);
vmWriter.WriteIf(elseLabel);
//'{'
Advance();
//statements
CompileStatements();
//'}'
Advance();
//if condition goto end
vmWriter.WriteGoto(endLabel);
//else'{'statements'}'
vmWriter.WriteLabel(elseLabel);
if(current.Keyword == Keyword.ELSE)
{
Advance();
Advance();
CompileStatements();
Advance();
}
vmWriter.WriteLabel(endLabel);
}
/// <summary>
/// Compiles a while statement.
/// </summary>
void CompileWhile()
{
string startLabel = NewLabel("WhileStart");
string endLabel = NewLabel("WhileEnd");
//start of the loop
vmWriter.WriteLabel(startLabel);
//while
Advance();
//'('
Advance();
//expression
CompileExpression();
//')'
Advance();
//if ~condition go to end
vmWriter.WriteArithmetic(Command.NOT);
vmWriter.WriteIf(endLabel);
//'{'
Advance();
//statements
CompileStatements();
//'}'
Advance();
//if condition go to start or continue
vmWriter.WriteGoto(startLabel);
vmWriter.WriteLabel(endLabel);
}
/// <summary>
/// Compiles an do statement.
/// </summary>
void CompileDo()
{
//do
Advance();
//subroutineCall
CompileSubroutineCall();
//';'
Advance();
//pop return value
vmWriter.WritePop(Segment.TEMP, 0);
}
/// <summary>
/// Compiles a return statement.
/// </summary>
void CompileReturn()
{
//return
Advance();
//expression?
if(!(current.Type == TokenType.SYMBOL && current.Symbol == ';'))
{
CompileExpression();
}
else
{
vmWriter.WritePush(Segment.CONST, 0);
}
//';'
Advance();
vmWriter.WriteReturn();
}
/// <summary>
/// Compiles an expression.
/// </summary>
void CompileExpression()
{
//term
CompileTerm();
//(op term)*
while(IsOp(current.Symbol))
{
//op
char symbol = current.Symbol;
Advance();
//term
CompileTerm();
switch(symbol)
{
case '+':
vmWriter.WriteArithmetic(Command.ADD);
break;
case '-':
vmWriter.WriteArithmetic(Command.SUB);
break;
case '*':
vmWriter.WriteCall("Math.multiply", 2);
break;
case '/':
vmWriter.WriteCall("Math.divide", 2);
break;
case '<':
vmWriter.WriteArithmetic(Command.LT);
break;
case '>':
vmWriter.WriteArithmetic(Command.GT);
break;
case '=':
vmWriter.WriteArithmetic(Command.EQ);
break;
case '&':
vmWriter.WriteArithmetic(Command.AND);
break;
case '|':
vmWriter.WriteArithmetic(Command.OR);
break;
}
}
}
/// <summary>
/// Compiles a term. If the current token is an identifier, the routine must distinguish between
/// a variable, an array entry, or a subroutine call. 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.
/// </summary>
void CompileTerm()
{
//unaryOp Term
if(current.Type == TokenType.SYMBOL && IsUnaryOp(current.Symbol))
{
Advance();
CompileTerm();
if(current.Symbol == '-')
vmWriter.WriteArithmetic(Command.NEG);
else
vmWriter.WriteArithmetic(Command.NOT);
}
//'('expression')'
else if(current.Type == TokenType.SYMBOL && current.Symbol == '(')
{
Advance();
CompileExpression();
Advance();
}
//keywordConstant
else if(current.Type == TokenType.KEYWORD &&
(current.Keyword == Keyword.THIS || current.Keyword == Keyword.NULL ||
current.Keyword == Keyword.TRUE || current.Keyword == Keyword.FALSE))
{
switch(current.Keyword)
{
case Keyword.THIS:
vmWriter.WritePush(Segment.POINTER, 0);
break;
case Keyword.NULL:
vmWriter.WritePush(Segment.CONST, 0);
break;
case Keyword.TRUE:
vmWriter.WritePush(Segment.CONST, 0);
vmWriter.WriteArithmetic(Command.NOT); //~0 = -1
break;
case Keyword.FALSE:
vmWriter.WritePush(Segment.CONST, 0);
break;
}
Advance();
}
//integerConstant
else if(current.Type == TokenType.INT_CONST)
{
vmWriter.WritePush(Segment.CONST, current.IntVal);
Advance();
}
//stringConstant
else if(current.Type == TokenType.STRING_CONST)
{
string str = current.StringVal;
//new string
vmWriter.WritePush(Segment.CONST, str.Length);
vmWriter.WriteCall("String.new", 1);
//append each char
foreach(char ch in str.ToCharArray())
{
vmWriter.WritePush(Segment.CONST, (int)ch);
vmWriter.WriteCall("String.appendChar", 2);
}
Advance();
}
//identifier branch
else if(current.Type == TokenType.IDENTIFIER)
{
string name = current.Identifier;
//look ahead
Token next = tokens[tokens.IndexOf(current) + 1];
//array
if(next.Type == TokenType.SYMBOL && next.Symbol == '[')
{
//push base address of array variable into stack
vmWriter.WritePush(symbolTable.SegmentOf(name),symbolTable.IndexOf(name));
//varName
Advance();
//'['
Advance();
//expression
CompileExpression();
//']'
Advance();
//base+offset
vmWriter.WriteArithmetic(Command.ADD);
//pop into 'that' pointer
vmWriter.WritePop(Segment.POINTER,1);
//push *(base+index) onto stack
vmWriter.WritePush(Segment.THAT,0);
}
//subroutineCall
else if(next.Type == TokenType.SYMBOL && (next.Symbol == '(' || next.Symbol == '.'))
{
CompileSubroutineCall();
}
//varName
else
{
vmWriter.WritePush(symbolTable.SegmentOf(name), symbolTable.IndexOf(name));
Advance();
}
}
}
/// <summary>
/// Compiles a (possibly empty) comma-separated list of expressions.
/// </summary>
int CompileExpressionList()
{
int nArgs = 0;
if(current.Type == TokenType.SYMBOL && current.Symbol == ')')
return nArgs;
//expression
nArgs++;
CompileExpression();
//(',' expression)*
while(current.Type == TokenType.SYMBOL && current.Symbol == ',')
{
nArgs++;
Advance();
CompileExpression();
}
return nArgs;
}
void CompileSubroutineCall()
{
int nArgs = 0;
string name;
//look ahead
Token next = tokens[tokens.IndexOf(current) + 1];
//(className|varName).subroutineName'('expressionList')'
if(next.Type == TokenType.SYMBOL && next.Symbol == '.')
{
name = current.Identifier;
Advance();
Advance();
//subroutineName
string subroutineName = current.Identifier;
string type = symbolTable.TypeOf(name);
if(string.IsNullOrEmpty(type))
{
name = name + "." + subroutineName;
}
else
{
nArgs = 1;
//push variable onto stack
vmWriter.WritePush(symbolTable.SegmentOf(name), symbolTable.IndexOf(name));
name = symbolTable.TypeOf(name) + "." + subroutineName;
}
Advance();
Advance();
nArgs += CompileExpressionList();
Advance();
//call
vmWriter.WriteCall(name, nArgs);
}
//subroutineName'('expressionList')'
else
{
name = current.Identifier;
Advance();
//pointer
vmWriter.WritePush(Segment.POINTER, 0);
Advance();
nArgs = CompileExpressionList() + 1;
Advance();
//call
vmWriter.WriteCall(className + "." + name, nArgs);
}
}
string CompileType()
{
string type;
if(current.Type == TokenType.KEYWORD)
{
type = Enum.GetName(typeof(Keyword), current.Keyword).ToLower();
Advance();
}
else
{
type = current.Identifier;
Advance();
}
return type;
}
void Advance()
{
if(tokens.IndexOf(current) + 1 < tokens.Count)
current = tokens[tokens.IndexOf(current) + 1];
}
bool IsOp(char symbol)
{
if(symbol == '+' || symbol == '-' || symbol == '*' || symbol == '/' || symbol == '&' || symbol == '|' ||
symbol == '<' || symbol == '>' || symbol == '=')
return true;
return false;
}
bool IsUnaryOp(char symbol)
{
if(symbol == '-' || symbol == '~')
return true;
return false;
}
string NewLabel(string name)
{
string label = name + labelIndex;
labelIndex++;
return label;
}
}
}