-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
61 lines (49 loc) · 1.9 KB
/
pipeline.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
from cmp.evaluation import evaluate_reverse_parse
from cmp.tools.LR1_Parser import LR1Parser
from format_visitor import FormatVisitor
from grammar import lexer
from inference_gatherer import InferenceGatherer
from type_collector_builder import TypeBuilder, TypeCollector
from type_inferencer import TypeInferencer
from type_linker import TypeLinker
class Pipeline():
def __init__(self, G) -> None:
self.parser = LR1Parser(G)
self.tree = None
def __call__(self, program):
tokens = lexer(program)
parse, operations, result = self.parser(tokens)
if not result:
return parse
ast = evaluate_reverse_parse(parse, operations, tokens)
formatter = FormatVisitor()
self.tree = formatter.visit(ast)
collector = TypeCollector()
collector.visit(ast)
context = collector.context
builder = TypeBuilder(context)
builder.visit(ast)
gatherer = InferenceGatherer(context)
scope = gatherer.visit(ast)
change = True
inferencer = TypeInferencer(context)
while change:
change = inferencer.visit(ast, scope)
linker = TypeLinker(context)
linker.visit(ast, scope)
s = "Type Collector Errors:\n"
s = self.format_errors(collector.errors, s)
s += "Type Builder Errors:\n"
s = self.format_errors(builder.errors, s)
s += "Type Linker Errors:\n"
s = self.format_errors(linker.errors, s)
s += "Total Errors: " + str(len(collector.errors) + len(builder.errors) + len(linker.errors))
for auto, typex in linker.inferenced:
s += "Inferenced " + typex.name + " from " + auto + "\n"
return s
def format_errors(self, errors, s = ""):
count = 1
for error in errors:
s += str(count) + ". " + error + "\n"
count += 1
return s