-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoot.py
84 lines (68 loc) · 2.17 KB
/
hoot.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
import sys
from error import RuntimeError
from tokens import TokenType
from scanner import Scanner
from resolver import Resolver
from interpreter import Interpreter
class Hoot:
def __init__(self):
self.interpreter = Interpreter(self)
self.had_error = False
self.had_runtime_error = False
def main(self):
if len(sys.argv) > 2:
print('Usage: hoot.py [script]')
sys.exit(64)
elif len(sys.argv) == 2:
exit_code = self.run_file(sys.argv[1])
sys.exit(exit_code)
else:
self.run_prompt()
pass
def run(self, source):
from parser_ import Parser
scanner = Scanner(self, source)
scanner.scan_tokens()
parser = Parser(self, scanner.tokens)
statements = parser.parse()
# stop for parser errors
if (self.had_error):
return
resolver = Resolver(self)
resolver.resolve_stmts(statements)
# stop for resolution errors
if (self.had_error):
return
self.interpreter.interpret(statements)
def run_file(self, path):
with open(path) as f:
self.run(f.read())
if self.had_error:
return 65
if self.had_runtime_error:
return 70
return 0
def run_prompt(self):
while True:
line = input('> ')
if len(line) == 0:
break
self.reset()
self.run(line)
def reset(self):
self.had_error = False
self.had_runtime_error = False
def report(self, line, where, message):
print(f'[line {line}] Error{where}: {message}', file=sys.stderr)
self.had_error = True
def error(self, token, message: str):
if token.type_ == TokenType.EOF:
self.report(token.line, " at end", message)
else:
self.report(token.line, f" at '{token.lexeme}'", message)
def runtime_error(self, error: RuntimeError):
print(f'[line {error.token.line}] {error.message}')
self.had_runtime_error = True
if __name__ == '__main__':
hoot = Hoot()
hoot.main()