-
Notifications
You must be signed in to change notification settings - Fork 0
/
Traductor2B.py
1067 lines (917 loc) · 28.2 KB
/
Traductor2B.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 sys
sys.path.append('../..')
from sly import Lexer, Parser
#usar $ tanto para & como para *
tabla = {}
tablaGlobal = {}
contenedor = {} #Diccionario con todas las tablas.
labels = 0
labelsprintf=0
freememory=0
nivel=1
varprintf=[]
valueprintf=[]
labelsif=[]
endlabelswhile=[]
labelswhile=[]
tablaVectores={}
decla=False
asig=False
eax=False
prinfvar=0
printfstring=""
longitud=[]
vectores={}
vector=False
counterparameter = 0
declavar={}
declavalue=[]
f=open("Ensamblador.S","w+")
def incrementparameter():
global counterparameter
counterparameter += 4
def resetparameter():
global counterparameter
counterparameter = 0
def stackvariable():
global decla, declavalue, declavar, tabla, longitud, tablaVectores, vectores
decla = False
longitud.reverse()
for i in range(0, len(declavar)):
value = declavalue.pop()
var = declavar[value]
tam = longitud.pop()
if tam >= 1:
vectores[value] = tam
if var == None:
manejador.insertVar(value, 0, tam, 0) # ID, valor
NodoVariable(tam)
else:
if type(var) is int:
manejador.insertVar(value, var, tam, 0) # ID, valor
NodoVariable(tam)
f.write("movl $" + str(var) + ", %eax\n")
f.write("movl %eax, " + str(tabla[value][0]) + "(%ebp)\n")
else:
if type(value) is str:
try:
manejador.insertVar(value, declavar[var], tam, 0) # ID, valor
NodoVariable(tam)
f.write("movl $" + str(declavar[var]) + ", %eax\n")
f.write("movl %eax, " + str(tabla[value][0]) + "(%ebp)\n")
except:
manejador.insertVar(value, tabla[var][1], tam, 0) # ID, valor
NodoVariable(tam)
if (tam > 0):
f.write("movl " + str(tabla[var][0]) + "(%ebp), " + str(tabla[value][0]) + "(%ebp)\n")
else:
manejador.insertVar(var, 0, tam, 0) # ID, valor
NodoVariable(tam)
vector = False
declavar.clear()
del declavalue[:]
del longitud[:]
class Tabla():
contador = 0
def aumentarcontador(self):
self.contador-=4
def insertVar(self, var, value,tam, ambito):
if ambito == 0: #Ambito local
if var in tabla:
raise NameError('Variable ' + "\"" + str(var) + "\"" + ' declarada anteriormente')
else:
if int(tam)<=1:
self.contador-= 4
tabla[var] = [self.contador, value]
else:
for i in range(0,int(tam)):
self.contador-= 4
if i==0:
tabla[var] = [self.contador, 0]
else:
tabla[var+str(i)]=[self.contador, 0]
else:
if var in tablaGlobal:
raise NameError('Variable ' + "\"" + str(var) + "\"" + ' declarada anteriormente')
else:
if int(tam)<=1:
self.contador-= 4
tablaGlobal[var] = [self.contador, value]
else:
for i in range(0,int(tam)):
self.contador-= 4
if i==0:
tablaGlobal[var] = [self.contador, 0]
else:
tablaGlobal[var+str(i)]=[self.contador, 0]
def insertValue(self, var, value,tam):
if var in tabla:
if tam<=-1:
tabla[var][1] = value #Modificamos el campo valor de la variable.
else:
if tam==0:
tabla[var][1] = value
else:
tabla[var+str(tam)][1] = value
else: #Si no esta en la función puede ser una función global.
if var in tablaGlobal:
if tam <= -1:
tablaGlobal[var][1] = value # Modificamos el campo valor de la variable.
else:
if tam == 0:
tablaGlobal[var][1] = value
else:
tablaGlobal[var + str(tam)][1] = value
else:
raise NameError('Variable ' + "\"" + str(var) + "\"" + 'no declarada')
def compVar(self, var):
if var in tabla:
return 't'
else:
return 'f'
def cadena(self, var):
if var in tabla:
res = str(tabla[var][0]) + "(%ebp)"
else:
res = "$" + str(var)
return res
manejador = Tabla()
def incrementLabel():
global labels
labels = labels + 1
#print('L' + str(labels) + ':')
def decrementLabel(): #Lo uso en el while.
global labels
labels = labels - 1
class NodoVariable():
def __init__(self,tam):
tam=int(tam)
if tam==-1:
tam=1
f.write("subl $"+str(4*tam)+", %esp\n")
class NodoAsign():
def __init__(self, variable, valor,tam):
if tam<=-1:
if valor == None:
f.write("movl %eax, " + str(tabla[variable][0]) + "(%ebp)\n")
else:
if type(valor) is str:
f.write("movl "+str(tabla[variable][0])+"(%ebp) ,"+str(tabla[variable][0]) + "(%ebp)\n")
else:
if eax:
f.write("movl %eax ," + str(tabla[variable][0]) + "(%ebp)\n")
else:
f.write("movl $" + str(valor) + ", " + str(tabla[variable][0]) + "(%ebp)\n")
else:
if tam>0:
variable=variable+str(tam)
if valor == None:
f.write("movl %eax, " + str(tabla[variable][0]) + "(%ebp)\n")
else:
if type(valor) is str:
f.write("movl "+str(tabla[variable][0])+"(%ebp) ,"+str(tabla[variable][0]) + "(%ebp)\n")
else:
if eax:
f.write("movl %eax ," + str(tabla[variable][0]) + "(%ebp)\n")
else:
f.write("movl $" + str(valor) + ", " + str(tabla[variable][0]) + "(%ebp)\n")
class NodoSuma():
def __init__(self):
f.write("addl %ecx, %eax\n")
#f.write("movl %eax, " + str1)
class NodoResta():
def __init__(self):
f.write("subl %ecx, %eax\n")
class NodoProducto():
def __init__(self):
f.write("imull %ecx, %eax\n")
class NodoDivision():
def __init__(self):
f.write("cdq\n")
f.write("divl %ecx\n")
class NodoEqual():
def __init__(self):
global labels
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp %edx, %ecx\n")
f.write("jne final"+str(labels)+"\n")
f.write("movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoNequal():
def __init__(self):
global labels
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp %edx, %ecx\n")
f.write("je final"+str(labels)+"\n")
f.write("movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoGreater():
def __init__(self):
global labels
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp %edx, %ecx\n")
f.write("jle final"+str(labels)+"\n")
f.write("movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoSmaller():
def __init__(self):
global labels
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp %edx, %ecx\n")
f.write("jg final"+str(labels)+"\n")
f.write(" movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoSmallerEqual():
def __init__(self):
global labels
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp %edx, %ecx\n")
f.write("jg final"+str(labels)+"\n")
f.write(" movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoGreaterEqual():
def __init__(self):
global labels
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp %edx, %ecx\n")
f.write("jl final"+str(labels)+"\n")
f.write(" movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoNeg():##Problema con esto
def __init__(self):
global eax
if eax:
f.write("negl %eax\n")
else:
f.write("negl %ecx\n")
class NodoNot():#aqui
def __init__(self):
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp $0, %edx\n")
f.write("jne final"+str(labels)+"\n")
f.write("movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoAnd():
def __init__(self):
f.write("movl %eax,%edx\n")
f.write("movl $0,%eax\n")
f.write("cmp $0, %edx\n")
f.write("je final"+str(labels)+"\n")
f.write("movl %ecx,%edx\n")
f.write("cmp $0, %edx\n")
f.write("je final"+str(labes)+"\n")
f.write("movl $1, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoOr():
def __init__(self):
f.write("movl %eax,%edx\n")
f.write("movl $1,%eax\n")
f.write("cmp $0, %edx\n")
f.write("jne final"+str(labels)+"\n")
f.write("movl %ecx,%edx\n")
f.write("cmp $0, %edx\n")
f.write("jne final"+str(labels)+"\n")
f.write("movl $0, %eax\n")
f.write("final"+str(labels)+":\n")
incrementLabel()
class NodoWhile():
def __init__(self):
global endlabelswhile,labels
f.write("cmp $0,%eax\n")
f.write("jne final"+str(labels)+"\n")
endlabelswhile.append(labels)
incrementLabel()
class NodoIf():
def __init__(self):
global labels,labelsif
f.write("cmp $0,%eax\n")
f.write("je final"+str(labels)+"\n")
labelsif.append(labels)
incrementLabel()
class NodoLogic():
def __init__(self, valor1, op, valor2):
self. valor1 = valor1
self.op = op
self.valor2 = valor2
def escribir(self):
#Averiguar en que posicion esta la variable que buscas.
f.write( "cmp " + manejador.cadena(self.valor2) + "," + manejador.cadena(self.valor1)+"\n")
def salto(self):
global labels
if self.op == '!=':
f.write("je L" + str(labels + 1)+"\n")
if self.op == '==':
f.write("jne L" + str(labels + 1)+"\n")
if self.op == '<':
f.write("jl L" + str(labels + 1)+"\n")
if self.op == '>':
f.write("jg L" + str(labels + 1)+"\n")
if self.op == '<=':
f.write("jle L" + str(labels + 1)+"\n")
if self.op == '>=':
f.write("jge L" + str(labels + 1)+"\n")
class beginFunction():
def __init__(self, nombre):
#resetparameter()
f.write('.text\n')
f.write('.globl ' + nombre+"\n")
f.write('.type ' + nombre + ", @function\n")
f.write(nombre + ":\n")
f.write('pushl %ebp\n')
manejador.aumentarcontador()
f.write('movl %esp, %ebp\n')
class endFunction():
def __init__(self):
f.write('movl %ebp, %esp\n')
f.write('popl %ebp\n')
f.write('ret\n')
class CalcLexer(Lexer):
tokens = {ID, TIPO, NUM, PLUS, MINUS, TIMES, DIVIDE, ASSIGN, LPAREN, RPAREN, EQUAL, NEQUAL ,GREATER,OR,AND,
IF, ELSE, WHILE, LKEY, RKEY, COMA, END, LESS, BIGGEROREQUAL, LESSOREQUAL, MAIN, RETURN,RCORCHERTE,LCORCHETE,DIRECC,PRINTF,CADENA,SCANF}
ignore = ' \t'
# Tokens
ID = r'[a-zA-Z_][a-zA-Z0-9_]*'
CADENA =r'"[a-zA-Z0-9% ]*"' # COMPROBAR ESTO
ID['int'] = TIPO
ID['void'] = TIPO
ID['if'] = IF
ID['else'] = ELSE
ID['while'] = WHILE
#ID['for']=FOR
ID['main'] = MAIN
ID['return'] = RETURN
ID['printf'] = PRINTF
ID['scanf']=SCANF
NUM = r'\d+'
#Aritmetic
AND=r'&&'
OR=r'\|\|'
PLUS = r'\+'
MINUS = r'-'
TIMES = r'\*'
DIVIDE = r'/'
BIGGEROREQUAL = r'>='
LESSOREQUAL = r'<='
EQUAL = r'=='
NEQUAL = r'!='
GREATER = r'>'
LESS = r'<'
ASSIGN = r'='
LPAREN = r'\('
RPAREN = r'\)'
LKEY = r'{'
RKEY = r'}'
COMA = r','
END = r';'
RCORCHERTE=r'\]'
LCORCHETE=r'\['
DIRECC=r'&'
# Ignored pattern
ignore_newline = r'\n+'
# Extra action for newlines
def ignore_newline(self, t):
self.lineno += t.value.count('\n')
def error(self, t):
print("Illegal character '%s'" % t.value[0])
self.index += 1
class CalcParser(Parser):
tokens = CalcLexer.tokens
#manejador = Tabla()
#precedence = (
# ('left', PLUS, MINUS),
# ('left', TIMES, DIVIDE),
# ('right', UMINUS)
# )
def __init__(self):
self.names = { }
"""
@_('declaraciontipo END jolin')
def main_f(self, p):
pass
@_(' ')
def jolin(self, p):
print("Zopotamadreeee")
"""
@_('TIPO MAIN begin LPAREN RPAREN LKEY entrada RKEY empty11 main_f')
def main_f(self, p):
pass
@_('TIPO ID begin LPAREN parametro reserva RPAREN LKEY entrada RKEY empty11 main_f') #Funcion estándar
def main_f(self, p):
pass
@_(' ')
def main_f(self, p):
pass
@_('')
def empty11(self,p):
endFunction()
@_(' ')
def begin(self, p):
global tabla
#manejador.tabla = {}
tabla = {}
#f.write(p[-1]) #Hell yeah, accedemos al principio.
beginFunction(p[-1])
@_('TIPO ID resto')
def parametro(self, p): #PEEERFECTO!!!!!
global declavalue, declavar, longitud
print("GUARDANDO EN LA TABLA" + str(p.ID) + " = " + str(p[-1]))
declavar[p.ID] = 1
declavalue.append(p.ID)
longitud.append(1);
#f.write('incrementado parametro ' + p.ID)
incrementparameter()
@_(' ')
def parametro(self, p):
pass
@_('COMA parametro')
def resto(self, p):
pass
@_(' ')
def resto(self, p):
pass
@_(' ')
def reserva(self, p):
global counterparameter
if counterparameter != 0:
f.write('subl $' + str(counterparameter) + ", %esp\n")
resetparameter()
stackvariable()
@_('asignacion END entrada')
def entrada(self,p):
pass
@_('declaraciontipo END entrada')
def entrada(self,p):
pass
@_('instruction entrada')
def entrada(self,p):
pass
@_('imprimir END entrada')
def entrada(self,p):
pass
@_('escanear END entrada')
def entrada(self,p):
pass
@_('PRINTF LPAREN CADENA empty9 idextra empty8 RPAREN')
def imprimir(self,p):
global varprintf,asig,freememory
imprimir=p.CADENA
imprimir=imprimir.split("%d")
printfstring=""
for i in range(0,len(imprimir)):
printfstring=printfstring+imprimir[i]
if varprintf:
v=varprintf.pop()
if type(v) is int:
printfstring=printfstring+str(v)
else:
printfstring=printfstring+str((tabla[v][1]))
#print(printfstring)
del varprintf[:]
asig=False
@_('SCANF LPAREN CADENA empty9 idextra empty10 RPAREN')
def escanear(self,p):
pass
@_(' COMA basico idextra')
def idextra(self,p):
global varprintf
varprintf.append(p.basico)
@_(' COMA DIRECC basico idextra')
def idextra(self,p):
global varprintf
varprintf.append(p.basico)
@_(' ')
def idextra(self,p):
pass
@_('')
def empty8(self,p):
global labelsprintf,varprintf,tabla,freememory
for i in range(0,len(varprintf)):
freememory+=1
if type(varprintf[i]) is int:
f.write("pushl "+str(varprintf[i])+"(%ebp)\n")
else:
f.write("pushl "+str(tabla[varprintf[i]][0])+"(%ebp)\n")
f.write("pushl $s"+str(labelsprintf)+"\n")
f.write("call printf\n")
f.write("addl $"+str(4*freememory)+" ,esp\n")
f.seek(0,0)
line=f.read()
f.seek(0,0)
f.write("\n")
f.write(".s"+str(labelsprintf)+":\n")
f.write(".string "+p[-3]+":\n")
f.write(".section .rodata")
f.write("\n")
f.write("\n")
f.write(line)
labelsprintf=labelsprintf+1
freememory=0
@_('')
def empty10(self,p):
global labelsprintf,varprintf,tabla,freememory
for i in range(0,len(varprintf)):
freememory+=1
f.write("leal "+str(tabla[varprintf[i]][0])+"(ebp), %eax\n")
f.write("pushl %eax\n")
f.write("pushl $s"+str(labelsprintf)+"\n")
f.write("call scanf\n")
f.write("addl $"+str(4*freememory)+" ,esp\n")
f.seek(0,0)
line=f.read()
f.seek(0,0)
f.write("\n")
f.write(".s"+str(labelsprintf)+":\n")
f.write(".string "+p[-3]+":\n")
f.write(".section .rodata")
f.write("\n")
f.write("\n")
f.write(line)
labelsprintf=labelsprintf+1
freememory=0
@_('')
def empty9(self,p):
global asig
asig=True
@_('')
def entrada(self,p):
pass
@_('TIPO empty5 declaracion')
def declaraciontipo(self,p):
stackvariable()
@_('ID dim valordec empty6 restodeclaracion')
def declaracion(self,p):
global longitud
if int(p.dim)>1 and p.valordec!=None:
raise NameError('Inicializacion de vector erronea')
longitud.append(p.dim)
@_('')
def empty5(self,p):
global decla,nivel
if nivel!=1:
raise NameError("Declaracion anidada")
decla=True
@_('ID dimasig valorasig empty7 restoasignacion')#falta empty6
def asignacion(self, p): #Falta buscar el valor en el sistema
pass
@_(' ')
def empty7(self,p):
global eax,asig,decla,vectores
tam=-1
if int(p[-2])>-1:
v=vectores[p[-3]]
if v!=None:
if int(p[-2])>v or int(p[-2])<0:
raise NameError('Sobrepasada la longitud del vector '+p[-3])
else:
tam=int(p[-2])
else:
raise NameError('Variable '+p[-3]+' no es un vector')
asig=True
manejador.insertValue(p[-3], p[-1],tam)
NodoAsign(p[-3], p[-1],tam)
asig=False
eax=False
@_('ASSIGN logic dimasig')
def valordec(self,p):
if p.dimasig>-1:
if p.dimasig==0:
return tabla[p[-5]][1]
else:
return tabla[p[-5]+str(p.dimasig)][1]
else:
return p.logic
@_('')
def valordec(self,p):
pass
@_('ASSIGN logic dimasig')
def valorasig(self,p):
if p.dimasig>1:
if p.dimasig==0:
return tabla[p[-5]][1]
else:
return tabla[p[-5]+str(p.dimasig)][1]
else:
return p.logic
@_('')
def valorasig(self,p):
pass
@_('LCORCHETE NUM RCORCHERTE dim')
def dim(self,p):
return int(p.NUM)*p.dim
@_('')
def dim(self,p):
return 1
@_('LCORCHETE NUM RCORCHERTE dimasig')
def dimasig(self,p):
if int(p.NUM)==1 and p.dimasig==-1:
return 1
else:
return int(p.NUM)*p.dimasig
@_('')
def dimasig(self,p):
return -1
@_(' ')
def empty6(self,p):
global declavalue,declavar
declavar[p[-3]]=p[-1]
declavalue.append(p[-3])
@_('COMA asignacion ')
def restoasignacion(self,p):
pass
@_('')
def restoasignacion(self,p):
global asig
asig=False
@_('COMA declaracion ')
def restodeclaracion(self,p):
pass
@_('')
def restodeclaracion(self,p):
pass
@_('ID LPAREN argumentos RPAREN END')
def instruction(self, p):
global counterparameter
f.write('call ' + p.ID+"\n")
f.write('addl ' + str(counterparameter) + ", %esp\n")
resetparameter()
@_(' ')
def argumentos(self, p):
pass
@_('ID restoargumentos')
def argumentos(self, p):
f.write("pushl " + manejador.cadena(p.ID)+"\n")#p.ID)
incrementparameter()
@_('COMA argumentos')
def restoargumentos(self, p):
pass
@_(' ')
def restoargumentos(self, p):
pass
@_('RETURN logic END')
def instruction(self, p):
pass
@_('WHILE LPAREN empty1 empty2 entrada RKEY')
def instruction(self, p):
global labels,endlabelswhile,nivel
f.write('jmp final' + str(labelswhile.pop())+"\n")
f.write("final"+str(endlabelswhile.pop())+":\n")
incrementLabel()
global eax
eax=False
nivel-=1
@_(" ")
def empty1(self, p):
global labels,labelswhile,endlabelswhile,nivel
nivel+=1
f.write("final"+str(labels)+":\n")
labelswhile.append(labels)
incrementLabel() #Imprimir la etiqueta actual
@_("logic RPAREN LKEY")
def empty2(self, p):
global eax
eax=False
NodoWhile()
#Comienzo del bucle while
#@_('WHILE LPAREN empty1 empty2 statement RKEY')
@_('IF LPAREN empty3 LKEY entrada RKEY elseif')
def instruction(self,p):
global eax,nivel
eax=False
nivel-=1
@_('logic RPAREN')
def empty3(self,p):
global eax,nivel
nivel+=1
eax=False
NodoIf()
@_(' ')
def elseif(self,p):
global labels,labelsif
f.write("final"+str(labelsif.pop())+":\n")
incrementLabel()
@_('empty4 entrada RKEY')
def elseif(self,p):
global labels,labelsif
f.write("final"+str(labelsif.pop())+":\n")
@_("ELSE LKEY")
def empty4(self,p):
global labels,labelsif
f.write("jmp final"+str(labels)+"\n")
f.write("final"+str(labelsif.pop())+":\n")
labelsif.append(labels)
incrementLabel()
@_('logic EQUAL logicand')
def logic(self, p):
NodoEqual()
if type(p.logic) is str:
return tabla[p.logic][1]==p.logicand
else:
return p.logic == p.logicand
#return NodoLogic(p.logic, p.EQUAL, p.logicpr)
@_('logic NEQUAL logicand')
def logic(self, p):
NodoNequal()
if type(p.logic) is str:
return tabla[p.logic][1]!=p.logicand
else:
return p.logic != p.logicand
@_('logicand')
def logic(self, p):
return p.logicand #Perfe, debería subir el nodo.
@_('logicand AND logicor')
def logicand(self, p):
NodoAnd()
if type(p.logicand) is str:
return tabla[p.logicand][1]<=p.logicor
else:
return p.logicand <= p.logicor
@_('logicor')
def logicand(self,p):
return p.logicor
@_('logicor OR logicpr')
def logicor(self,p):
NodoOr()
if type(p.logicor) is str:
return tabla[p.logicor][1]<=p.logicpr
else:
return p.logicor <= p.logicpr
@_('logicpr')
def logicor(self,p):
return p.logicpr
@_('logicpr LESSOREQUAL expr')
def logicpr(self, p):
NodoSmallerEqual()
if type(p.logicpr) is str:
return tabla[p.logicpr][1]<=p.pexpr
else:
return p.logicpr <= p.expr
@_('logicpr BIGGEROREQUAL expr')
def logicpr(self, p):
NodoGreaterEqual()
if type(p.logicpr) is str:
return tabla[p.logicpr][1]>=p.pexpr
else:
return p.logicpr >= p.expr
@_('logicpr GREATER expr')
def logicpr(self, p):
NodoGreater()
if type(p.logicpr) is str:
return tabla[p.logicpr][1]>p.pexpr
else:
return p.logicpr > p.expr
@_('logicpr LESS expr')
def logicpr(self, p):
NodoSmaller()
if type(p.logicpr) is str:
return tabla[p.logicpr][1]<p.pexpr
else:
return p.logicpr < p.expr
@_('expr')
def logicpr(self, p):
return p.expr
@_('expr PLUS fact')
def expr(self, p):
global tabla,eax
NodoSuma()
if type(p.expr) is str:
return tabla[p.expr][1]+p.fact
else:
return p.expr+p.fact
@_('expr MINUS fact')
def expr(self, p):
NodoResta()
if type(p.expr) is str:
return tabla[p.expr][1]-p.fact
else:
return p.expr-p.fact #p.expr0 - p.expr1
@_('fact')
def expr(self, p):
return p.fact
@_('fact TIMES basico')
def fact(self, p):
NodoProducto()
if type(p.fact) is str:
return tabla[p.fact][1]*p.basico
else:
return p.fact*p.basico
@_('fact DIVIDE basico')
def fact(self, p):
NodoDivision()
if type(p.fact) is str:
return tabla[p.fact][1]/p.basico
else:
return p.fact/p.basico
@_('basico')
def fact(self, p):
return p.basico
@_('MINUS basico')
def basico(self, p):
NodoNeg()
return -p.basico
@_('ID dimasig') #Uso de la variable
def basico(self, p): #Si el id no esta en la tabla, debe mostrar error.
global eax,decla,asig
var=p.ID
if p.dimasig>0:
var=var+str(p.dimasig)
if not decla:
if not asig:
if eax:
f.write("movl "+str(tabla[var][0])+"(%ebp), %ecx\n")
else:
f.write("movl "+str(tabla[var][0])+"(%ebp), %eax\n")
eax=True
return tabla[var][1]
else:
if eax:
f.write("movl "+str(tabla[var][0])+"(%ebp), %ecx\n")
else:
f.write("movl "+str(tabla[var][0])+"(%ebp), %eax\n")
eax=True
asig=True
return var
else:
return var
@_('DIRECC ID dimasig')
def basico(self,p):
global eax,decla,asig
var=p.ID
if p.dimasig>0:
var=var+str(p.dimasig)
if not decla:
if not asig:
if eax:
f.write("movl "+str(tabla[var][0])+"(%ebp), %ecx\n")