forked from nicolaskribas/DFA-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cod_int.py
81 lines (68 loc) · 1.95 KB
/
cod_int.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
codigo = []
asm = []
TS = []
def geracod(i):
cont = 0
var = i
i = i + 2
while 1:
if TS[i][2] == '*' or TS[i][2] == '/':
codigo.append(['T'+str(cont),'=',TS[i-1][2],TS[i][2],TS[i+1][2]])
del(TS[i-1])
del(TS[i-1])
TS[i-1][2] = 'T'+str(cont)
cont = cont + 1
i = var + 1
elif TS[i][2] == ';':
break
i = i+1
i = var + 2
while 1:
if TS[i][2] == '+' or TS[i][2] == '-':
codigo.append(['T'+str(cont),'=',TS[i-1][2],TS[i][2],TS[i+1][2]])
del(TS[i-1])
del(TS[i-1])
TS[i-1][2] = 'T'+str(cont)
cont= cont + 1
i = var + 1
elif TS[i][2] == ';':
break
i = i+1
if cont == 0:
codigo.append([TS[var][2],'=',TS[var+2][2]])
else:
codigo.append([TS[var][2],'=',codigo[len(codigo)-1][0]])
print("Código:")
print(codigo)
def geraASM():
for i in range(len(codigo)):
asm.append(['LOAD',codigo[i][2]])
if len(codigo[i]) > 3:
if codigo[i][3] == '*':
asm.append(['MULT',codigo[i][4]])
elif codigo[i][3] == '/':
asm.append(['DIV',codigo[i][4]])
elif codigo[i][3] == '+':
asm.append(['ADD',codigo[i][4]])
elif codigo[i][3] == '-':
asm.append(['SUB',codigo[i][4]])
asm.append(['STR',codigo[i][0]])
print("Código de maquina:")
print(asm)
with open('Cod_intermediario.txt', 'w') as arquivo:
for valor in asm:
for v in valor:
arquivo.write(v + ' ')
arquivo.write('\n')
arquivo.close()
arquivo = open('TS.txt','r')
for linha in arquivo:
linha = linha.rstrip('\n')
aux = linha.split(' ')
TS.append(aux)
arquivo.close()
for i in range(len(TS)):
if TS[i][3] == 'opvar':
geracod(i)
break
geraASM()