-
Notifications
You must be signed in to change notification settings - Fork 0
/
pylox.py
61 lines (46 loc) · 1.09 KB
/
pylox.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
import enum
import sys
from lexing import TokenType, Token, Scanner
from parsing import Expr, Parser
import error
import interpreter
import resolver
interp = interpreter.Interpreter()
def run(code):
scanner = Scanner(code)
tokens = scanner.scan_tokens()
if error.had_error:
return
parser = Parser(tokens)
stmts = parser.parse()
if error.had_error:
return
res = resolver.Resolver(interp)
res.resolve(stmts)
if error.had_error:
return
interp.interpret(stmts)
def run_file(path):
with open(path) as f:
run(f.read())
if error.had_error:
sys.exit(65)
def run_prompt():
print('> ', end='', flush=True)
while True:
line = sys.stdin.readline()
if not line:
break
run(line)
error.had_error = False
print('> ', end='', flush=True)
def main():
if len(sys.argv) > 2:
print('Usage: pylox [script]')
sys.exit(64)
elif len(sys.argv) == 2:
run_file(sys.argv[1])
else:
run_prompt()
if __name__ == '__main__':
main()