-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
108 lines (89 loc) · 2.71 KB
/
main.py
File metadata and controls
108 lines (89 loc) · 2.71 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
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
import sys
import pathlib
from libs import tokenizer, parser, interpreter, resolver
def castNonetoNil(value):
if value is None:
return "nil"
return str(value)
def remove_trailing_zeros(number_str):
try:
number = float(number_str)
result = ('{:.10f}'.format(number)).rstrip('0').rstrip('.')
return result
except:
return number_str
def flatten(lst):
flat_list = []
for item in lst:
if isinstance(item, list):
flat_list.extend(flatten(item))
else:
flat_list.append(item)
return flat_list
def main():
command = sys.argv[1]
filename = sys.argv[2]
file_contents = pathlib.Path(filename).read_text()
scanner = tokenizer.Scanner(file_contents)
tokens, errors = scanner.scan_tokens()
parse = parser.Parser(tokens)
_interpreter = interpreter.Interpreter()
_resolver = resolver.Resolver(_interpreter)
if command == "tokenize":
for token in tokens:
print(token)
for error in errors:
print(error, file=sys.stderr)
if errors:
exit(65)
else:
exit(0)
elif command == "parse":
if errors:
for error in errors:
print(error, file=sys.stderr)
exit(65)
ast = parse.parse()
if len(ast) == 0:
exit(65);
printer = parser.AstPrinter()
for stmt in ast:
print(printer.print(stmt))
elif command == "evaluate":
ast = parse.parse()
print("EVAL: ", ast)
if len(ast) == 0 or parse.has_errors:
print("I AM FUCKED")
exit(65)
try:
for stmt in ast:
eval = _interpreter.evaluate(stmt)
print("EVAL: ", remove_trailing_zeros(eval))
except Exception as e:
print(e, file=sys.stderr)
exit(70)
elif command == "run":
ast = parse.parse()
printer = parser.AstPrinter()
if len(ast) == 0 or parse.has_errors:
exit(65)
_resolver.resolve(ast)
if _resolver.has_error:
exit(65)
try:
for stmt in ast:
_interpreter.run(stmt)
# if isinstance(result, list):
# _result = flatten(result)
# for r in _result:
# print(remove_trailing_zeros(r))
# else:
# if result is not None:
# print(remove_trailing_zeros(result))
except Exception as e:
print(e, file=sys.stderr)
exit(70)
else:
print("Wrong command")
if __name__ == "__main__":
main()