-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
52 lines (42 loc) · 1.22 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
import argparse
import sys
from interpreter import Interpreter
from lexer import Lexer
from parser import Parser
from symbols import SemanticAnalyzer
from errors import LexerError, ParserError, SemanticError
def main():
parser = argparse.ArgumentParser(
description='SPI - Simple Pascal Interpreter'
)
parser.add_argument('inputfile', help='Pascal source file')
parser.add_argument(
'--scope',
help='Print scope information',
action='store_true',
)
parser.add_argument(
'--stack',
help='Print call stack',
action='store_true',
)
args = parser.parse_args()
_SHOULD_LOG_SCOPE, _SHOULD_LOG_STACK = args.scope, args.stack
text = open(args.inputfile, 'r').read()
lexer = Lexer(text)
try:
parser = Parser(lexer)
tree = parser.parse()
except (LexerError, ParserError) as e:
print(e.message)
sys.exit(1)
semantic_analyzer = SemanticAnalyzer(_SHOULD_LOG_SCOPE)
try:
semantic_analyzer.visit(tree)
except SemanticError as e:
print(e.message)
sys.exit(1)
interpreter = Interpreter(tree, _SHOULD_LOG_STACK)
interpreter.interpret()
if __name__ == '__main__':
main()