-
Notifications
You must be signed in to change notification settings - Fork 0
/
trst
executable file
·45 lines (37 loc) · 1.34 KB
/
trst
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
#!/usr/bin/python
# -*- coding: latin1 -*-
import argparse
from triestin import Tokenizer, Parser, Generator, State
def triestin(**kwargs):
tokens = Tokenizer(**kwargs).tokenize()
# for i in tokens:
# print(i)
tree = Parser(tokens).parse()
# print(tree)
return Generator().generate(tree)
# print(triestin(filename='trst.src'))
if __name__=="__main__":
print("Triestin v0.0.1")
print("ctrl+C per uscire")
local_state = State()
while True:
try:
# Python 2 on CLI
user_input = raw_input('triestin> ')
output = triestin(source_code=user_input)
local_state.add_history(output)
if output['state'] == 'exec':
exec(output['code'])
if output['state'] == 'eval':
if type(eval(output['code'])) == tuple:
print(eval(output['code'])[0])
else:
print(eval(output['code']))
if output['state'] == 'anzi':
if local_state.history[-2]['node'] == 'AssignNode':
prev_node = local_state.history[-2]
prev_var = prev_node['code'].split('=')[0].strip()
new_code = '{} = {}'.format(prev_var, output['code'])
exec(new_code)
except Exception as e:
print(e)