-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
48 lines (33 loc) · 946 Bytes
/
main.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
import sys
from AstPrinter import ASTPrinter
from Interpreter import Interpreter
from Parser import Parser
from Scanner import Scanner
def main() -> None:
args = sys.argv[1:]
if len(args) > 1:
print("Usage: main.py [script]")
sys.exit(64)
elif len(args) == 1:
run_file(args[0])
else:
run_prompt()
def run_file(file_path: str) -> None:
with open(file_path, "r") as file:
run(file.read())
def run_prompt() -> None:
interpreter = Interpreter()
while True:
line = input("> ")
if len(line) == 0:
break
run(line, interpreter)
def run(source: str, interpreter: Interpreter | None = None) -> None:
if not interpreter:
interpreter = Interpreter()
scanner = Scanner(source, [])
tokens = scanner.scan_tokens()
parser = Parser(tokens)
interpreter.interpret(parser.parse())
if __name__ == "__main__":
main()