-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoxBase.py
61 lines (43 loc) · 1.37 KB
/
LoxBase.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
# Written by Hunter Wilkins
# Followed Bob Nystrom's book: Crafting Interpreter
# https://craftinginterpreters.com/contents.html
import sys
from LoxErrors import GlobalErrors
from Lox.Parser import Parser
from Lox.Resolver import Resolver
from Lox.Interpreter import Interpreter
from Lox.Scanner import Scanner
interpreter = Interpreter()
def run(source):
scanner = Scanner(source)
tokens = scanner.scan_tokens()
parser = Parser(tokens)
statements = parser.parse()
if GlobalErrors.had_error or GlobalErrors.had_runtime_error or statements[0] is None:
return
else:
resolver = Resolver(interpreter)
resolver.resolve(statements)
if GlobalErrors.had_error or GlobalErrors.had_runtime_error:
return
interpreter.interpret(statements)
def run_file(filename):
with open(filename, 'r') as file:
code = file.read()
run(code)
if GlobalErrors.had_error:
sys.exit(65)
elif GlobalErrors.had_runtime_error:
sys.exit(70)
def run_prompt():
while True:
run(input('> '))
GlobalErrors.had_error = False
GlobalErrors.had_runtime_error = False
def main():
if len(sys.argv) > 1:
run_file(sys.argv[1])
else:
run_prompt()
if __name__ == '__main__':
main()