-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.py
More file actions
executable file
·33 lines (29 loc) · 1.34 KB
/
Interpreter.py
File metadata and controls
executable file
·33 lines (29 loc) · 1.34 KB
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
from Token import Token
from Constants import Token_type
def interpret(tokens: list):
for line_index,token_line in enumerate(tokens):
# Calculate all calculationes
for index,token in enumerate(token_line):
try:
if token.token_type == Token_type.ADD:
token_line[index] = Token(Token_type.INTEGER,token_line[index-1].value+token_line[index+1].value)
elif token.token_type == Token_type.SUB:
token_line[index] = Token(Token_type.INTEGER,token_line[index-1].value-token_line[index+1].value)
elif token.token_type == Token_type.MUL:
token_line[index] = Token(Token_type.INTEGER,token_line[index-1].value*token_line[index+1].value)
elif token.token_type == Token_type.DIV:
token_line[index] = Token(Token_type.INTEGER,token_line[index-1].value/token_line[index+1].value)
else:
continue
token_line.pop(index+1)
token_line.pop(index-1)
except:
raise Exception('Error in Line {line} near {token_value}'.
format(line=line_index,token_value=token.value))
#
print('CALCULATED TOKENS:')
for line in tokens:
print(line)
print('')
print('='*100)
print('')