-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler3645.py
77 lines (61 loc) · 2.33 KB
/
compiler3645.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
import os
import sys
import antlr4
from gen.GraspLexer import GraspLexer
from gen.GraspParser import GraspParser
from edu.yu.compilers.frontend.Semantics import Semantics
from edu.yu.compilers.frontend.SyntaxErrorHandler import SyntaxErrorHandler
from edu.yu.compilers.intermediate.util.BackendMode import BackendMode
from edu.yu.compilers.backend.converter.Converter import Converter
from antlr4 import CommonTokenStream
def main(args):
if len(args) != 2:
print(args)
print("python3 compiler3645.py <sourceFileName>")
return
source_file_name = args[1]
mode = BackendMode.CONVERTER
# Create the input stream.
source = open(source_file_name, 'r').read()
# Create the character stream from the input stream.
cs = antlr4.InputStream(source) # FIXME
# Custom syntax error handler.
syntax_error_handler = SyntaxErrorHandler()
# Create a lexer which scans the character stream
# to create a token stream.
lexer = GraspLexer(cs)
lexer.removeErrorListeners()
lexer.addErrorListener(syntax_error_handler)
tokens = CommonTokenStream(lexer)
# Create a parser which parses the token stream.
parser = GraspParser(tokens)
# Pass 1: Check syntax and create the parse tree.
parser.removeErrorListeners()
parser.addErrorListener(syntax_error_handler)
tree = parser.program()
#
# error_count = syntax_error_handler.get_count()
# if error_count > 0:
# print(f"\nThere were {error_count} syntax errors.")
# print("Object file not created or modified.")
# return
# Pass 2: Semantic operations.
pass2 = Semantics(mode)
pass2.visit(tree)
error_count = pass2.getErrorCount()
if error_count > 0:
print(f"\nThere were {error_count} semantic errors.")
print("Object file not created or modified.")
return
if mode == BackendMode.CONVERTER:
# Pass 3: Convert from Grasp to Java.
pass3 = Converter()
objectCode = str(pass3.visit(tree))
print(objectCode)
java_file_name = (source_file_name[source_file_name.rfind(os.sep) + 1:source_file_name.index('.')] + '.java').capitalize()
open(java_file_name, "x")
javaFile = open(java_file_name, "a")
javaFile.write(objectCode)
javaFile.close()
if __name__ == "__main__":
main(sys.argv)