forked from lakshmi-warrier/EoC_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilationEngine.java
More file actions
1042 lines (886 loc) · 32 KB
/
CompilationEngine.java
File metadata and controls
1042 lines (886 loc) · 32 KB
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.*;
public class CompilationEngine
{
static VMWriter vmWriter;
static JackTokenizer tokenizer;
static SymbolTable symbolTable;
static String Class;
static String Subroutine;
static int LabelCount;
public CompilationEngine(File inFile, File outFile)
{
tokenizer = new JackTokenizer(inFile);
vmWriter = new VMWriter(outFile);
symbolTable = new SymbolTable();
LabelCount = 0;
}
static String Function()
{
if (Class.length() != 0 && Subroutine.length() !=0)
{
return Class + "." + Subroutine;
}
return "";
}
static String CompilerParse()
{
JackTokenizer.Advance();
// check if the line is keyword
// Also, if the keyword is int|char|boolean
// if yes, the corresponding token is returned
if (JackTokenizer.token() == JackTokenizer.TYPE.KEYWORD && (JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.INT || JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.CHAR || JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.BOOLEAN))
{
return JackTokenizer.CurrentToken();
}
// checks if the token is an identifier or not
// if yes, the corresponding token is returned
if (JackTokenizer.token() == JackTokenizer.TYPE.IDENTIFIER)
{
return JackTokenizer.Identifier();
}
// if all the above is failed, an error is being showed.
error("in|char|boolean|className");
return "";
}
public void ClassCompiler()
{
//checks if the code starts with class
JackTokenizer.Advance();
// if it dosen't starts with class, it will goes to the method error to show where the error has happened
if (JackTokenizer.token() != JackTokenizer.TYPE.KEYWORD || JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.CLASS)
{
error("class"); // goes to the method error
}
//checks the className
JackTokenizer.Advance();
// checks if the class name is an identifier
// if not an identifier it shows an error
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("className");
}
//classname does not need to be put in symbol table
// checks if the current token type is identifier or not
// if it is a identifier it is being stored in the string class
Class = JackTokenizer.Identifier();
// checks if the next line starts with {
Symbol('{');
//classVarDec* subroutineDec*
CompilerClassVarDec();
CompileSubroutine();
//checks if it has closed bracker '}'
Symbol('}');
// checks if the code have more token or not
if (JackTokenizer.HasMoreTokens())
{
throw new IllegalStateException("Unexpected tokens");
}
//saves the file
VMWriter.close();
}
static void CompilerClassVarDec()
{
//first determine whether there is a classVarDec, nextToken is } or start subroutineDec
JackTokenizer.Advance();
//checks if the next line is a }
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '}')
{
JackTokenizer.Pointer();
return;
}
//next is start subroutine or classVariable, both start with keyword
// checks if it is keyword or not
// if not a keyword, it shows an error
if (JackTokenizer.token() != JackTokenizer.TYPE.KEYWORD)
{
error("Keywords");
}
//next is subroutine
// checks if the keywords are contructor| function| Method
// if yes, it goes to the method pointer
if (JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.CONSTRUCTOR || JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.FUNCTION || JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.METHOD)
{
JackTokenizer.Pointer();
return;
}
//classVariable
// checks if the keyword is static or field
// if not, it shows the error
if (JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.STATIC && JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.FIELD)
{
error("static or field");
}
Symbol.KIND kind = null;
String type = "";
String name = "";
// for class variable
// checks if it is static or field and store it as "kind"
switch (JackTokenizer.KeyWords())
{
case STATIC:
{
kind = Symbol.KIND.STATIC;
break;
}
case FIELD:
{
kind = Symbol.KIND.FIELD;
break;
}
}
// gets the type of keyword
// if it is int|boolean|char|identifier
// it is being stored as
type = CompilerParse();
//at least one varName
while(true)
{
//varName
JackTokenizer.Advance();
// checks if the variable name is an identifier or not
// if not a identifier, an error is shown
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("identifier");
}
// the identifier is stored as name
name = JackTokenizer.Identifier();
// the symbol,type of varibale and identifiers are added to symbol table
SymbolTable.define(name,type,kind);
//checks for (, or ;)
JackTokenizer.Advance();
// if the symbol is not , or ;
// it shows an error
if (JackTokenizer.token() != JackTokenizer.TYPE.SYMBOL || (JackTokenizer.Symbol() != ',' && JackTokenizer.Symbol() != ';'))
{
error("',' or ';'");
}
// if the symbol is ; the loop gets breaks
if (JackTokenizer.Symbol() == ';')
{
break;
}
}
// the method is being called unless ';' is being found
CompilerClassVarDec();
}
static void CompileSubroutine()
{
//checks if the line has '}'
JackTokenizer.Advance();
//next is a '}', then it returns
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '}')
{
JackTokenizer.Pointer();
return;
}
//checks if the token start with keyword
//also, if that starts with function|constructor|method
// if not, prints the error
if (JackTokenizer.token() != JackTokenizer.TYPE.KEYWORD || (JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.CONSTRUCTOR && JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.FUNCTION && JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.METHOD))
{
error("constructor|function|method");
}
JackTokenizer.KEYWORD keyword = JackTokenizer.KeyWords();
// add to symbol table
SymbolTable.startSubroutine();
//for method this is the first argument
// if the token is method, then it gets added to the hashmap subroutineSymbols in the class SymbolTable
if (JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.METHOD)
{
SymbolTable.define("this",Class, Symbol.KIND.ARG); // add to symboltable
}
String type = "";
//'void' or type
JackTokenizer.Advance();
// checks if the token is there a keyword or not
// checks if the keyword is void or not
if (JackTokenizer.token() == JackTokenizer.TYPE.KEYWORD && JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.VOID)
{
// if the token is void, string type is being stored as void
type = "void";
}
else
{
// if not, it will check if the token is int|char|boolean and store it as type
JackTokenizer.Pointer();
type = CompilerParse();
}
//subroutineName which is a identifier
JackTokenizer.Advance();
// checks if the subroutine name is an identifier or not
//if not, an error will be displayed
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("subroutineName");
}
// the identifier is being stored as string subroutine
Subroutine = JackTokenizer.Identifier();
//checks if there is the opening bracket '('
Symbol('(');
//parameterList
ParameterList();
//checks if there is an closing bracket ')'
Symbol(')');
//SUBROUTINE BODY
// goes to the method compileSubroutineMain()
compileSubroutineMain(keyword);
//goes to method compileSubroutine()
CompileSubroutine();
}
static void compileSubroutineMain(JackTokenizer.KEYWORD keyword)
{
// checks if there is the opening bracket '{'
Symbol('{');
//goes to method varDec
VarDec();
//write VM function declaration
writeFunctionDec(keyword);
//statements
StatementCompile();
//checks if it has the symbol '}'
Symbol('}');
}
static void writeFunctionDec(JackTokenizer.KEYWORD keyword)
{
VMWriter.writeFunction(Function(),SymbolTable.varCount(Symbol.KIND.VAR));
//METHOD and CONSTRUCTOR need to load this pointer
if (keyword == JackTokenizer.KEYWORD.METHOD)
{
//A Jack method with k arguments is compiled into a VM function that operates on k + 1 arguments.
// The first argument (argument number 0) always refers to the this object.
VMWriter.writePush(VMWriter.SEGMENT.ARG, 0);
VMWriter.writePop(VMWriter.SEGMENT.POINTER,0);
}
else if (keyword == JackTokenizer.KEYWORD.CONSTRUCTOR)
{
//A Jack function or constructor with k arguments is compiled into a VM function that operates on k arguments.
VMWriter.writePush(VMWriter.SEGMENT.CONST,SymbolTable.varCount(Symbol.KIND.FIELD));
VMWriter.writeCall("Memory.alloc", 1);
VMWriter.writePop(VMWriter.SEGMENT.POINTER,0);
}
}
static void StatementCompile()
{
//determine whether there is a statement next can be a '}'
JackTokenizer.Advance();
//next is a '}'
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '}')
{
JackTokenizer.Pointer();
return;
}
//next is 'let'|'if'|'while'|'do'|'return'
// if not an error is shown
if (JackTokenizer.token() != JackTokenizer.TYPE.KEYWORD)
{
error("keyword");
}
else
{
switch (JackTokenizer.KeyWords())
{
// goes to the corresponding methos and the corresponding vm code is generated and written to the vm file
case LET:
{
Let();
break;
}
case IF:
{
If();
break;
}
case WHILE:
{
While();
break;
}
case DO:
{
Do();
break;
}
case RETURN:
{
Return();
break;
}
default:
{
// if the above keywords are not found, an error will be shown
error("'let'|'if'|'while'|'do'|'return'");
}
}
}
StatementCompile();
}
static void ParameterList()
{
//check if there is parameterList, if next token is ')' than go back
JackTokenizer.Advance();
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == ')')
{
JackTokenizer.Pointer();
return;
}
String type = "";
//there is parameter, at least one varName
JackTokenizer.Pointer();
while(true)
{
//checks if it is int|char|boolean and stored as type
type = CompilerParse();
//varName
JackTokenizer.Advance();
//checks if the token is an identifier or not
//if not, an error will be displayed
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("identifier");
}
// it being added to the hashtable subroutine symbols
SymbolTable.define(JackTokenizer.Identifier(),type, Symbol.KIND.ARG);
//checks if the symbol is ',' or ')' is present or not
// if not present, an error is being shown
JackTokenizer.Advance();
if (JackTokenizer.token() != JackTokenizer.TYPE.SYMBOL || (JackTokenizer.Symbol() != ',' && JackTokenizer.Symbol() != ')'))
{
error("',' or ')'");
}
// if the symbol is ')' , it goes back
if (JackTokenizer.Symbol() == ')')
{
JackTokenizer.Pointer();
break;
}
}
}
static void VarDec()
{
//determine if there is a varDec
JackTokenizer.Advance();
//no 'var' go back
if (JackTokenizer.token() != JackTokenizer.TYPE.KEYWORD || JackTokenizer.KeyWords() != JackTokenizer.KEYWORD.VAR)
{
JackTokenizer.Pointer();
return;
}
//checks if it is int|char|boolean and stored as type
String type = CompilerParse();
while(true)
{
//varName
JackTokenizer.Advance();
// if the varname is not an identifier an error is being shown
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("identifier");
}
// the varname is being added to symboltable subroutine symbols
SymbolTable.define(JackTokenizer.Identifier(),type, Symbol.KIND.VAR);
//checks if the symbols are ',' or ';'
// if not an error will be shown
JackTokenizer.Advance();
if (JackTokenizer.token() != JackTokenizer.TYPE.SYMBOL || (JackTokenizer.Symbol() != ',' && JackTokenizer.Symbol() != ';'))
{
error("',' or ';'");
}
// if the symbol is ';' the loop breaks
if (JackTokenizer.Symbol() == ';')
{
break;
}
}
// the method is being called unless ';' is being found
VarDec();
}
static void Do()
{
//subroutineCall
SubroutineCall();
//checks if the ';' is present
//if not present the corresponding error is shown
Symbol(';');
//pop return value
VMWriter.writePop(VMWriter.SEGMENT.TEMP,0);
}
static void Let()
{
//gets the varName
JackTokenizer.Advance();
//checks if the token is an identifier or not
//if not the error in the variable name will be shown
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("varName");
}
// the current token is saved as varname
String varName = JackTokenizer.Identifier();
//checks the next token
JackTokenizer.Advance();
// checks if the token is a symbol or not
// if not a symbol, an error will be shown
if (JackTokenizer.token() != JackTokenizer.TYPE.SYMBOL || (JackTokenizer.Symbol() != '[' && JackTokenizer.Symbol() != '='))
{
error("'['|'='");
}
boolean expExist = false;
//'[' expression ']' ,need to deal with array [base+offset]
if (JackTokenizer.Symbol() == '[')
{
expExist = true;
//push array variable,base address into stack
VMWriter.writePush(segment(SymbolTable.kindOf(varName)),SymbolTable.indexOf(varName));
//calc offset
Expression();
//']'
Symbol(']');
//base+offset
VMWriter.writeArithmetic(VMWriter.COMMAND.ADD);
}
if (expExist)
{
JackTokenizer.Advance();
}
//expression
Expression();
//checks if the token is ';'
Symbol(';');
if (expExist)
{
//*(base+offset) = expression
//pop expression value to temp
VMWriter.writePop(VMWriter.SEGMENT.TEMP,0);
//pop base+index into 'that'
VMWriter.writePop(VMWriter.SEGMENT.POINTER,1);
//pop expression value into *(base+index)
VMWriter.writePush(VMWriter.SEGMENT.TEMP,0);
VMWriter.writePop(VMWriter.SEGMENT.THAT,0);
}
else
{
//pop expression value directly
VMWriter.writePop(segment(SymbolTable.kindOf(varName)), SymbolTable.indexOf(varName));
}
}
static VMWriter.SEGMENT segment(Symbol.KIND kind)
{
// the corresponding vm code is being written to the vm file
switch (kind)
{
case FIELD:
{
return VMWriter.SEGMENT.THIS;
}
case STATIC:
{
return VMWriter.SEGMENT.STATIC;
}
case VAR:
{
return VMWriter.SEGMENT.LOCAL;
}
case ARG:
{
return VMWriter.SEGMENT.ARG;
}
default:
{
return VMWriter.SEGMENT.NONE;
}
}
}
static void While()
{
String continueLabel = Label();
String topLabel = Label();
//top label for while loop
VMWriter.writeLabel(topLabel);
//checks if the opening bracket is present or not
Symbol('(');
//expression while condition: true or false
Expression();
//checks if the closing bracket is present or not
Symbol(')');
//if ~(condition) go to continue label
VMWriter.writeArithmetic(VMWriter.COMMAND.NOT);
VMWriter.writeIf(continueLabel);
//checks if the opening bracket is present or not
Symbol('{');
//statements
StatementCompile();
//checks if the closing bracket is present or not
Symbol('}');
//if (condition) go to top label
VMWriter.writeGoto(topLabel);
//or continue
VMWriter.writeLabel(continueLabel);
}
static String Label()
{
return "LABEL_" + (LabelCount++);
}
static void Return()
{
//check if there is any expression
JackTokenizer.Advance();
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == ';')
{
//no expression push 0 to stack
VMWriter.writePush(VMWriter.SEGMENT.CONST,0);
}
else
{
//expression exist
JackTokenizer.Pointer();
//expression
Expression();
//checks if the symbol ';' is present
Symbol(';');
}
// the vm code for return is being written to the vm file
VMWriter.writeReturn();
}
static void If()
{
String elseLabel = Label();
String endLabel = Label();
//checks if the opening bracket is present or not
Symbol('(');
//expression
Expression();
//checks if the closing bracket is present or not
Symbol(')');
//if ~(condition) go to else label
VMWriter.writeArithmetic(VMWriter.COMMAND.NOT);
VMWriter.writeIf(elseLabel);
//checks if the opening bracket is present or not
Symbol('{');
//statements
StatementCompile();
//checks if the closing bracket is present or not
Symbol('}');
//if condition after statement finishing, go to end label
VMWriter.writeGoto(endLabel);
//check if there is 'else'
VMWriter.writeLabel(elseLabel);
//gets the next token
JackTokenizer.Advance();
//checks if the token is keyword and the keyword is "else"
if (JackTokenizer.token() == JackTokenizer.TYPE.KEYWORD && JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.ELSE)
{
//checks if the opening bracket is present or not
Symbol('{');
//statements
StatementCompile();
//checks if the closing bracket is present or not
Symbol('}');
}
else
{
JackTokenizer.Pointer();
}
//vm code for label is being written onto vm file
VMWriter.writeLabel(endLabel);
}
static void Term()
{
JackTokenizer.Advance();
//check if it is an identifier
if (JackTokenizer.token() == JackTokenizer.TYPE.IDENTIFIER)
{
//varName|varName '[' expression ']'|subroutineCall
String tempId = JackTokenizer.Identifier();
JackTokenizer.Advance();
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '[')
{
//this is an array entry
//push array variable,base address into stack
VMWriter.writePush(segment(SymbolTable.kindOf(tempId)),SymbolTable.indexOf(tempId));
//expression
Expression();
//checks if the closing bracket is present or not
Symbol(']');
//base+offset
VMWriter.writeArithmetic(VMWriter.COMMAND.ADD);
//pop into 'that' pointer
VMWriter.writePop(VMWriter.SEGMENT.POINTER,1);
//push *(base+index) onto stack
VMWriter.writePush(VMWriter.SEGMENT.THAT,0);
}
else if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && (JackTokenizer.Symbol() == '(' || JackTokenizer.Symbol() == '.'))
{
//this is a subroutineCall
JackTokenizer.Pointer();
JackTokenizer.Pointer();
SubroutineCall();
}
else
{
//this is varName
JackTokenizer.Pointer();
//push variable directly onto stack
VMWriter.writePush(segment(SymbolTable.kindOf(tempId)), SymbolTable.indexOf(tempId));
}
}
else
{
//integerConstant|stringConstant|keywordConstant|'(' expression ')'|unaryOp term
if (JackTokenizer.token() == JackTokenizer.TYPE.INTEGERCONSTANT)
{
//integerConstant just push its value onto stack
VMWriter.writePush(VMWriter.SEGMENT.CONST,JackTokenizer.IntegerValue());
}
else if (JackTokenizer.token() == JackTokenizer.TYPE.STRINGCONSTANT)
{
//stringConstant new a string and append every char to the new stack
String str = JackTokenizer.StringValue();
VMWriter.writePush(VMWriter.SEGMENT.CONST,str.length());
VMWriter.writeCall("String.new",1);
for (int i = 0; i < str.length(); i++)
{
VMWriter.writePush(VMWriter.SEGMENT.CONST,(int)str.charAt(i));
VMWriter.writeCall("String.appendChar",2);
}
}
else if(JackTokenizer.token() == JackTokenizer.TYPE.KEYWORD && JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.TRUE)
{
//~0 is true
VMWriter.writePush(VMWriter.SEGMENT.CONST,0);
VMWriter.writeArithmetic(VMWriter.COMMAND.NOT);
}
else if(JackTokenizer.token() == JackTokenizer.TYPE.KEYWORD && JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.THIS)
{
//push this pointer onto stack
VMWriter.writePush(VMWriter.SEGMENT.POINTER,0);
}
else if(JackTokenizer.token() == JackTokenizer.TYPE.KEYWORD && (JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.FALSE || JackTokenizer.KeyWords() == JackTokenizer.KEYWORD.NULL))
{
//0 for false and null
VMWriter.writePush(VMWriter.SEGMENT.CONST,0);
}
else if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '(')
{
//expression
Expression();
//checks if the closing bracket is present or not
Symbol(')');
}
else if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && (JackTokenizer.Symbol() == '-' || JackTokenizer.Symbol() == '~'))
{
char s = JackTokenizer.Symbol();
//term
Term();
if (s == '-')
{
VMWriter.writeArithmetic(VMWriter.COMMAND.NEG);
}
else
{
VMWriter.writeArithmetic(VMWriter.COMMAND.NOT);
}
}
else
{
error("integerConstant|stringConstant|keywordConstant|'(' expression ')'|unaryOp term");
}
}
}
static void SubroutineCall()
{
JackTokenizer.Advance();
// checks if the token is an identifier or not
// if not, an error will be shown
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("identifier");
}
//the identifier is stored as a string name
String name = JackTokenizer.Identifier();
int nArgs = 0;
JackTokenizer.Advance();
//checks if the token is a symbol or not
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '(')
{
//push this pointer
VMWriter.writePush(VMWriter.SEGMENT.POINTER,0);
//'(' expressionList ')'
//expressionList
nArgs = ExpressionList() + 1;
//')'
Symbol(')');
//call subroutine
VMWriter.writeCall(Class + '.' + name, nArgs);
}
else if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == '.')
{
//(className|varName) '.' subroutineName '(' expressionList ')'
String objName = name;
//subroutineName
JackTokenizer.Advance();
if (JackTokenizer.token() != JackTokenizer.TYPE.IDENTIFIER)
{
error("identifier");
}
name = JackTokenizer.Identifier();
//check for if it is built-in type
String type = SymbolTable.typeOf(objName);
if (type.equals("int")||type.equals("boolean")||type.equals("char")||type.equals("void"))
{
error("no built-in type");
}
else if (type.equals(""))
{
name = objName + "." + name;
}
else
{
nArgs = 1;
//push variable directly onto stack
VMWriter.writePush(segment(SymbolTable.kindOf(objName)), SymbolTable.indexOf(objName));
name = SymbolTable.typeOf(objName) + "." + name;
}
//checks if the opening bracket is present or not
Symbol('(');
//expressionList
nArgs += ExpressionList();
//checks if the closing bracket is present or not
Symbol(')');
//call subroutine
VMWriter.writeCall(name,nArgs);
}
else // shows an error
{
error("'('|'.'");
}
}
static void Expression() // to write the vm command add, sub, gt, lt into the vm file
{
// the corresponding vm codes are being written onto the vm file of the terms
Term();
//(op term)*
while (true)
{
JackTokenizer.Advance();
//checks if the token is a symbol and an operation
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.IsOperation())
{
String opCmd = "";
switch (JackTokenizer.Symbol()) // checks the symbols
{
case '+':
{
opCmd = "add";
break;
}
case '-':
{
opCmd = "sub";
break;
}
case '*':
{
opCmd = "call Math.multiply 2";
break;
}
case '/':
{
opCmd = "call Math.divide 2";
break;
}
case '<':
{
opCmd = "lt";
break;
}
case '>':
{
opCmd = "gt";
break;
}
case '=':
{
opCmd = "eq";
break;
}
case '&':
{
opCmd = "and";
break;
}
case '|':
{
opCmd = "or";
break;
}
default:
{
error("Unknown op!");
break;
}
}
//term
Term();
//write the corresponding vm command to vm file
VMWriter.writeCommand(opCmd,"","");
}
else // breaks the loop
{
JackTokenizer.Pointer();
break;
}
}
}
static int ExpressionList()
{
int nArgs = 0;
JackTokenizer.Advance();
//determine if there is any expression, if next is ')' then no
if (JackTokenizer.token() == JackTokenizer.TYPE.SYMBOL && JackTokenizer.Symbol() == ')')
{
JackTokenizer.Pointer();
}
else
{
nArgs = 1;