Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (49 loc) · 1.41 KB
/
Copy pathmain.py
File metadata and controls
59 lines (49 loc) · 1.41 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
import argparse
import sys
from pascal_interpreter import (
Lexer, Parser, SemanticAnalyzer, Interpreter,
LexerError, ParserError, SemanticError
)
from pascal_interpreter.semantic_analyzer import set_scope_logging
from pascal_interpreter.interpreter import set_stack_logging
def main():
arg_parser = argparse.ArgumentParser(
description='Pascal Interpreter'
)
arg_parser.add_argument('inputfile', help='Pascal source file')
arg_parser.add_argument(
'--scope',
help='Print scope information',
action='store_true',
)
arg_parser.add_argument(
'--stack',
help='Print call stack',
action='store_true',
)
args = arg_parser.parse_args()
set_scope_logging(args.scope)
set_stack_logging(args.stack)
try:
with open(args.inputfile, 'r') as f:
text = f.read()
except FileNotFoundError:
print(f"Error: File '{args.inputfile}' not found")
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()
try:
semantic_analyzer.visit(tree)
except SemanticError as e:
print(e.message)
sys.exit(1)
interpreter = Interpreter(tree)
interpreter.interpret()
if __name__ == '__main__':
main()