-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.py
executable file
·1012 lines (766 loc) · 30.7 KB
/
parser.py
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
#!/usr/bin/python
from ply import yacc
from JSlexer import tokens, lexer, debug
from helpers import symbolTable as SymbolTable
from helpers import threeAddressCode as ThreeAddressCode
########################################
############# STATEMENTS ###############
########################################
def p_start(p):
'''start : block
| statements'''
# Any remaining breaks and continues need to be purged
TAC.noop(p[1]['loopEndList'])
TAC.noop(p[1]['loopBeginList'])
# Here we have to have statements so that we can return back to the calling function
TAC.emit('', '' , -1, 'HALT')
# Main has zero parameters
ST.addAttributeToCurrentScope('numParam', 0)
# Now delete the main scope
ST.deleteScope('main')
# Emit code
p[0] = {}
def p_block(p):
'block : SEP_OPEN_BRACE statements SEP_CLOSE_BRACE'
# Emit code
p[0] = {}
# For break statement
p[0]['loopEndList'] = p[2].get('loopEndList', [])
p[0]['loopBeginList'] = p[2].get('loopBeginList', [])
def p_block_empty(p):
'block : SEP_OPEN_BRACE empty SEP_CLOSE_BRACE'
# Emit code
p[0] = {}
# Empty blocks are not allowed, this points out this mistake
debug.printError('Empty blocks are not allowed')
raise SyntaxError
def p_statments(p):
'''statements : statement statements
| statement M_statements'''
p[0] = {}
# break statements and continue statements need to pushed up
p[0]['loopEndList'] = TAC.merge(p[1].get('loopEndList', []), p[2].get('loopEndList', []))
p[0]['loopBeginList'] = TAC.merge(p[1].get('loopBeginList', []), p[2].get('loopBeginList', []))
# The set of statements that require a semi-colon termination
def p_statment(p):
'''statement : assignment M_quad SEP_SEMICOLON
| declaration M_quad SEP_SEMICOLON
| breakStatement M_quad SEP_SEMICOLON
| continueStatement M_quad SEP_SEMICOLON
| returnStatement M_quad SEP_SEMICOLON
| printStatement M_quad SEP_SEMICOLON
| functionCall M_quad SEP_SEMICOLON'''
# the empty semicolon rules introduces a shift reduce conflict
# Emit code
p[0] = {}
# Statements waiting for the next list get backpatched
nextList = p[1].get('nextList', [])
TAC.backPatch(nextList, p[2]['quad'])
# break statements and continue statements need to pushed up
p[0]['loopEndList'] = p[1].get('loopEndList', [])
p[0]['loopBeginList'] = p[1].get('loopBeginList', [])
# The set of statements that don't require a semi-colon termination
def p_statement_no_semicolon(p):
'''statement : ifThen M_quad
| ifThenElse M_quad
| whileStatement M_quad
| functionStatement M_quad'''
# Emit code
p[0] = {}
# Statements waiting for the next list get backpatched
nextList = p[1].get('nextList', [])
TAC.backPatch(nextList, p[2]['quad'])
# break statements and continue statements need to pushed up
p[0]['loopEndList'] = p[1].get('loopEndList', [])
p[0]['loopBeginList'] = p[1].get('loopBeginList', [])
# To notify the user of a missing semicolon
def p_statement_error(p):
'''statement : assignment M_quad
| breakStatement M_quad
| returnStatement M_quad
| continueStatement M_quad
| printStatement M_quad
| functionCall M_quad'''
# Emit code
p[0] = {}
# Statements waiting for the next list get backpatched
nextList = p[1].get('nextList', [])
TAC.backPatch(nextList, p[2]['quad'])
# break statements and continue statements need to pushed up
p[0]['loopEndList'] = p[1].get('loopEndList', [])
p[0]['loopBeginList'] = p[1].get('loopBeginList', [])
# Raise an error
debug.printError('Semicolon missing')
raise SyntaxError
# Marker to mark the nextQuad value
def p_mark_quad(p):
'M_quad : empty'
p[0] = { 'quad' : TAC.getNextQuad()}
# Marker for blanck statements
def p_mark_statements(p):
'M_statements : empty'
p[0] = {}
########################################
############# DECLARATION ##############
########################################
def p_declaration(p):
'declaration : VAR decList'
# Add identifiers to local scope
for identifierName in p[2]:
# Put the identifier into the symbolTable
identifierEntry = ST.existsInCurrentScope(identifierName)
if identifierEntry == False:
ST.addIdentifier(identifierName, 'UNDEFINED')
# Create a temporary for the current scope
displayValue, offset = ST.getAttribute(identifierName, 'scopeLevel'), ST.getAttribute(identifierName, 'offset')
place = ST.newTemp((displayValue, offset), variable=identifierName)
ST.addAttribute(identifierName, ST.getCurrentScope(), place)
else:
debug.printError('Redefined Variable "%s"' %identifierName)
raise SyntaxError
debug.printStatement("Declaration '%s'" %identifierName)
# Type rules
p[0] = {}
def p_decList(p):
'decList : IDENTIFIER SEP_COMMA decList'
p[0] = [ p[1] ] + p[3]
def p_decList_base(p):
'decList : IDENTIFIER'
p[0] = [p[1]]
def p_decList_empty(p):
'decList : empty'''
p[0] = [ ]
########################################
############# ASSIGNMENT ###############
########################################
def p_assignment(p):
'assignment : VAR assignList'
# In case the var is not present
p[0] = { 'type' : 'VOID' }
# Now we add all of these statements
for identifier in p[2]:
if not ST.existsInCurrentScope(identifier['name']):
# Store information about the identifier
ST.addIdentifier(identifier['name'], identifier['type'])
# This is a new variable, so we link the temporary to our variable
displayValue, offset = ST.getAttribute(identifier['name'], 'scopeLevel'), ST.getAttribute(identifier['name'], 'offset')
ST.changeMemoryLocationOfTemp(identifier['place'], (displayValue, offset), variable=identifier['name'])
ST.addAttribute(identifier['name'], ST.getCurrentScope(), identifier['place'])
ST.addAttribute(identifier['name'], 'reference', identifier['reference'])
# print the name of the statement
debug.printStatement("ASSIGNMENT of %s" %identifier['name'])
else:
debug.printError('Redefined Variable "%s"' %identifier['name'])
raise SyntaxError
def p_assignList(p):
'assignList : IDENTIFIER OP_ASSIGNMENT expression SEP_COMMA assignList'
# Create an identifier instance to pass
identifier = {}
identifier['name'] = p[1]
identifier['type'] = p[3]['type']
identifier['place'] = p[3]['place']
identifier['reference'] = p[3].get('reference')
p[0] = [identifier] + p[5]
def p_assignList_base(p):
'assignList : IDENTIFIER OP_ASSIGNMENT expression'
identifier = {}
identifier['name'] = p[1]
identifier['type'] = p[3]['type']
identifier['place'] = p[3]['place']
identifier['reference'] = p[3].get('reference')
p[0] = [identifier]
def p_assignment_redefinition(p):
'assignment : IDENTIFIER OP_ASSIGNMENT expression'
# To store information
p[0] = {}
identifierEntry = ST.exists(p[1])
if identifierEntry == True:
ST.addAttribute(p[1], 'type', p[3]['type'])
# Check if the function is in the current scope or parent one
if ST.existsInCurrentScope(p[1]):
place = ST.getAttribute(p[1], ST.getCurrentScope())
else:
# store the address into the address descriptor
displayValue, offset = ST.getAttribute(p[1], 'scopeLevel'), ST.getAttribute(p[1], 'offset')
place = ST.newTemp((displayValue, offset), variable=p[1])
ST.addAttribute(p[1], ST.getCurrentScope(), place)
TAC.emit(place, p[3]['place'], '', '=')
else:
debug.printError('Undefined Variable "%s"' %p[1])
raise SyntaxError
# print the name of the statement
debug.printStatement("ASSIGNMENT of %s" %p[1])
########################################
############## FUNCTIONS ###############
########################################
def p_functionStatement(p):
'''functionStatement : FUNCTION IDENTIFIER M_scope SEP_OPEN_PARENTHESIS argList SEP_CLOSE_PARENTHESIS M_insertArgs block
| FUNCTION M_anonName M_scope SEP_OPEN_PARENTHESIS argList SEP_CLOSE_PARENTHESIS M_insertArgs block'''
# Any remaining breaks and continues need to be purged
TAC.noop(p[8]['loopEndList'])
TAC.noop(p[8]['loopBeginList'])
# Here we have to have statements so that we can return back to the calling function
TAC.emit('', '' , '', 'JUMPBACK')
# print the name of the statement
functionName = p[3]['reference']
ST.deleteScope(functionName)
# Type rules
p[0] = { 'type' : 'FUNCTION', 'reference': functionName }
def p_scope(p):
'M_scope : empty'
p[0] = {}
# Name the function
p[0]['reference'] = ST.nameAnon()
# Now add the identifier as a function reference
if p[-1] != None: # Check for anon function
# Check if the function exists or not
if ST.existsInCurrentScope(p[-1]):
debug.printError("Redefinition of function '%s'" %p[-1])
raise SyntaxError
else:
# Print to console
debug.printStatementBlock("Definition of function '%s'" %p[-1])
# Create a new variable to hold the function
ST.addIdentifier(p[-1], 'FUNCTION')
# Create a new temporary for the function and store it in the addressList
displayValue, offset = ST.getAttribute(p[-1], 'scopeLevel'), ST.getAttribute(p[-1], 'offset')
place = ST.newTemp((displayValue, offset), variable=p[-1])
ST.addAttribute(p[-1], ST.getCurrentScope(), place)
ST.addAttribute(p[-1], 'reference', p[0]['reference'])
# Emit the location of the function reference
TAC.emit(place, p[0]['reference'], '', '=REF')
else:
# Print to console
debug.printStatementBlock('Function Definition "%s"' %p[0]['reference'])
# Create a function scope
ST.addScope(p[0]['reference'])
TAC.createFunctionCode(p[0]['reference'])
def p_anonName(p):
'M_anonName : empty'
p[0] = None
def p_argList(p):
'argList : hint SEP_COMMA argList'
p[0] = [ p[1] ] + p[3]
def p_argList_base(p):
'argList : hint'
p[0] = [p[1]]
def p_argList_empty(p):
'argList : empty'''
p[0] = [ ]
def p_hint(p):
'''hint : IDENTIFIER OP_HINT HINT_NUMBER
| IDENTIFIER OP_HINT HINT_CALLBACK
| IDENTIFIER OP_HINT HINT_STRING
| IDENTIFIER OP_HINT HINT_ARRAY
| IDENTIFIER OP_HINT HINT_BOOLEAN'''
p[0] = {'name': p[1] }
# According to the hint assign a type to the identifier
if p[3] == 'callback':
p[0]['type'] = 'CALLBACK'
elif p[3] == 'num':
p[0]['type'] = 'NUMBER'
elif p[3] == 'bool':
p[0]['type'] = 'BOOLEAN'
elif p[3] == 'string':
p[0]['type'] = 'STRING'
else:
p[0]['type'] = 'ARRAY'
def p_hint_error(p):
'hint : IDENTIFIER'
# Pass in any empty object
p[0] = {'name': p[1]}
debug.printError("No hint provided for variable '%s'" %p[1])
raise SyntaxError
def p_insertArgs(p):
'M_insertArgs : empty'
# Add identifiers to local scope
for argument in p[-2]:
# Any callback is stored as a CALLBACK which is a different type
if ST.existsInCurrentScope(argument['name']):
debug.printError("Redefinition of argument '%s'" %argument['name'])
raise SyntaxError
else:
if argument['type'] == 'FUNCTION':
ST.addIdentifier(argument['name'], 'CALLBACK')
else:
ST.addIdentifier(argument['name'], argument['type'])
# store the address into the address descriptor
# The parameters have to loaded in form memory
displayValue, offset = ST.getAttribute(argument['name'], 'scopeLevel'), ST.getAttribute(argument['name'], 'offset')
place = ST.newTemp((displayValue, offset), variable=argument['name'], loadFromMemory=True)
ST.addAttribute(argument['name'], ST.getCurrentScope(), place)
debug.printStatementBlock("Argument '%s' of type '%s'" %(argument['name'], argument['type']))
# Now we store the number of parameters in the function
ST.addAttributeToCurrentScope('numParam', len(p[-2]))
########################################
######## RETURN STATEMENT ##############
########################################
def p_returnStatement(p):
'returnStatement : RETURN expression'
# Type rules
p[0] = { 'type' : p[2]['type'] }
# Get the current returnType from function
returnType = ST.getAttributeFromCurrentScope('returnType')
# If the function has not been assigned a return type as of yet
if returnType == 'UNDEFINED':
# Assign a returnType to the function
if p[2]['type'] == 'FUNCTION':
ST.addAttributeToCurrentScope('returnType', 'CALLBACK')
debug.printStatement("Return statement of type 'CALLBACK'")
else:
ST.addAttributeToCurrentScope('returnType', p[2]['type'])
debug.printStatement("Return statement of type '%s'" %p[2]['type'])
elif p[2]['type'] != returnType:
p[0]['type'] = 'TYPE_ERROR'
debug.printError('Return Types dont match')
raise SyntaxError
else:
# In this case, the return types match, so we needn't do anything
pass
# Emit code for the return type
TAC.emit(p[2]['place'], '' ,'', 'RETURN')
########################################
######## FUNCTIONS CALLS ###############
########################################
def p_functionCall(p):
'functionCall : IDENTIFIER SEP_OPEN_PARENTHESIS actualParameters SEP_CLOSE_PARENTHESIS'
p[0] = {}
# If the identifier does not exist then we output error
if not ST.exists(p[1]):
p[0]['type'] = 'REFERENCE_ERROR'
debug.printError("Function '%s' is not defined" %p[1])
raise SyntaxError
else:
identifierType = ST.getAttribute(p[1], 'type')
# If the function exists in the current scope
if identifierType in [ 'FUNCTION', 'CALLBACK' ]:
if not ST.existsInCurrentScope(p[1]):
# The definition of the function has to be loaded in from memory
displayValue, offset = ST.getAttribute(p[1], 'scopeLevel'), ST.getAttribute(p[1], 'offset')
place = ST.newTemp((displayValue, offset),variable=p[1], loadFromMemory=True)
ST.addAttribute(p[1], ST.getCurrentScope(), place)
else:
place = ST.getAttribute(p[1], ST.getCurrentScope())
# Now we print the param statements
for param in p[3]:
TAC.emit(param, '', '', 'PARAM')
# The name of the function
debug.printStatementBlock("Function call to '%s'" %p[1])
# We jump to the function
TAC.emit('', '', place, 'JUMPLABEL')
# In case the function call is used in an expression
# The type of the statment is dependent on the input condition
if identifierType != 'CALLBACK':
reference = ST.getAttribute(p[1], 'reference')
p[0]['type'] = ST.getAttributeFromFunctionList(reference, 'returnType')
if p[0]['type'] != 'CALLBACK':
returnPlace = ST.newTemp()
TAC.emit(returnPlace, '', '', 'FUNCTION_RETURN')
p[0]['place'] = returnPlace
else:
p[0]['type'] = 'CALLBACK'
else:
p[0]['type'] = 'REFERENCE_ERROR'
debug.printError('Not a function "%s"' %p[1])
raise SyntaxError
def p_parameters(p):
'actualParameters : expression SEP_COMMA actualParameters'
p[0] = [p[1]['place']] + p[3]
def p_parameters_base(p):
'actualParameters : expression'
p[0] = [p[1]['place']]
def p_parameters_empty(p):
'actualParameters : empty'
p[0] = []
########################################
######## BREAK STATEMENT ###############
########################################
def p_breakStatement(p):
'breakStatement : BREAK'
debug.printStatement('Break')
# Type rules
p[0] = {}
# Emit code
p[0]['loopEndList'] = [TAC.getNextQuad()]
TAC.emit('', '', -1, 'GOTO')
########################################
######## CONTINUE STATEMENT ############
########################################
def p_continueStatement(p):
'continueStatement : CONTINUE'
debug.printStatement('Continue')
# Type rules
p[0] = {}
# Emit code
p[0]['loopBeginList'] = [TAC.getNextQuad()]
TAC.emit('', '', -1, 'GOTO')
########################################
############# IF THEN ##################
########################################
def p_ifThen(p):
'ifThen : IF SEP_OPEN_PARENTHESIS expression SEP_CLOSE_PARENTHESIS M_ifBranch block'
# Type rules
if p[3]['type'] != 'BOOLEAN':
debug.printError('If condition must be a boolean')
raise SyntaxError
p[0] = {}
# For break statement and next waiting functions
p[0]['nextList'] = TAC.merge(p[5].get('falseList', []), p[6].get('nextList', []))
p[0]['loopEndList'] = p[6].get('loopEndList', [])
p[0]['loopBeginList'] = p[6].get('loopBeginList',[])
########################################
############# IF THEN ELSE #############
########################################
def p_ifThenElse(p):
'ifThenElse : IF SEP_OPEN_PARENTHESIS expression SEP_CLOSE_PARENTHESIS M_ifBranch block ELSE M_elseBranch block'
# Type rules
if p[3]['type'] != 'BOOLEAN':
debug.printError('If condition must be a boolean')
raise SyntaxError
p[0] = {}
# backPatch the if branch
TAC.backPatch(p[5]['falseList'], p[8]['quad'])
p[0]['nextList'] = p[8]['nextList']
# For break statement
p[0]['loopEndList'] = TAC.merge(p[9].get('loopEndList', []), p[6].get('loopEndList', []))
p[0]['loopBeginList'] = TAC.merge(p[9].get('loopBeginList', []), p[6].get('loopBeginList', []))
def p_m_ifBranch(p):
'M_ifBranch : empty'
# Print to the console
debug.printStatementBlock("If Branch")
p[0] = {}
p[0]['falseList'] = [TAC.getNextQuad()]
TAC.emit(p[-2]['place'], '', -1, 'COND_GOTO_Z')
def p_m_elseBranch(p):
'M_elseBranch : empty'
# Print to the console
debug.printStatementBlock("Else Branch")
p[0] = {}
p[0]['nextList'] = [TAC.getNextQuad()]
TAC.emit('', '', -1, 'GOTO')
p[0]['quad'] = TAC.getNextQuad()
########################################
########## WHILE STATEMENT #############
########################################
def p_while(p):
'whileStatement : WHILE M_quad SEP_OPEN_PARENTHESIS expression SEP_CLOSE_PARENTHESIS M_whileBranch block'
# Emit code
p[0] = {}
p[0]['nextList'] = []
# Backpatch
if p[4]['type'] == 'BOOLEAN':
# Backpatch continue statements and break statements
TAC.backPatch(p[7]['loopBeginList'], p[2]['quad'])
p[0]['nextList'] = TAC.merge(p[7].get('loopEndList', []), p[7].get('nextList', []))
p[0]['nextList'] = TAC.merge(p[6].get('falseList', []), p[0].get('nextList', []))
# Loop around
TAC.emit('', '', p[2]['quad'], 'GOTO')
else:
debug.printError('The condition of while should be a boolean')
raise SyntaxError
p[0]['type'] = 'VOID'
def p_m_whileBranch(p):
'M_whileBranch : empty'
p[0] = {}
p[0]['falseList'] = [TAC.getNextQuad()]
TAC.emit(p[-2]['place'], '', -1, 'COND_GOTO_Z')
# Print to the console
debug.printStatementBlock("While Statement")
########################################
############## PRINT ###################
########################################
def p_printStatement(p):
'printStatement : CONSOLE OP_DOT LOG SEP_OPEN_PARENTHESIS printList SEP_CLOSE_PARENTHESIS'
p[0] = {}
for printIterator in p[5]:
# Check if the given expression is printable or not
expType = printIterator.get('type')
if expType in ['STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED']:
TAC.emit(printIterator['place'], '', printIterator['type'], 'PRINT')
debug.printStatement("Print Statement of type %s" %printIterator['type'])
else:
debug.printError('Given expression is not a printable type')
raise SyntaxError
# We print a new line
# TAC.emit('', '', 'NEW_LINE', 'PRINT')
def p_printList(p):
'printList : expression SEP_COMMA printList'
p[0] = [ { 'place': p[1]['place'], 'type' : p[1]['type'] } ] + p[3]
def p_printList_base(p):
'printList : expression'''
p[0] = [ { 'place': p[1]['place'], 'type' : p[1]['type'] } ]
def p_printList_empty(p):
'printList : empty'''
p[0] = []
########################################
############## EXPRESSIONS #############
########################################
# Precedence of operators
precedence = (
('left', 'OP_OR'),
('left', 'OP_AND'),
('left', 'OP_EQUALS', 'OP_NOT_EQUALS'),
('left', 'OP_LESS_THEN', 'OP_GREATER_THEN', 'OP_LESS_THEN_E', 'OP_GREATER_THEN_E'),
('left', 'OP_PLUS', 'OP_MINUS'),
('left', 'OP_MULTIPLICATION', 'OP_DIVISION', 'OP_MODULUS'),
('right', 'UMINUS', 'OP_TYPEOF', 'OP_NOT'),
)
######## UNARY EXPRESSIONS ############
def p_expression_unary(p):
'''expression : OP_MINUS expression %prec UMINUS
| OP_TYPEOF expression'''
# Type rules
expType = 'UNDEFINED'
# Emit code
p[0] = {}
p[0]['place'] = ST.newTemp()
# Conditional branch to figure out what code to emit and check types
if p[1] == '-':
if p[2]['type'] == 'NUMBER':
expType = 'NUMBER'
TAC.emit(p[0]['place'], p[2]['place'] , '' , 'uni-')
else:
expType = 'TYPE_ERROR'
debug.printError('Type Mismatch in expression')
raise SyntaxError
else:
expType = 'STRING'
TAC.emit(p[0]['place'], p[2]['type'] , '' , '=')
# Return type of the statment
p[0]['type'] = expType
######## BINARY EXPRESSIONS ############
def p_expression_binop(p):
'''expression : expression OP_PLUS expression
| expression OP_MINUS expression
| expression OP_MULTIPLICATION expression
| expression OP_DIVISION expression
| expression OP_MODULUS expression'''
# Type rules
expType = 'UNDEFINED'
# To store information
p[0] = {}
p[0]['place'] = ST.newTemp()
# To emit codes
if p[1]['type'] == 'NUMBER' and p[3]['type'] == 'NUMBER':
expType = 'NUMBER'
TAC.emit(p[0]['place'], p[1]['place'], p[3]['place'], p[2])
else:
expType = 'TYPE_ERROR'
debug.printError('Type Mismatch in Arithematic Expression')
raise SyntaxError
p[0]['type'] = expType
######## RELATIONAL EXPRESSION ############
def p_expression_relational(p):
'''expression : expression OP_GREATER_THEN expression
| expression OP_GREATER_THEN_E expression
| expression OP_LESS_THEN expression
| expression OP_LESS_THEN_E expression
| expression OP_EQUALS expression
| expression OP_NOT_EQUALS expression'''
# Type rules
expType = 'UNDEFINED'
if p[1]['type'] == p[3]['type'] == 'NUMBER':
expType = 'BOOLEAN'
else:
expType = 'TYPE_ERROR'
debug.printError('Operands to relational expressions must be numbers')
raise SyntaxError
p[0] = { 'type' : expType }
p[0]['place'] = ST.newTemp()
# Emit code
TAC.emit(p[0]['place'], p[1]['place'], p[3]['place'], p[2])
######## LOGICAL EXPRESSION ##############
def p_expression_logical_and(p):
'expression : expression OP_AND M_quad expression'
# Type rules
expType = 'BOOLEAN'
# Backpatching code
p[0] = {}
p[0]['place'] = ST.newTemp()
if p[1]['type'] == p[4]['type'] == 'BOOLEAN':
expType = 'BOOLEAN'
TAC.emit(p[0]['place'], p[1]['place'], p[4]['place'] , p[2])
else:
expType = 'TYPE_ERROR'
debug.printError('Operands to logical expressions must be integers')
raise SyntaxError
# Type of the expression
p[0]['type'] = expType
def p_expression_logical_or(p):
'expression : expression OP_OR M_quad expression'
# Type rules
expType = 'UNDEFINED'
# Backpatching code
p[0] = {}
p[0]['place'] = ST.newTemp()
if p[1]['type'] == p[4]['type'] == 'BOOLEAN':
expType = 'BOOLEAN'
TAC.emit(p[0]['place'], p[1]['place'], p[4]['place'] , p[2])
else:
expType = 'TYPE_ERROR'
debug.printError('Operands to logical expressions must be integers')
raise SyntaxError
# Type of the expression
p[0]['type'] = expType
def p_expression_logical_not(p):
'expression : OP_NOT expression'
# Type rules
expType = 'BOOLEAN'
# Backpatching code
p[0] = {}
p[0]['place'] = ST.newTemp()
if p[2]['type'] != 'BOOLEAN':
expType = 'TYPE_ERROR'
debug.printError('Operands to logical expressions must be integers')
raise SyntaxError
else:
TAC.emit(p[0]['place'], p[2]['place'], '' , p[1])
# Type of the expression
p[0]['type'] = expType
######## GROUP EXPRESSION ##############
def p_expression_group(p):
'expression : SEP_OPEN_PARENTHESIS expression SEP_CLOSE_PARENTHESIS'
# Type rules
p[0] = { 'type' : p[2]['type'] }
# emit code
p[0]['place'] = p[2]['place']
# Backpatching code
if p[2]['type'] == 'BOOLEAN':
p[0]['trueList'] = p[2].get('trueList', [])
p[0]['falseList'] = p[2].get('falseList', [])
######## BASE TYPE EXPRESSION ###########
def p_expression_baseType(p):
'expression : baseType'
# Type rules
p[0] = { 'type' : p[1]['type'] }
p[0]['place'] = ST.newTemp()
# emit code for backPatch
if p[1]['type'] in ['FUNCTION', 'STRING']:
p[0]['reference'] = p[1]['reference']
TAC.emit(p[0]['place'], p[0]['reference'], '', '=REF')
else:
TAC.emit(p[0]['place'], p[1]['value'], '', '=i')
######## IDENTIFIER EXPRESSION ###########
def p_expression_identifier(p):
'expression : IDENTIFIER'
# Type rules
p[0] = {}
# We have to check if the identifier exists in the current scope or not, and
# accordingly load it in
if ST.exists(p[1]):
p[0]['type'] = ST.getAttribute(p[1], 'type')
# Here we have to load in the value of the variable
if not ST.existsInCurrentScope(p[1]):
# If an identifier is used, we assume that it is present in memory
displayValue, offset = ST.getAttribute(p[1], 'scopeLevel'), ST.getAttribute(p[1], 'offset')
p[0]['place'] = ST.newTemp((displayValue, offset), variable=p[1], loadFromMemory=True)
ST.addAttribute(p[1], ST.getCurrentScope(), p[0]['place'])
else:
p[0]['place'] = ST.getAttribute(p[1], ST.getCurrentScope())
else:
p[0]['type'] = 'REFERENCE_ERROR'
debug.printError('Undefined Variable "%s"' %p[1])
raise SyntaxError
######## FUNCTION CALLS ##################
def p_expression_functionCall(p):
'expression : functionCall'
# Return the value of the function
p[0] = {}
p[0]['type'] = p[1]['type']
if p[1]['type'] == 'CALLBACK':
debug.printError('Callback functions cannot be used as expressions')
raise SyntaxError
else:
p[0]['place'] = p[1]['place']
######## ARRAY EXPRESSION ################
def p_expression_array(p):
'expression : array'
p[0] = {}
print "array"
########################################
########## BASE TYPES ##################
########################################
def p_baseType_number(p):
'baseType : NUMBER'
# Type rules
p[0] = { 'type' : 'NUMBER', 'value' : int(p[1]) }
def p_baseType_boolean(p):
'baseType : BOOLEAN'
# Type rules
if p[1] == 'true':
value = 1
else:
value = 0
p[0] = { 'type' : 'BOOLEAN' , 'value' : value }
def p_baseType_string(p):
'baseType : STRING'
# Type rules
p[0] = { 'type' : 'STRING' , 'reference': ST.nameString(), 'value' : p[1] }
# Whenever a string is defined, we have to add it to the function's data region
ST.addToStringList(p[0]['reference'], p[1])
def p_baseType_undefined(p):
'baseType : UNDEFINED'
# Type rules
p[0] = { 'type' : 'UNDEFINED', 'value' : 0}
######## FUNCTION EXPRESSION ###########
def p_baseType_function(p):
'baseType : functionStatement'
# Type rules
p[0] = { 'type': 'FUNCTION', 'reference': p[1]['reference']}
####### ARRAY EXPRESSIONS ##############
def p_baseType_array(p):
'array : SEP_OPEN_BRACKET arrayList SEP_CLOSE_BRACKET'
p[0] = {'type': 'NUMBER'}
def p_arrayList(p):
'arrayList : expression SEP_COMMA arrayList'
print p[3]
if p[3]['type'] == 'UNDEFINED':
p[0] = {'value': p[1]['place'], 'type' : p[1]['type']}
elif p[1]['type'] == p[3]['type']:
p[0] = {'value': [p[1]['place']] + p[3]['value'], 'type' : p[1]['type']}
else:
debug.printError('Elements of an array must be of the same type')
def p_arrayList_base(p):
'arrayList : expression'
p[0] = {'value': [p[1]['place']], 'type' : p[1]['type']}
def p_arrayList_empty(p):
'arrayList : empty'
p[0] = {'value': [], 'type': 'UNDEFINED'}
########################################
################ EMPTY #################
########################################
def p_empty(p):
'empty :'
p[0] = {}
########################################
############# ERROR ####################
########################################
def p_error(p):
debug.printError("Whoa. You are seriously hosed.")
# Read ahead looking for a closing '}'
tok = parser.token()
if not tok:
while 1:
if not tok or tok.type in ['SEP_SEMICOLON', 'SEP_OPEN_BRACE', 'SEP_CLOSE_BRACE']:
break
tok = parser.token() # Get the next token
parser.restart()
# parser.errok()
######################################################################################################
######## Required Globals ##############
ST = SymbolTable.SymbolTable()
TAC = ThreeAddressCode.ThreeAddressCode(ST)
parser = yacc.yacc()
########################################
def parseProgram(program):
parser.parse(program, lexer=lexer)
return ST, TAC, debug