-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
63 lines (52 loc) · 1.73 KB
/
Copy pathinterpreter.py
File metadata and controls
63 lines (52 loc) · 1.73 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
import sys
from errors import SparrowError, computeLineStarts, formatError
from frontend.lexer.tokenizer import tokenize
from frontend.parser.parser import parseProgram
from runtime.environment import Environment
from runtime.evaluator import execute
from semantic.type_environment import TypeEnvironment
from semantic.typecheck import checkStmt
def main() -> None:
# 1. read path from argv, read file
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} <input-file>")
exit(1)
path = sys.argv[1]
with open(path, "r") as file:
src = file.read()
lineStarts = computeLineStarts(src)
# 2. tokenize + parse (catch SparrowError -> formatError -> print -> exit)
try:
tokens = tokenize(src)
except SparrowError as e:
print(formatError(e, src, lineStarts))
exit(2)
try:
ast = parseProgram(tokens)
except SparrowError as e:
print(formatError(e, src, lineStarts))
exit(3)
# 3. typecheck every statement against one fresh TypeEnvironment (catch + report + exit on failure)
typeEnv = TypeEnvironment()
for stmt in ast:
try:
checkStmt(
stmt,
typeEnv,
False,
)
except SparrowError as e:
print(formatError(e, src, lineStarts))
exit(4)
# 4. only if all passed: execute every statement against one fresh Environment, print non-None results
env = Environment()
for stmt in ast:
try:
out = execute(stmt, env)
if out is not None:
print(out)
except SparrowError as e:
print(formatError(e, src, lineStarts))
exit(5)
if __name__ == "__main__":
main()