-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·36 lines (28 loc) · 927 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
import sys
from lex import Lexer
from parse import Parser
from utils import PROMPT
def shell(lexer: Lexer, parser: Parser):
"""Start the interactive shell."""
while True:
source = input(PROMPT).strip()
if not source:
continue
if not source.endswith(";"):
source += ";"
tokens = lexer.tokenize(source)
if ast := parser.parse(tokens):
result = ast.visit()
# Uncomment next line for debugging purposes
# print(ast)
print(result.repr())
if __name__ == "__main__":
lexer, parser = Lexer(), Parser()
if len(sys.argv) >= 2:
with open(sys.argv[1]) as source:
tokens = lexer.tokenize(source.read())
ast = parser.parse(tokens)
ast.visit() if ast else ...
else:
print("Starting interactive MochaScript interpreter.")
shell(lexer, parser)