/
alice_yacc.py
1509 lines (1361 loc) · 42.7 KB
/
alice_yacc.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
import ply.yacc as yacc
from sys import argv
from collections.abc import Iterable
from alice_lex import tokens
from sem_cube import get_result
from structs import *
#--------------------------Environment Setup------------------------------------
tmpvar_n = -1
quad_count = -1
fun = False
dims = False
dim = None
recursive_calls = []
S = stacks()
funDir = mdl_dir()
constants = cte_table()
variables = var_table()
quadruples = quadruple_list()
memory = memory()
env = 'global'
#--------------------------Auxiliary Functions----------------------------------
'''
find(ID, list):
Receives an ID and the list where it will look for the ID.
If it finds it, the function will return the entire var/cte object,
otherwise it will return False.
'''
def find(ID, list_name):
if list_name == 'dec':
if not variables.var_list:
return False
for var in variables.var_list:
if var.ID != ID:
continue
else:
if env != 'global' and (var.v_address >= 5000 and var.v_address < 10000):
return var
elif env == 'global' and (var.v_address >= 0 and var.v_address < 5000):
return var
return False
elif list_name == 'cte':
if not constants.cte_list:
return False
for constant in constants.cte_list:
if constant.ID == ID:
return constant
return False
elif list_name == 'var':
if not variables.var_list:
return False
var_list = variables.var_list.copy()
var_list.reverse()
for var in var_list:
if var.ID != ID:
continue
else:
return var
return False
'''
quad_gen(quad):
Receives a tuple containing the values of a new quadruple to add to the
list, generates a new quadruple object, adds it, and updates the counter.
'''
def quad_gen(quad):
global quad_count
new_quad = quadruple(*quad)
quadruples.append(new_quad)
quad_count += 1
'''
quad_address(temp):
Returns the address of a provided ID, if temp was fed into the fucntion,
otherwise it pops a value from the symbols stack and returns its address.
'''
def quad_address(temp=None):
if temp == None:
temp = S.Symbols.pop()
token = find(temp, 'cte')
if not token:
token = find(temp, 'var')
if not token:
print(f"Error! Variable '{temp}' wasn't declared!")
quit()
return token.v_address
'''
temporary_handler(type, append, name):
Creates a new temporary variable of a given type. If append is true, it
will add the value to the symbol and type stacks, if name is not false,
the new variable will receive the name of the value given.
'''
def temporary_handler(type, append=True, name=False):
if type == 0:
address = memory.tmpi[0] + memory.tmpi[1]
if address >= memory.tmpf[0]:
print(f'Error! Too many temporal integer variables!')
quit()
memory.tmpi[1] += 1
typetxt = 'int'
elif type == 1:
address = memory.tmpf[0] + memory.tmpf[1]
if address >= memory.tmpb[0]:
print(f'Error! Too many temporal float variables!')
quit()
memory.tmpf[1] += 1
typetxt = 'float'
elif type == 3:
address = memory.tmpb[0] + memory.tmpb[1]
if address >= memory.ctei[0]:
print(f'Error! Too many temporal boolean variables!')
quit()
memory.tmpb[1] += 1
typetxt = 'bool'
else:
address = memory.ptrs[0] + memory.ptrs[1]
if address >= 32000:
print(f'Error! Too many pointers!')
quit()
memory.ptrs[1] += 1
typetxt = 'pointer'
if not name:
global tmpvar_n
tmpvar_n += 1
if type == 4:
tn = 'ptr' + str(tmpvar_n)
else:
tn = 't' + str(tmpvar_n)
else:
tn = name
if append:
S.Types.append((type, typetxt))
S.Symbols.append(tn)
new_var = var_object(tn, (type, typetxt), address, [[0, 0], 1])
variables.append(new_var)
return address
'''
constant_handler(p, cte, append):
Given a list containing a group of values, it will attempt to find the
value of the top of the list in the constants table. If it finds it, it
will return the constant object of the ID, otherwise it will create a
new object for the ID. If append is true, it will add the value to the
symbol and type stacks.
'''
def constant_handler(p, cte, append=True):
if append:
S.Symbols.append(p[-1])
S.Types.append(cte)
if find(p[-1], 'cte') != False:
return find(p[-1], 'cte')
else:
if cte[0] == 0:
address = memory.ctei[0] + memory.ctei[1]
if address >= memory.ctef[0]:
print(f'Error! Too many integer literals!')
quit()
memory.ctei[1] += 1
elif cte[0] == 1:
address = memory.ctef[0] + memory.ctef[1]
if address >= memory.ctes[0]:
print(f'Error! Too many float literals!')
quit()
memory.ctef[1] += 1
else:
address = memory.ctes[0] + memory.ctes[1]
if address >= memory.ptrs[0]:
print(f'Error! Too many string literals!')
quit()
memory.ctes[1] += 1
new_cte = cte_object(p[-1], address)
constants.append(new_cte)
'''
expression_handler(operator):
Given an operator for an expression, it first evaluates the top of the
operators stack in order to check if it corresponds with the operator
found. It evaluates the semantics of the operation and creates a new
quadruple for the received operation.
'''
def expression_handler(operator):
length = len(S.Operators)
try:
size = length - 1 - S.Operators.index('(')
except ValueError:
size = length
if size > 0 and S.Operators[length - 1] == operator:
type_der = S.Types.pop()
type_izq = S.Types.pop()
operator = S.Operators.pop()
res = get_result((operator, type_izq[0], type_der[0]))
if res is False:
if operator == 'and' or operator == 'or':
print(f'Semantic error! Expected two bool operands, got {type_izq[1]} and {type_der[1]}!')
elif operator in ['==', '¬=', '<', '<=', '>', '>=']:
print(f'Semantic error! Cannot compare {type_izq[1]} and {type_der[1]}!')
else:
print(f'Semantic error! Cannot perform arithmetic operations between {type_izq[1]} and {type_der[1]}!')
quit()
der_address = quad_address()
izq_address = quad_address()
tn = temporary_handler(res)
quad_gen((operator, izq_address, der_address, tn))
'''
unary_handler(p, operator):
Given an array of symbols and an operator, the function will evaluate
the semantics of the operation and generate one of 3 possible quadruples
for either ++, --, or unary -.
'''
def unary_handler(p, operator):
length = len(S.Operators)
try:
size = length - 1 - S.Operators.index('(')
except ValueError:
size = length
if size > 0 and S.Operators[length - 1] == operator:
type_der = S.Types.pop()
operator = S.Operators.pop()
if operator == '-':
if type_der[0] > 1:
print(f'Semantic error! {type_der[1]} data cannot be turned negative!')
quit()
else:
res = type_der[0]
else:
res = get_result((operator, type_der[0]))
if res is False:
print(f'Semantic error! Cannot perform arithmetic increment or decrease on {type_der[1]}!')
quit()
der_address = quad_address()
if operator == '-' or (der_address >= 26000 and der_address < 31000):
tn = temporary_handler(res)
quad_gen((operator, None, der_address, tn))
else:
S.Symbols.append(p[-3])
S.Types.append(type_der)
quad_gen((operator, None, der_address, der_address))
'''
dimension_tracker(dim):
Generates the semantic verification for array indexation, as well as the
S1*m1 multiplication and S1*m1+S2 sum in case of a matrix.
'''
def dimension_tracker(dim):
val = quad_address(S.Symbols[-1])
curr = dims.arr_size if type(dims.arr_size[1]) != list else dims.arr_size[0]
izq = find(curr[0][0], 'cte')
der = find(curr[0][1], 'cte')
quad_gen(('Verify', val, izq.v_address, der.v_address))
if type(dims.arr_size[1]) == list:
aux = [S.Symbols.pop(), S.Types.pop()]
temp = temporary_handler(0)
size = find(curr[0][1], 'cte')
quad_gen(('*', val, size.v_address, temp))
if dim > 1:
aux2 = [quad_address(S.Symbols.pop()), S.Types.pop()]
aux1 = [quad_address(S.Symbols.pop()), S.Types.pop()]
temp = temporary_handler(0)
quad_gen(('+', aux1[0], aux2[0], temp))
'''
call_solver(ARE, IDList, aux, modify):
Given an empty ARE quadruple, it will fill it up with the resources
necessary to call the function, as well as generate the pertinent
Parameter quadruples and the GoSub quadruple.
'''
def call_solver(ARE, IDList, aux, modify=False):
addresses = [[], []]
ARE[2] = addresses[0]
ARE[3] = addresses[1]
params = []
if fun.size != None:
locals = fun.size[0].copy()
else:
temp = [IDList, aux.copy()]
for i in range(len(IDList)):
prototype = fun.prototyping[i][0]
param = aux.pop()
if prototype != param[1][0]:
print(f"Error! Type mismatch in function call to '{fun.ID}' with parameter {i+1}!")
quit()
var = find(param[0], 'cte')
if not var:
var = find(param[0], 'var')
if fun.size != None and var.v_address >= 6000 and var.v_address < 11000:
locals[prototype] -= 1
addresses[0].append(var.v_address)
if not modify:
params.append(('Parameter', None, var.v_address, i+1))
if fun.size != None:
for i in range(len(locals)):
if locals[i] == 0:
continue
for j in range(locals[i]):
if i == 0:
address = memory.lcli[0] + memory.lcli[1]
if address >= memory.lclf[0]:
print(f'Error! Too many local integer variables!')
quit()
memory.lcli[1] += 1
elif i == 1:
address = memory.lclf[0] + memory.lclf[1]
if address >= memory.lcls[0]:
print(f'Error! Too many local float variables!')
quit()
memory.lclf[1] += 1
else:
address = memory.lcls[0] + memory.lcls[1]
if address >= memory.tmpi[0]:
print(f'Error! Too many local string variables!')
quit()
memory.lcls[1] += 1
addresses[0].append(address)
temps = fun.size[1]
for i in range(len(temps)):
if temps[i] == 0:
continue
for j in range(temps[i]):
address = temporary_handler(i, False)
addresses[1].append(address)
if not modify:
quad_gen(ARE)
else:
quad_gen(('ARE', fun.ID, None, None))
recursive_calls.append([quad_count, temp])
if not modify:
for quad in params:
quad_gen(quad)
quad_gen(('GoSub', None, None, fun.beginning))
else:
return ARE
'''
get_IDs(IDList):
Given a list with multiple levels of nested lists, it flattens the list
and returns a single list with all the values.
'''
def get_IDs(IDList):
for item in IDList:
if isinstance(item, Iterable) and not isinstance(item, str):
for x in get_IDs(item):
yield x
else:
yield item
'''
end_yacc():
Exports the quadruples, constants and the function directory to JSON format.
'''
def end_yacc():
export(quadruples, constants, funDir)
#---------------------------Program Structure-----------------------------------
def p_program(p):
'''
program : BEGIN beginprog ID lclenv_setup COLON global ENDPROG endprog
'''
end_yacc()
def p_global(p):
'''
global : module global
| declaration global
| main
'''
def p_main(p):
'''
main : MAIN lclenv_setup stmtblock
'''
def p_stmtblock(p):
'''
stmtblock : COLON initstmt END
'''
def p_initstmt(p):
'''
initstmt : stmt stmtchain
| empty
'''
def p_stmtchain(p):
'''
stmtchain : stmtchain stmt
| empty
'''
def p_stmt(p):
'''
stmt : assignment
| conditional
| print
| input
| iteration
| plot
| declaration
| mirror
| expression popexpr
| return
'''
#--------------------------Complex Statements-----------------------------------
def p_conditional(p):
'''
conditional : IF expr neuralgic_if THEN stmtblock neuralgic_cond
| IF expr neuralgic_if THEN COLON initstmt ELSE neuralgic_else stmtblock neuralgic_cond
'''
def p_iteration(p):
'''
iteration : while
| do_while
| for
'''
def p_while(p):
'''
while : WHILE neuralgic_while expr while_expr stmtblock while_end
'''
def p_do_while(p):
'''
do_while : DO neuralgic_dw stmtblock WHILE expression dw_end
'''
def p_for(p):
'''
for : FOR ID for_id ASSIGN expr for_expr COLON expr neuralgic_for stmtblock for_end
'''
#-------------------------------Module------------------------------------------
def p_module(p):
'''
module : MODULE ID TYPE_ASSIGN mdl_type lclenv_setup LPAREN params neuralgic_params RPAREN stmtblock lclenv_end
'''
def p_mdl_type(p):
'''
mdl_type : VOID
| type
'''
p[0] = p[1]
def p_params(p):
'''
params : ID TYPE_ASSIGN type rparams
| empty
'''
if len(p) > 2:
if p[4] == None:
p[0] = p[1], p[3]
else:
p[0] = p[1], p[3], p[4]
else:
p[0] = p[1]
def p_rparams(p):
'''
rparams : rparams COMA ID TYPE_ASSIGN type
| empty
'''
if len(p) > 2:
p[0] = p[1], p[3], p[5]
else:
p[0] = p[1]
#-----------------------------Variables-----------------------------------------
def p_assignment(p):
'''
assignment : variable ASSIGN expression neuralgic_assign
'''
def p_declaration(p):
'''
declaration : LET ID others TYPE_ASSIGN type idxsize neuralgic_dec SEMICOLON
'''
def p_others(p):
'''
others : others COMA ID
| empty
'''
if len(p) > 2:
if p[1] == None:
p[0] = p[3]
p[0] = p[1], p[3]
else:
p[0] = p[1]
def p_idxsize(p):
'''
idxsize : L_SBRKT CTE_I matrix R_SBRKT
| empty
'''
if len(p) > 2:
p[0] = p[2], p[3]
else:
p[0] = p[1]
def p_matrix(p):
'''
matrix : COMA CTE_I
| empty
'''
if len(p) > 2:
p[0] = p[2]
else:
p[0] = p[1]
#------------------------------Expressions--------------------------------------
def p_expression(p):
'''
expression : expr SEMICOLON
'''
p[0] = p[1]
def p_expr(p):
'''
expr : andexpr
| expr OR neuralgic_opr andexpr neuralgic_expr
'''
p[0] = p[1]
def p_andexpr(p):
'''
andexpr : eqlexpr
| andexpr AND neuralgic_opr eqlexpr neuralgic_expr
'''
p[0] = p[1]
def p_eqlexpr(p):
'''
eqlexpr : relexpr
| eqlexpr eqop neuralgic_opr relexpr neuralgic_expr
'''
p[0] = p[1]
def p_eqop(p):
'''
eqop : EQ
| NE
'''
p[0] = p[1]
def p_relexpr(p):
'''
relexpr : sumexpr
| relexpr relop neuralgic_opr sumexpr neuralgic_expr
'''
p[0] = p[1]
def p_relop(p):
'''
relop : LE
| LT
| GE
| GT
'''
p[0] = p[1]
def p_sumexpr(p):
'''
sumexpr : term
| sumexpr sumop neuralgic_opr term neuralgic_expr
'''
p[0] = p[1]
def p_sumop(p):
'''
sumop : PLUS
| MINUS
'''
p[0] = p[1]
def p_term(p):
'''
term : unary
| term mulop neuralgic_opr unary neuralgic_expr
'''
p[0] = p[1]
def p_mulop(p):
'''
mulop : EXPONENT
| MULTIPLY
| DIVIDE
| INT_DIV
'''
p[0] = p[1]
def p_unary(p):
'''
unary : postfix
| MINUS neuralgic_opr postfix neuralgic_unary
'''
if len(p) > 2:
p[0] = p[3]
else:
p[0] = p[1]
def p_postfix(p):
'''
postfix : factor
| factor postop neuralgic_opr neuralgic_unary
'''
p[0] = p[1]
def p_postop(p):
'''
postop : ADD
| DECREASE
'''
p[0] = p[1]
def p_factor(p):
'''
factor : LPAREN neuralgic_opr expr RPAREN neuralgic_paren
| value
| variable
| systemdef neuralgic_stats
| call add_call
'''
if len(p) > 3:
p[0] = p[3]
else:
p[0] = p[1]
def p_value(p):
'''
value : CTE_I neuralgic_int
| CTE_F neuralgic_float
| CTE_STRING neuralgic_str
'''
p[0] = p[1]
def p_variable(p):
'''
variable : ID neuralgic_var
| ID neuralgic_var L_SBRKT neuralgic_array expr evaluate_dimension R_SBRKT end_dimensions
| ID neuralgic_var L_SBRKT neuralgic_array expr evaluate_dimension COMA expr neuralgic_matrix R_SBRKT end_dimensions
'''
if len(p) == 9:
p[0] = p[1], p[5], p[7]
elif len(p) == 11:
p[0] = p[1], p[5], p[7], p[8], p[9]
else:
p[0] = p[1]
#----------------------------System Functions-----------------------------------
def p_print(p):
'''
print : PRINT LPAREN funparam RPAREN neuralgic_print SEMICOLON
'''
def p_funparam(p):
'''
funparam : expr auxparams
| empty
'''
if len(p) > 2:
if p[2] == None:
p[0] = p[1]
p[0] = p[1], p[2]
else:
p[0] = p[1]
def p_auxparams(p):
'''
auxparams : auxparams COMA expr
| empty
'''
if len(p) > 2:
if p[1] == None:
p[0] = p[3]
p[0] = p[1], p[3]
else:
p[0] = p[1]
def p_input(p):
'''
input : INPUT LPAREN expr COMA ID RPAREN neuralgic_input SEMICOLON
'''
def p_mirror(p):
'''
mirror : MIRROR LPAREN expr COMA ID RPAREN SEMICOLON neuralgic_mirror
'''
def p_plot(p):
'''
plot : x_plot
| xy_plot
'''
def p_x_plot(p):
'''
x_plot : HIST LPAREN ID COMA CTE_STRING RPAREN SEMICOLON neuralgic_xplot
| VIOLIN LPAREN ID COMA CTE_STRING RPAREN SEMICOLON neuralgic_xplot
| BOXPLOT LPAREN ID COMA CTE_STRING RPAREN SEMICOLON neuralgic_xplot
'''
def p_xy_plot(p):
'''
xy_plot : BAR LPAREN ID COMA ID COMA CTE_STRING RPAREN SEMICOLON neuralgic_xyplot
| SCATTER LPAREN ID COMA ID COMA CTE_STRING RPAREN SEMICOLON neuralgic_xyplot
'''
def p_systemdef(p):
'''
systemdef : SIZE LPAREN ID RPAREN
| MEAN LPAREN ID RPAREN
| MEDIAN LPAREN ID RPAREN
| MODE LPAREN ID RPAREN
| VARIANCE LPAREN ID RPAREN
| STD LPAREN ID RPAREN
| RANGE LPAREN ID RPAREN
| SUM LPAREN ID RPAREN
| MIN LPAREN ID RPAREN
| MAX LPAREN ID RPAREN
'''
p[0] = p[1], p[3]
def p_call(p):
'''
call : ID verify_ID LPAREN funparam neuralgic_call RPAREN
'''
p[0] = p[1], p[3]
#-------------------------------Typed Rules-------------------------------------
def p_return(p):
'''
return : RETURN expression neuralgic_return
'''
def p_type(p):
'''
type : INT
| FLOAT
| STRING
'''
p[0] = p[1]
#---------------------------Neuralgic Rules-------------------------------------
def p_lclenv_setup(p):
'''
lclenv_setup :
'''
if p[-3] == 'begin':
# On beginning of program, add program to function directory
program = mdl_object(p[-1], 'void', quad_count, variables, None, None)
funDir.append(program)
else:
global env
env = p[-1]
if env != 'main':
# If the environment is a typed function, evaluate if the module already exists
if env in ['int', 'float', 'string']:
type = None
address = None
for module in funDir.modules:
if module.ID == p[-3]:
print(f"Error! Module '{p[-3]}' already exists!")
quit()
if find(p[-3], 'var') != False:
print(f"Error! Cannot create module '{p[-3]}', name already reserved!")
quit()
if env == 'int':
type = (0, env)
address = memory.gbli[0] + memory.gbli[1]
if address >= memory.gblf[0]:
print(f'Error! Too many global integer variables!')
quit()
memory.gbli[1] += 1
elif env == 'float':
type = (1, env)
address = memory.gblf[0] + memory.gblf[1]
if address >= memory.gbls[0]:
print(f'Error! Too many global float variables!')
quit()
memory.gblf[1] += 1
elif env == 'string':
type = (2, env)
address = memory.gbls[0] + memory.gbls[1]
if address >= memory.lcli[0]:
print(f'Error! Too many global string variables!')
quit()
memory.gbls[1] += 1
new_var = var_object(p[-3], type, address, [[0, 0], 1])
variables.append(new_var)
program = mdl_object(p[-3], env, quad_count + 1, None, None, None)
funDir.append(program)
else:
goback = S.Jumps.pop()
quadruples.quadruples[goback].storage = quad_count + 1
def p_lclenv_end(p):
'''
lclenv_end :
'''
global env
global tmpvar_n
tmpvar_n = -1
env = 'global'
size = [[0, 0, 0],[0, 0, 0]]
temp = var_table()
aux = []
temp.var_list = variables.copy()
for i in range(len(temp.var_list)):
if temp.var_list[i].v_address >= 6000 and temp.var_list[i].v_address < 8000:
size[0][0] += 1
continue
elif temp.var_list[i].v_address >= 8000 and temp.var_list[i].v_address < 10000:
size[0][1] += 1
continue
elif temp.var_list[i].v_address >= 10000 and temp.var_list[i].v_address < 11000:
size[0][2] += 1
continue
elif temp.var_list[i].v_address >= 11000 and temp.var_list[i].v_address < 16000:
size[1][0] += 1
continue
elif temp.var_list[i].v_address >= 16000 and temp.var_list[i].v_address < 21000:
size[1][1] += 1
continue
elif temp.var_list[i].v_address >= 21000 and temp.var_list[i].v_address < 26000:
size[1][2] += 1
continue
aux.append(temp.var_list[i])
funDir.modules[-1].variables = temp
funDir.modules[-1].size = size
if not recursive_calls:
pass
else:
for i in range(len(recursive_calls)):
call = recursive_calls[i][0]
info = recursive_calls[i][1]
curr = quadruples.quadruples[call]
ARE = [curr.operation, curr.operand1, curr.operand2, curr.storage]
ARE = call_solver(ARE, *info, modify=True)
curr.operand2 = ARE[2]
curr.storage = ARE[3]
memory.clear()
variables.var_list = aux
quad_gen(('EndModule', None, None, None))
def p_beginprog(p):
'''
beginprog :
'''
quad_gen(('Goto', None, None, 'Main'))
S.Jumps.append(quad_count)
def p_endprog(p):
'''
endprog :
'''
quad_gen(('EndProgram', None, None, None))
def p_popexpr(p):
'''
popexpr :
'''
if not S.Symbols:
return
S.Symbols.pop()
S.Types.pop()
def p_neuralgic_assign(p):
'''
neuralgic_assign :
'''
der_address = quad_address()
izq_address = quad_address()
type_der = S.Types.pop()
type_izq = S.Types.pop()
res = get_result((p[-2], type_izq[0], type_der[0]))
if res is False:
print(f'Semantic error! Type mismatch: Got {type_izq[1]} and {type_der[1]}!')
quit()
quad_gen((p[-2], None, der_address, izq_address))
def p_neuralgic_dec(p):
'''
neuralgic_dec :
'''
if p[-1] == None:
arr_size = [[0, 0], 1]
elif p[-1][1] == None:
dim1 = int(p[-1][0])
constant_handler([dim1 - 1], (0, 'int'), False)
constant_handler([1], (0, 'int'), False)
constant_handler([0], (0, 'int'), False)
if dim1 <= 1:
print(f"Error! Array's size isn't valid!")
quit()
arr_size = [[0, dim1 - 1], 1]
else:
dim1 = int(p[-1][0])
dim2 = int(p[-1][1])
constant_handler([dim1 - 1], (0, 'int'), False)
constant_handler([dim2 - 1], (0, 'int'), False)
constant_handler([dim2], (0, 'int'), False)
constant_handler([1], (0, 'int'), False)
constant_handler([0], (0, 'int'), False)
if dim1 <= 1 or dim2 <= 1:
print(f"Error! Matrix's {'1st' if dim1 <= 0 else '2nd'} dimension's value isn't valid!")
quit()
tsize = dim1 * dim2
arr_size = [[[0, dim1 - 1], dim2], [[0, dim2 - 1], 1]]
if p[-4] == None:
IDList = [p[-5]]
else:
IDList = list(get_IDs(p[-4]))
IDList.insert(0, p[-5])
for ID in IDList:
qtype = None
if ID == None:
continue
if find(ID, 'dec') != False:
print(f"Error! Variable '{ID}' already declared!")
quit()
if p[-2] == 'int':
qtype = (0, p[-2])
if env == 'global':
address = memory.gbli[0] + memory.gbli[1]
if address >= memory.gblf[0]:
print(f'Error! Too many global integer variables!')
quit()
memory.gbli[1] = memory.gbli[1] + arr_size[0][1] + 1 if type(arr_size[1]) == int else memory.gbli[1] + tsize
else:
address = memory.lcli[0] + memory.lcli[1]
if address >= memory.lclf[0]:
print(f'Error! Too many local integer variables!')
quit()
memory.lcli[1] = memory.lcli[1] + arr_size[0][1] + 1 if type(arr_size[1]) == int else memory.lcli[1] + tsize
elif p[-2] == 'float':
qtype = (1, p[-2])
if env == 'global':
address = memory.gblf[0] + memory.gblf[1]
if address >= memory.gbls[0]:
print(f'Error! Too many global float variables!')
quit()
memory.gblf[1] = memory.gblf[1] + arr_size[0][1] + 1 if type(arr_size[1]) == int else memory.gblf[1] + tsize
else:
address = memory.lclf[0] + memory.lclf[1]
if address >= memory.lcls[0]:
print(f'Error! Too many local float variables!')
quit()
memory.lclf[1] = memory.lclf[1] + arr_size[0][1] + 1 if type(arr_size[1]) == int else memory.lclf[1] + tsize
else:
qtype = (2, p[-2])
if env == 'global':
address = memory.gbls[0] + memory.gbls[1]
if address >= memory.lcli[0]:
print(f'Error! Too many global string variables!')
quit()
memory.gbls[1] = memory.gbls[1] + arr_size[0][1] + 1 if type(arr_size[1]) == int else memory.gbls[1] + tsize
else:
address = memory.lcls[0] + memory.lcls[1]
if address >= memory.tmpi[0]:
print(f'Error! Too many local string variables!')
quit()
memory.lcls[1] = memory.lcls[1] + arr_size[0][1] + 1 if type(arr_size[1]) == int else memory.lcls[1] + tsize
new_var = var_object(ID, qtype, address, arr_size)
variables.append(new_var)
def p_neuralgic_opr(p):
'''
neuralgic_opr :
'''
S.Operators.append(p[-1])
def p_neuralgic_int(p):