-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.py
37 lines (29 loc) · 789 Bytes
/
repl.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
import lexer
import parser
import tokens
import evaluator
import monkey_object as mobject
from typing import List
import readline
PROMPT = ">>"
def start(env=None):
if env is None:
env = mobject.Environment()
while True:
print(PROMPT, end=" ")
line = input()
if not line:
return
l = lexer.Lexer(line)
p = parser.Parser(l)
program = p.parse_program()
if len(p.errors) != 0:
print_parser_errors(p.errors)
continue
evaluated = evaluator.eval(program, env)
if evaluated is not None:
print(evaluated.inspect)
def print_parser_errors(errors: List[str]):
print("Woops! Parser Errors!")
for error in errors:
print(f" {error}")