-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
112 lines (102 loc) · 3.2 KB
/
main.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
import eel
import logging
from time import sleep
from core.cmp.visitors import *
from core.cmp.evaluation import *
def build_AST(G, text):
data, err = [], False
ast = None
txt = '================== TOKENS =====================\n'
tokens = tokenize_text(text)
txt += format_tokens(tokens)
data.append(txt)
txt = '=================== PARSE =====================\n'
#print(parser([t.token_type for t in tokens], get_shift_reduce=True))
try:
parse, operations = CoolParser([t.token_type for t in tokens], get_shift_reduce=True)
except:
err = True
txt += 'Impossible to parse\n'
#print('\n'.join(repr(x) for x in parse))
data.append(txt)
if not err:
txt = '==================== AST ======================\n'
ast = evaluate_reverse_parse(parse, operations, tokens)
formatter = FormatVisitor()
tree = formatter.visit(ast)
txt += str(tree)
data.append(txt)
return ast, '\n\n'.join(data)
def error_formatter(errors):
txt = 'Errors: [\n'
for error in errors:
txt += f'\t{error}\n'
txt += ']\n'
return txt
def run_pipeline(G, text):
data, err = [], False
ast, txt = build_AST(G, text)
errors = context = scope = None
data.append(txt)
if ast:
txt = '============== COLLECTING TYPES ===============\n'
errors = []
collector = TypeCollector(errors)
collector.visit(ast)
context = collector.context
if len(errors):
txt += error_formatter(errors)
err = True
txt += 'Context:\n'
txt += str(context)
data.append(txt)
errors.clear()
txt = '=============== BUILDING TYPES ================\n'
builder = TypeBuilder(context, errors)
builder.visit(ast)
if len(errors):
txt += error_formatter(errors)
err = True
errors.clear()
data.append(txt)
txt = '=============== CHECKING TYPES ================\n'
checker = TypeChecker(context, errors)
scope = checker.visit(ast)
if len(errors):
txt += error_formatter(errors)
err = True
errors.clear()
data.append(txt)
txt = '=============== INFERING TYPES ================\n'
inferer = InferenceVisitor(context, errors)
while True:
old = scope.count_auto()
scope = inferer.visit(ast)
if old == scope.count_auto():
break
errors.clear()
scope = inferer.visit(ast)
if len(errors):
txt += error_formatter(errors)
err = True
errors.clear()
txt += 'Context:\n'
txt += str(context) + '\n'
formatter = ComputedVisitor()
if not err:
tree = formatter.visit(ast)
txt += 'AST:\n' + str(tree)
data.append(txt)
return '\n\n'.join(data)
@eel.expose
def compile(text):
sleep(2)
return run_pipeline(CoolGrammar, text)
def main():
eel.init('web')
eel_options = {'port': 8045}
eel.start('index.html', size=(1000, 860), options=eel_options, block=False)
while True:
eel.sleep(0.1)
if __name__ == '__main__':
main()