-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·91 lines (73 loc) · 2.63 KB
/
main.py
File metadata and controls
executable file
·91 lines (73 loc) · 2.63 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
#!/usr/bin/python3
# coding:utf-8
from antlr4 import *
from dist.PlayScriptLexer import PlayScriptLexer
from dist.PlayScriptParser import PlayScriptParser
from src.ScanScopes import ScanScopes
from src.PlayVisitor import PlayVisitor
from src.TypeResolver import TypeResolver
from src.TypeChecker import TypeChecker
from src.RefResolver import RefResolver
def main(line):
input_stream = InputStream(line)
# lexing
lexer = PlayScriptLexer(input_stream)
stream = CommonTokenStream(lexer)
# parsing
parser = PlayScriptParser(stream)
tree = parser.prog()
# semantic analysis
printer = ScanScopes(tree)
walker = ParseTreeWalker()
walker.walk(printer, tree)
# I属性解析
type_resolver = TypeResolver(tree)
walker = ParseTreeWalker()
walker.walk(type_resolver, tree)
# S属性解析、类型推导
ref_resolver = RefResolver(tree)
walker = ParseTreeWalker()
walker.walk(ref_resolver, tree)
# 类型检查
type_checker = TypeChecker(tree)
walker = ParseTreeWalker()
walker.walk(type_checker, tree)
compile_err_list = type_checker.compile_error_list
compile_err_list.extend(type_resolver.compile_error_list)
compile_err_list.extend(ref_resolver.compile_err_list)
if len(compile_err_list) != 0:
print("compile error,details as below:\n")
for compile_error in compile_err_list:
print(" line %s: %s" % (compile_error.err_line, compile_error.err_msg))
return False
# 闭包分析
# for k, v in NodeScopeMap.node_scope_list:
# for symbol in v.symbol_list:
# if isinstance(symbol, FunctionSymbol):
# tmp = symbol.compute_needed_symbols()
# print(tmp)
# use customized visitor to traverse AST
visitor = PlayVisitor()
return visitor.visit(tree)
def unittest():
import os
for i in os.listdir("tests"):
if i.endswith(".play"):
file_path = "tests/" + i
print(file_path)
with open(file_path, "r") as f:
s = f.read()
main(s)
if __name__ == '__main__':
# main("{int a=2*3+1;a=a+3*2;int b=1+2;snoopy_print(a, b);} {int a=2;}")
# main("{int a=2;{int b=2;{int c=2;}}snoopy_print(b);} {}")
# main("{int a=2;{int a=3;snoopy_print(a);}snoopy_print(a);}")
# main("{int a=2;{a=3;int b=4;snoopy_print(a);}snoopy_print(a);snoopy_print(b);}")
# main("snoopy_print(2+3);")
# main("{int x(int a){snoopy_print(a);}x(3);}")
# unittest()
with open("tests/extends.play", "r") as f:
script = f.read()
main(script)
# x = NodeScopeMap.node_scope_list
# print(x)