-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlox.py
87 lines (69 loc) · 1.94 KB
/
lox.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
78
79
80
81
82
83
84
85
86
87
import sys
from typing import List
from astprinter import AstPrinter
from loxparser import Parser
from interpreter import Interpreter
from reporting import ErrorReporter
from scanner import Scanner
# UTILS
def read_file(filepath: str):
contents = ''
with open(filepath, 'r') as fp:
fp.read()
return contents
# MAIN LOGIC
class Lox:
def __init__(self):
self.error_reporter = ErrorReporter()
# needed for persisting global variables (not yet supported)
self.interpreter = Interpreter(self.error_reporter)
def run(self, source: str):
"""
run `source` code
"""
scanner = Scanner(source, self.error_reporter)
tokens = scanner.scan_tokens()
# print tokens
# for token in tokens:
# print(token)
parser = Parser(tokens, self.error_reporter)
statements = parser.parse()
if self.error_reporter.had_error:
# there was error; lazily exit
return
# print(AstPrinter().print(expression))
self.interpreter.interpret(statements)
def run_prompt(self):
"""
run repl prompt
"""
while True:
line = input('> ')
if line == '':
break
self.run(line)
def run_file(self, filepath: str):
"""
run from file
"""
source = read_file(filepath)
self.run(source)
if self.error_reporter.had_error:
sys.exit(65)
if self.error_reporter.had_runtime_error:
sys.exit(70)
def main(self):
"""
run main loop
"""
# remove script name from args
args = sys.argv[1:]
if len(args) > 1:
print('Usage: spanlox [script]')
sys.exit(64)
elif len(args) == 1:
self.run_file(args[0])
else:
self.run_prompt()
if __name__ == '__main__':
Lox().main()