-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (50 loc) · 1.34 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
""" Swarm Interpreter """
import argparse
import sys
from base.error import LexerError, ParserError, SemanticError, InterpreterError
from interpreter.interpreter import Interpreter
from lexer.lexer import Lexer
from parser.parser import Parser
from semanticAnalyzer.semanticAnalyzer import SemanticAnalyzer
def main():
argParser = argparse.ArgumentParser(
description='Swarm Interpreter'
)
argParser.add_argument('inputfile', help='Pascal source file')
argParser.add_argument(
'--scope',
help='Print scope information',
action='store_true',
)
argParser.add_argument(
'--stack',
help='Print call stack',
action='store_true',
)
args = argParser.parse_args()
SHOULD_LOG_SCOPE, SHOULD_LOG_STACK = args.scope, args.stack
text = open(args.inputfile, 'r', encoding='utf-8').read()
if not text:
print("Text is None.")
sys.exit(1)
lexer = Lexer(text)
try:
parser = Parser(lexer)
tree = parser.parse()
except (LexerError, ParserError) as e:
print(e.message)
sys.exit(1)
semantic_analyzer = SemanticAnalyzer(log_or_not=SHOULD_LOG_SCOPE)
try:
semantic_analyzer.visit(tree)
except SemanticError as e:
print(e.message)
sys.exit(1)
interpreter = Interpreter(tree, log_or_not=SHOULD_LOG_STACK)
try:
interpreter.interpret()
except InterpreterError as e:
print(e.message)
sys.exit(1)
if __name__ == '__main__':
main()