-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
61 lines (46 loc) · 1.88 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
import argparse
from arc_preprocessor import preprocessor
from arc_lexer import lexer
from arc_parser import arc_parser
from arc_ast import pretty_print
from symbol_collector import SymbolCollector
from symbol_table import SymbolTable
from reference_resolver import ReferenceResolver
from semantic_analyzer import SemanticAnalyzer
#from arc_ast import build_ast, validate_ast, print_ast
def print_ast(ast):
pretty_printer = pretty_print.PrettyPrint()
ast.accept(pretty_printer)
target_functions = {
'print': print_ast
}
def main():
arg_parser = argparse.ArgumentParser(description='Compile ModelArc code')
arg_parser.add_argument('input_file', type=str, help='Input file to compile')
target_function_keys = list(target_functions.keys())
arg_parser.add_argument('--target',
type=str,
default=target_function_keys[0],
choices=target_function_keys,
help='Target output format')
args = arg_parser.parse_args()
try:
source_code = preprocessor(args.input_file)
lexer.input(source_code)
parse_tree = arc_parser.parse(source_code, lexer=lexer, tracking=True)
symbol_table = SymbolTable()
symbol_collector = SymbolCollector(symbol_table)
symbol_collector.visit(parse_tree)
reference_resolver = ReferenceResolver(symbol_table)
reference_resolver.visit(parse_tree)
semantic_analyzer = SemanticAnalyzer(symbol_table)
semantic_analyzer.visit(parse_tree)
except Exception as e:
print(e)
exit(-1)
if args.target in target_functions:
target_functions[args.target](parse_tree)
else:
raise ValueError(f"Invalid target {args.target}")
if __name__ == '__main__':
main()