-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.py
177 lines (150 loc) · 4.8 KB
/
interpreter.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
import sys
from stack import Stack
from if_statement import IfBlock
# ==== Acessando o arquivo e coletando o conteúdo. ====
path = sys.argv[1]
if not path.endswith(".limpil"):
print("Você não selecionou um arquivo .limpil.")
sys.exit(0)
lines = list()
with open(path, "r") as file:
lines = [line.strip() for line in file.readlines()]
# ==== Separando os comandos para interpretação. ====
program = list()
token_counter = 0
label_tracker = {}
if_blocks = list()
if_block_open = None
line_counter = 0
for line in lines:
line_counter += 1
splitted_command = line.split(" ")
opcode = splitted_command[0]
if opcode == "":
continue
if opcode.startswith("--"): # Comentários
continue
if opcode.endswith(":"):
label_tracker[opcode[:-1]] = token_counter
continue
program.append(opcode)
token_counter += 1
if opcode == "ADICIONE.INTEIRO":
number = int(splitted_command[1])
program.append(number)
token_counter += 1
elif opcode == "SE":
if if_block_open is not None:
raise SyntaxError("Erro de sintaxe. Bloco SE iniciado, mas não fechado com SE.FIM")
if splitted_command[1][-1] != ":":
raise SyntaxError("Comando SE com a sintaxe incorreta. Se esqueceu do ':'?")
dataCheck = splitted_command[1][:-1]
if dataCheck[0] == '"':
block = IfBlock(value = dataCheck[1:-1], startLine=line_counter)
elif dataCheck.__contains__("."):
block = IfBlock(value = float(dataCheck), startLine=line_counter)
else:
block = IfBlock(value = int(dataCheck), startLine=line_counter)
if_block_open = block
elif opcode == "SE.FIM":
if if_block_open is None:
raise SyntaxError("Erro, bloco SE não foi inicializado!")
if_block_open.endLine = line_counter
if_blocks.append(if_block_open)
if_block_open = None
elif opcode == "ADICIONE.DECIMAL":
number = float(splitted_command[1])
program.append(number)
token_counter += 1
elif opcode == "IMPRIMA":
string = " ".join((splitted_command[1:]))[1:-1]
program.append(string)
token_counter += 1
elif opcode == "PULE.SE.IGUAL.ZERO":
label = splitted_command[1]
program.append(label)
token_counter += 1
elif opcode == "PULE.SE.MAIORQUE.ZERO":
label = splitted_command[1]
program.append(label)
token_counter += 1
# ==== Interpretando os comandos ====
stack = Stack(256)
pc = 0 # program_counter
if_counter = 0
if_content_skip = False
while program[pc] != "PARE":
opcode = program[pc]
pc += 1
if opcode == "SE.FIM":
if_content_skip = False
if if_content_skip is True:
continue
if opcode == "ADICIONE.INTEIRO":
number = program[pc]
pc += 1
stack.push(number)
elif opcode == "ADICIONE.DECIMAL":
number = program[pc]
pc += 1
stack.push(number)
elif opcode == "RETIRE":
stack.pop()
elif opcode == "RETIRE.E.IMPRIMA":
number = stack.pop()
print(number)
elif opcode == "SOMA":
a = stack.pop()
b = stack.pop()
stack.push(a+b)
elif opcode == "DIFERENCA":
a = stack.pop()
b = stack.pop()
stack.push(b-a)
elif opcode == "MUL":
a = stack.pop()
b = stack.pop()
stack.push(a*b)
elif opcode == "DIV":
a = stack.pop()
b = stack.pop()
stack.push(b/a)
elif opcode == "POTENCIA.QUADRADO":
a = stack.pop()
stack.push(a * a)
elif opcode == "IMPRIMA":
string_literal = program[pc]
pc += 1
print(string_literal)
elif opcode == "TOPO":
stack.top()
elif opcode == "LER.STRING":
string = str(input())
stack.push(string)
elif opcode == "LER.INTEIRO":
number = int(input())
stack.push(number)
elif opcode == "LER.DECIMAL":
number = float(input())
stack.push(number)
elif opcode == "PULE.SE.IGUAL.ZERO":
number = stack.top()
if number == 0:
pc = label_tracker[program[pc]]
else:
pc += 1
elif opcode == "PULE.SE.MAIORQUE.ZERO":
number = stack.top()
if number > 0:
pc = label_tracker[program[pc]]
else:
pc += 1
elif opcode == "SE":
if if_counter > len(if_blocks):
raise SystemError("if_counter maior que if_blocks. Erro do programa.")
block = if_blocks[if_counter]
if block.value == stack.top(): # Se a condição for correta, continue
if_counter += 1
else: # Caso contrário, pule para o fim do bloco.
if_counter += 1
if_content_skip = True