-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhjcCompile-2.py
More file actions
871 lines (683 loc) · 29.4 KB
/
hjcCompile-2.py
File metadata and controls
871 lines (683 loc) · 29.4 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
"""
hjcCompile.py -- CompileEngine class for Hack computer Jack compiler
Solution provided by Nand2Tetris authors, licensed for educational purposes
Refactored and skeleton-ized by Janet Davis, April 18, 2016
Refactored by John Stratton, April 2019
"""
from hjcTokens import *
from hjcTokenizer import *
from hjcOutputFile import *
from hjcSymbolTable import *
from hjcVmWriter import *
xml = True # Enable _WriteXml...() functions
class CompileEngine(object):
def __init__(self, inputFileName, outputFileName, xmlOutputFileName, source=True):
"""
Initializes the compilation of 'inputFileName' to 'outputFileName'.
If 'source' is True, source code will be included as comments in the
output.
"""
self.className = None
self.inputFileName = inputFileName
self.source = source
self.xmlIndent = 0
self.labelCounters = {}
self.xmlOutputFile = OutputFile(xmlOutputFileName)
self.vmWriter = VmWriter(outputFileName)
self.symbolTable = SymbolTable()
self.tokenizer = Tokenizer(inputFileName, self.xmlOutputFile, source)
if not self.tokenizer.Advance():
self._RaiseError('Premature EOF')
def Close(self):
"""
Finalize the compilation and close the output file.
"""
self.xmlOutputFile.Close()
def _GetNextLabel(self, label_prefix):
"""
Gets a label that is unique within this subroutine.
"""
if label_prefix not in self.labelCounters:
self.labelCounters[label_prefix] = -1
self.labelCounters[label_prefix] += 1
return label_prefix + str(self.labelCounters[label_prefix])
def CompileClass(self):
"""
Compiles <class> :=
'class' <class-name> '{' <class-var-dec>* <subroutine-dec>* '}'
ENTRY: Tokenizer positioned on the initial keyword.
EXIT: Tokenizer positioned after final '}'
"""
self._WriteXmlTag('<class>\n')
self._ExpectKeyword(KW_CLASS)
self._NextToken()
self.className = self._ExpectIdentifier()
self._NextToken()
self._ExpectSymbol('{')
self._NextToken()
while True:
if not self._MatchKeyword((KW_STATIC, KW_FIELD)):
break
self._CompileClassVarDec();
while True:
if not self._MatchKeyword((KW_CONSTRUCTOR, KW_FUNCTION, KW_METHOD)):
break
self._CompileSubroutine();
self._ExpectSymbol('}')
self._WriteTokenXML()
self._WriteXmlTag('</class>\n')
if self.tokenizer.Advance():
self._RaiseError('Junk after end of class definition')
def _CompileClassVarDec(self):
"""
Compiles <class-var-dec> :=
('static' | 'field') <type> <var-name> (',' <var-name>)* ';'
ENTRY: Tokenizer positioned on the initial keyword.
EXIT: Tokenizer positioned after final ';'.
"""
self._WriteXmlTag('<classVarDec>\n')
storageClass = self._ExpectKeyword((KW_STATIC, KW_FIELD))
self._NextToken()
if self.tokenizer.TokenType() == TK_KEYWORD:
variableType = self._ExpectKeyword((KW_INT, KW_CHAR, KW_BOOLEAN))
else:
variableType = self._ExpectIdentifier()
self._NextToken()
while True:
variableName = self._ExpectIdentifier()
self._NextToken()
if storageClass == KW_STATIC:
self._DefineSymbol(variableName, variableType, SYMK_STATIC)
else:
self._DefineSymbol(variableName, variableType, SYMK_FIELD)
if not self._MatchSymbol(','):
break
self._NextToken()
self._ExpectSymbol(';')
self._NextToken()
self._WriteXmlTag('</classVarDec>\n')
def _CompileSubroutine(self):
"""
Compiles <subroutine-dec> :=
('constructor' | 'function' | 'method') ('void' | <type>)
<subroutine-name> '(' <parameter-list> ')' <subroutine-body>
ENTRY: Tokenizer positioned on the initial keyword.
EXIT: Tokenizer positioned after <subroutine-body>.
"""
self._WriteXmlTag('<subroutineDec>\n')
self.symbolTable.StartSubroutine()
self.labelCounters = {}
subroutineType = self._ExpectKeyword((KW_CONSTRUCTOR, KW_FUNCTION, KW_METHOD))
self._NextToken()
#TODO-11C: For a method in particular, we need to declare an implicit first
# argument in the symbol table before we add the others
if self.tokenizer.TokenType() == TK_KEYWORD:
returnType = self._ExpectKeyword((KW_INT, KW_CHAR, KW_BOOLEAN, KW_VOID))
else:
returnType = self._ExpectIdentifier()
self._NextToken()
subroutineName = self._ExpectIdentifier()
self._NextToken()
self._ExpectSymbol('(')
self._NextToken()
self._CompileParameterList()
self._ExpectSymbol(')')
self._NextToken()
self._CompileSubroutineBody(subroutineType, subroutineName)
self._WriteXmlTag('</subroutineDec>\n')
def _CompileParameterList(self):
"""
Compiles <parameter-list> :=
( <type> <var-name> (',' <type> <var-name>)* )?
ENTRY: Tokenizer positioned on the initial keyword.
EXIT: Tokenizer positioned after <subroutine-body>.
"""
self._WriteXmlTag('<parameterList>\n')
while True:
if self._MatchSymbol(')'):
break;
if self.tokenizer.TokenType() == TK_KEYWORD:
parameterType = self._ExpectKeyword((KW_INT, KW_CHAR, KW_BOOLEAN))
else:
parameterType = self._ExpectIdentifier()
self._NextToken()
parameterName = self._ExpectIdentifier();
self._NextToken();
#TODO-11B: Define this parameter in the symbol table.
#Uncomment the next two lines and fill in a value for parameterKind.
#parameterKind =
#self._DefineSymbol(parameterName, parameterType, parameterKind)
if not self._MatchSymbol(','):
break
self._NextToken()
# Write close tag
self._WriteXmlTag('</parameterList>\n')
def _CompileSubroutineBody(self, subroutineType, subroutineName):
"""
Compiles <subroutine-body> :=
'{' <var-dec>* <statements> '}'
The tokenizer is expected to be positioned before the {
ENTRY: Tokenizer positioned on the initial '{'.
EXIT: Tokenizer positioned after final '}'.
"""
self._WriteXmlTag('<subroutineBody>\n')
self._ExpectSymbol('{')
self._NextToken()
while self._MatchKeyword(KW_VAR):
self._CompileVarDec()
#TODO-11A: Write the VM function command.
#The function name is given as a parameter above; you will also need self.className.
if subroutineType == KW_CONSTRUCTOR:
#In a constructor, the first operations must allocate memory for
# the current object.
self.vmWriter.WritePush(SEG_CONST, self.symbolTable.VarCount(SYMK_FIELD))
self.vmWriter.WriteCall("Memory.alloc", 1)
self.vmWriter.WritePop(SEG_POINTER, 0)
elif subroutineType == KW_METHOD:
#TODO-11C: For a method, we need to move the current object pointer
# from its spot as the first argument to the THIS
# pointer location
pass
self._CompileStatements()
self._ExpectSymbol('}')
self._NextToken()
self._WriteXmlTag('</subroutineBody>\n')
def _CompileVarDec(self):
"""
Compiles <var-dec> :=
'var' <type> <var-name> (',' <var-name>)* ';'
ENTRY: Tokenizer positioned on the initial 'var'.
EXIT: Tokenizer positioned after final ';'.
"""
self._WriteXmlTag('<varDec>\n')
storageClass = self._ExpectKeyword(KW_VAR)
self._NextToken()
if self.tokenizer.TokenType() == TK_KEYWORD:
variableType = self._ExpectKeyword((KW_INT, KW_CHAR, KW_BOOLEAN))
else:
variableType = self._ExpectIdentifier()
self._NextToken()
while True:
variableName = self._ExpectIdentifier()
self._NextToken()
#TODO-11B: Define the declared variable in the symbol table
if not self._MatchSymbol(','):
break
self._NextToken()
self._ExpectSymbol(';')
self._NextToken()
self._WriteXmlTag('</varDec>\n')
def _CompileStatements(self):
"""
Compiles <statements> := (<let-statement> | <if-statement> |
<while-statement> | <do-statement> | <return-statement>)*
The tokenizer is expected to be positioned on the first statement
ENTRY: Tokenizer positioned on the first statement.
EXIT: Tokenizer positioned after final statement.
"""
self._WriteXmlTag('<statements>\n')
kw = self._ExpectKeyword((KW_DO, KW_IF, KW_LET, KW_RETURN, KW_WHILE))
while kw:
if kw == KW_DO:
self._CompileDo()
elif kw == KW_IF:
self._CompileIf()
elif kw == KW_LET:
self._CompileLet()
elif kw == KW_RETURN:
self._CompileReturn()
elif kw == KW_WHILE:
self._CompileWhile()
kw = self._MatchKeyword((KW_DO, KW_IF, KW_LET, KW_RETURN, KW_WHILE))
self._WriteXmlTag('</statements>\n')
def _CompileLet(self):
"""
Compiles <let-statement> :=
'let' <var-name> ('[' <expression> ']')? '=' <expression> ';'
ENTRY: Tokenizer positioned on the first keyword.
EXIT: Tokenizer positioned after final ';'.
"""
self._WriteXmlTag('<letStatement>\n')
# TODO-10A: Delete the line below to unskip the statement
# and add code to compile a let statement
# without array indexing.
self._ExpectKeyword(KW_LET)
self._NextToken()
ident = self._ExpectIdentifier()
self._NextToken()
if self._MatchSymbol('[') == '[':
self._NextToken()
self._CompileExpression()
self._NextToken()
self._ExpectSymbol('=')
self._NextToken()
self._CompileExpression()
self._ExpectSymbol(';')
self._NextToken()
# TODO-10F: Account for array indexing.
#self._SkipStatement(';')
# TODO-11B: Add code to generate VM operations to perform a let statement.
'''
self._ExpectKeyword(KW_LET)
self._NextToken()
ident = self._ExpectIdentifier()
self._NextToken()
'''
#HINT: You will find this snippet of code helpful to handle assigning to arrays
#
#isArrayAssign = False
#if self._MatchSymbol('['):
# isArrayAssign = True
#TODO-11D: After computing the index, add that index to the base
# pointer. Leave the result on the stack for now.
#TODO-11B: After the expression is compiled, write a pop to assign
# the computed expression to given variable
#TODO-11D: Write VM commands to pop value into desired array location.
# The top value of the stack should be the value, and the value
# underneath it should be a pointer to the location the value should
# be stored.
self._WriteXmlTag('</letStatement>\n')
def _CompileDo(self):
"""
Compiles <do-statement> := 'do' <subroutine-call> ';'
<subroutine-call> := (<subroutine-name> '(' <expression-list> ')') |
((<class-name> | <var-name>) '.' <subroutine-name> '('
<expression-list> ')')
<*-name> := <identifier>
ENTRY: Tokenizer positioned on the first keyword.
EXIT: Tokenizer positioned after final ';'.
"""
self._WriteXmlTag('<doStatement>\n')
self._ExpectKeyword(KW_DO)
self._NextToken()
self._CompileCall()
#TODO-11A: Pop return value from void function into the temp segment
self._ExpectSymbol(';')
self._NextToken()
self._WriteXmlTag('</doStatement>\n')
def _CompileCall(self, firstIdentifier=None):
"""
<subroutine-call> := (<subroutine-name> '(' <expression-list> ')') |
((<class-name> | <var-name>) '.' <subroutine-name> '('
<expression-list> ')')
<*-name> := <identifier>
ENTRY: Tokenizer positioned on the first identifier.
If 'firstIdentifier' is supplied, tokenizer is on the '.'
EXIT: Tokenizer positioned after final ';'.
"""
scopeName = None
if firstIdentifier == None:
firstIdentifier = self._ExpectIdentifier()
self._NextToken()
sym = self._ExpectSymbol('.(')
self._NextToken()
if sym == '.':
scopeName = firstIdentifier
subroutineName = self._ExpectIdentifier()
self._NextToken()
sym = self._ExpectSymbol('(')
self._NextToken()
else:
subroutineName = firstIdentifier
argcount = 0
callClass = scopeName
if scopeName != None:
#TODO-11C: Set callClass based on the scopeName, whether it is an
# object name or class name. If object, push it for the first
# implicit argument of the method, as the new "current object"
pass
else:
#TODO-11C: we are implicitly calling a method on the current
# object, so push the current object on the stack as the first argument
pass
argcount += self._CompileExpressionList()
#TODO-11A: Write the call to the function using scopeName and functionName
self._ExpectSymbol(')')
self._NextToken()
def _CompileReturn(self):
"""
Compiles <return-statement> :=
'return' <expression>? ';'
ENTRY: Tokenizer positioned on the first keyword.
EXIT: Tokenizer positioned after final ';'.
"""
self._WriteXmlTag('<returnStatement>\n')
# TODO-10A: Replace the following line with code to compile a return statement.
self._ExpectKeyword(KW_RETURN)
self._NextToken()
# TODO-10B: Account for return values.
if self._MatchSymbol(';') != ';':
self._CompileExpression()
self._NextToken()
# TODO-11A: In the case that no return expression was given, write the VM
# command to return value 0 for void functions
# TODO-11A: Write VM return command
self._WriteXmlTag('</returnStatement>\n')
def _CompileIf(self):
"""
Compiles <if-statement> :=
'if' '(' <expression> ')' '{' <statements> '}' ( 'else'
'{' <statements> '}' )?
ENTRY: Tokenizer positioned on the first keyword.
EXIT: Tokenizer positioned after final '}'.
"""
self._WriteXmlTag('<ifStatement>\n')
self._ExpectKeyword(KW_IF)
self._NextToken()
self._ExpectSymbol('(')
self._NextToken()
self._CompileExpression()
truecase = self._GetNextLabel("IF_TRUE")
falsecase = self._GetNextLabel("IF_FALSE")
endif = self._GetNextLabel("IF_END")
self.vmWriter.WriteIf(truecase)
self.vmWriter.WriteGoto(falsecase)
self.vmWriter.WriteLabel(truecase)
self._ExpectSymbol(')')
self._NextToken()
self._ExpectSymbol('{')
self._NextToken()
self._CompileStatements()
self._ExpectSymbol('}')
self._NextToken()
if self._MatchKeyword(KW_ELSE):
self.vmWriter.WriteGoto(endif)
self.vmWriter.WriteLabel(falsecase)
self._NextToken()
self._ExpectSymbol('{')
self._NextToken()
self._CompileStatements()
self._ExpectSymbol('}')
self._NextToken()
self.vmWriter.WriteLabel(endif)
else:
self.vmWriter.WriteLabel(falsecase)
self._WriteXmlTag('</ifStatement>\n')
def _CompileWhile(self):
"""
Compiles <while-statement> :=
'while' '(' <expression> ')' '{' <statements> '}'
ENTRY: Tokenizer positioned on the first keyword
EXIT: Tokenizer positioned after final '}'.
"""
self._WriteXmlTag('<whileStatement>\n')
condition = self._GetNextLabel("WHILE_EXP")
end = self._GetNextLabel("WHILE_END")
# TODO-10C: Replace the following line with code to compile a while statement.
self._ExpectKeyword(KW_WHILE)
self._NextToken()
self._ExpectSymbol('(')
self._NextToken()
self._CompileExpression()
self._ExpectSymbol(')')
self._NextToken()
self._ExpectSymbol('{')
self._NextToken()
self._CompileStatements()
self._ExpectSymbol('}')
self._NextToken()
# TODO-11B: Extend the function to emit VM code for a while statement.
self._WriteXmlTag('</whileStatement>\n')
def _CompileExpression(self):
"""
Compiles <expression> :=
<term> (op <term)*
The tokenizer is expected to be positioned on the expression.
ENTRY: Tokenizer positioned on the expression.
EXIT: Tokenizer positioned after the expression.
"""
self._WriteXmlTag('<expression>\n')
# TODO-10E: Extend the following code.
self._CompileTerm()
if self._MatchSymbol("+-*/|<>=&"):
self._NextToken()
self._CompileTerm()
#In project 11, you may find this dictionary of VM opcodes helpful
vm_opcodes = {'+':OP_ADD, '-':OP_SUB, '|':OP_OR, "<":OP_LT, ">":OP_GT, "=":OP_EQ, "&":OP_AND}
# TODO-11A: Write the operation for the VM. Keep in mind that
# multiply and divide need to be handled by calls to the
# standard library, as the VM does not have those operators
# built-in
self._WriteXmlTag('</expression>\n')
def _EmitStringConstantVMCode(self, stringConstant):
"""Emits VM code for the given string constant"""
self.vmWriter.WritePush(SEG_CONST, len(stringConstant))
self.vmWriter.WriteCall("String.new", 1)
for char in stringConstant:
self.vmWriter.WritePush(SEG_CONST, ord(char))
self.vmWriter.WriteCall("String.appendChar", 2)
return
def _CompileTerm(self):
"""
Compiles a <term> :=
<int-const> | <string-const> | <keyword-const> | <var-name> |
(<var-name> '[' <expression> ']') | <subroutine-call> |
( '(' <expression> ')' ) | (<unary-op> <term>)
ENTRY: Tokenizer positioned on the term.
EXIT: Tokenizer positioned after the term.
"""
self._WriteXmlTag('<term>\n')
# TODO-10D: Extend the following to account for subroutine calls.
# TODO-10F: Extend the following to account for array indexing.
# TODO-10E: Extend the following to account for all other terms.
if self._MatchIntConstant() is not None:
self._NextToken()
elif self._MatchStringConstant() is not None:
self._NextToken()
elif self._MatchKeyword((KW_THIS,KW_NULL,KW_FALSE,KW_TRUE)):
self._NextToken()
elif self._MatchSymbol('(') == "(":
self._NextToken()
self._CompileExpression()
self._NextToken()
elif self._MatchSymbol("-~") is not None:
self._NextToken()
self._CompileTerm()
else:
self._ExpectIdentifier()
self._NextToken()
if self._MatchSymbol('[') == '[':
self._NextToken()
self._CompileExpression()
self._NextToken()
if self._MatchSymbol('.') == '.':
self._NextToken()
self._CompileCall()
# TODO-11A: Write VM commands for integer constants
# TODO-11B: Write VM commands for the unary operations,
# keyword constants, and string constants
# Note the helper function _EmitStringConstantVMCode above
#TODO 11-B: If the expression is a variable, push it onto the stack.
# Don't worry about array accesses yet.
#TODO 11-C: Perform an array access on the variable if needed. See pp 227-228.
self._WriteXmlTag('</term>\n')
def _CompileExpressionList(self):
"""
Compiles <expression-list> :=
(<expression> (',' <expression>)* )?
Returns number of expressions compiled.
ENTRY: Tokenizer positioned on the first expression.
EXIT: Tokenizer positioned after the last expression.
"""
self._WriteXmlTag('<expressionList>\n')
count = 0
while True:
if self._MatchSymbol(')'):
break
self._CompileExpression()
count += 1
if not self._MatchSymbol(','):
break
self._NextToken()
self._WriteXmlTag('</expressionList>\n')
return count
def _MatchKeyword(self, keywords):
"""
Check whether the next token matches one of 'keywords'.
'keywords' may be a keywordID or a list or tuple of keywordIDs.
If one of the keywords is matched, returns the matched keyword.
If next token is not a keyword matching one of the requested keywords, returns None.
"""
if type(keywords) == list:
keywords = tuple(keywords)
elif type(keywords) != tuple:
keywords = (keywords,)
if not self.tokenizer.TokenType() == TK_KEYWORD:
return None
if self.tokenizer.Keyword() in keywords:
return self.tokenizer.Keyword()
return None
def _ExpectKeyword(self, keywords):
"""
Parse the next token. It is expected to be one of 'keywords'.
'keywords' may be a keywordID or a list or tuple of keywordIDs.
If an expected keyword is parsed, returns the keyword.
Otherwise raises an error.
"""
match = self._MatchKeyword(keywords)
if not match:
self._RaiseError('Expected '+self._KeywordStr(keywords)+', got '+
self.tokenizer.TokenTypeStr())
else:
return match
def _ExpectIdentifier(self):
"""
Parse the next token. It is expected to be an identifier.
Returns the identifier parsed or raises an error.
"""
if not self.tokenizer.TokenType() == TK_IDENTIFIER:
self._RaiseError('Expected <identifier>, got '+
self.tokenizer.TokenTypeStr())
return self.tokenizer.Identifier()
def _MatchSymbol(self, symbols):
"""
Parse the next token. It is expected to be one of 'symbols'.
'symbols' is a string of one or more legal symbols.
If an expected symbol is parsed, returns the symbol.
If no such symbol is parsed, returns None.
"""
if not self.tokenizer.TokenType() == TK_SYMBOL or self.tokenizer.Symbol() not in symbols:
return None
else:
return self.tokenizer.Symbol()
def _ExpectSymbol(self, symbols):
"""
Parse the next token. It is expected to be one of 'symbols'.
'symbols' is a string of one or more legal symbols.
If an expected symbol is parsed, returns the symbol.
Otherwise raises an error.
"""
sym = self._MatchSymbol(symbols)
if not sym:
if not self.tokenizer.TokenType() == TK_SYMBOL:
self._RaiseError('Expected '+self._SymbolStr(symbols)+', got '+
self.tokenizer.TokenTypeStr())
else:
self._RaiseError('Expected '+self._SymbolStr(symbols)+', got '+
self._SymbolStr(self.tokenizer.Symbol()))
return sym
def _MatchIntConstant(self):
"""
Parse the next token. It is expected to be of type TK_INT_CONST
If an expected token is matched, returns the integer constant.
If no such token is parsed, returns None.
"""
if self.tokenizer.TokenType() == TK_INT_CONST:
return self.tokenizer.IntVal()
return None
def _MatchStringConstant(self):
"""
Parse the next token. It is expected to be TK_STRING_CONST.
If the string constant if present, or None.
"""
if self.tokenizer.TokenType() == TK_STRING_CONST:
return self.tokenizer.StringVal()
return None
def _RaiseError(self, error):
message = '%s line %d:\n %s\n %s' % (
self.inputFileName, self.tokenizer.LineNumber(),
self.tokenizer.LineStr(), error)
raise HjcError(message)
def _KeywordStr(self, keywords):
if type(keywords) != tuple:
return '"' + self.tokenizer.KeywordStr(keywords) + '"'
ret = ''
for kw in keywords:
if len(ret):
ret += ', '
ret += '"' + self.tokenizer.KeywordStr(kw) + '"'
if len(keywords) > 1:
ret = 'one of (' + ret + ')'
return ret
def _SymbolStr(self, symbols):
if type(symbols) != tuple:
return '"' + symbols + '"'
ret = ''
for symbol in symbols:
if len(ret):
ret += ', '
ret += '"' + symbol + '"'
if len(symbols) > 1:
ret = 'one of (' + ret + ')'
return ret
def _NextToken(self, emitXML=True):
if emitXML:
self._WriteTokenXML()
if not self.tokenizer.Advance():
self._RaiseError('Premature EOF')
def _ConsumeToken(self):
if not self.tokenizer.Advance():
self._RaiseError('Premature EOF')
def _WriteXmlTag(self, tag):
if xml:
if tag[1] == '/':
self.xmlIndent -= 1
self.xmlOutputFile.Write(' ' * self.xmlIndent)
self.xmlOutputFile.Write(tag)
if '/' not in tag:
self.xmlIndent += 1
def _WriteXml(self, tag, value):
if xml:
self.xmlOutputFile.Write(' ' * self.xmlIndent)
self.xmlOutputFile.WriteXml(tag, value)
def _SkipStatement(self, end_symbol):
"""
Consumes tokens unparsed until the specified end symbol is seen.
Used as a placeholder for actually parsing the statements.
"""
while not self._ExpectSymbol(end_symbol):
self._NextToken();
self._NextToken();
def _WriteTokenXML(self):
"""
Writes out whatever the next token is to the output file, regardless of type or content.
"""
tokenType = self.tokenizer.TokenType()
if tokenType == TK_SYMBOL:
self._WriteXml('symbol', self.tokenizer.Symbol())
elif tokenType == TK_KEYWORD:
self._WriteXml('keyword', self.tokenizer.KeywordStr())
elif tokenType == TK_IDENTIFIER:
self._WriteXml('identifier', self.tokenizer.Identifier())
elif tokenType == TK_INT_CONST:
self._WriteXml('integerConstant', str(self.tokenizer.IntVal()))
elif tokenType == TK_STRING_CONST:
self._WriteXml('stringConstant', self.tokenizer.StringVal())
def _DefineSymbol(self, name, type, kind):
"""
Adds a new symbol to the symbol table.
"""
self.symbolTable.Define(name, type, kind)
def _KindToSegment(self, kind):
"""
Converts symbol table "kinds" to segment numbers for the VmWriter.
"""
return (SEG_STATIC, SEG_THIS, SEG_ARG, SEG_LOCAL)[kind]
def _WriteSymbolTableEntry(self, name, assign=False):
"""
Writes an XML tag for a symbol table entry.
"""
type = self.symbolTable.TypeOf(name)
kind = self.symbolTable.KindOfStr(name)
index = self.symbolTable.IndexOf(name)
self._WriteXmlTag('<tableEntry type="%s" kind="%s" index="%s" assign="%s">\n' % (type, kind, index, assign))