-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.py
45 lines (33 loc) · 1.04 KB
/
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
38
39
40
41
42
43
44
45
from io import StringIO
import click
from luatopy.lexer import Lexer
from luatopy.parser import Parser
from luatopy.obj import Environment
from luatopy import evaluator
@click.command()
@click.option('--tokens', is_flag=True, help='Show lexer tokens')
@click.option('--ast-code', is_flag=True, help='Show AST code')
def run(tokens, ast_code):
print("luatopy repl")
if tokens:
print("* Config: Show lexer tokens")
if ast_code:
print("* Config: Show ast code")
env = Environment()
while True:
source = input("> ")
lexer = Lexer(StringIO(source))
if tokens:
print(list(lexer.tokens()))
parser = Parser(lexer)
program = parser.parse_program()
if parser.errors:
for err in parser.errors:
print("ERROR: {0}".format(err))
if ast_code:
print(program.to_code())
evaluated = evaluator.evaluate(program, env)
if evaluated:
print(evaluated.inspect())
if __name__ == '__main__':
run()