-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·32 lines (25 loc) · 935 Bytes
/
run.py
File metadata and controls
executable file
·32 lines (25 loc) · 935 Bytes
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
#!/usr/bin/env python
import ast
from argparse import ArgumentParser
from kanren import var, run
from evalo import evalo
parser = ArgumentParser(description="Evaluate a python file")
parser.add_argument("--path", help="Path to the file that should be interpreted")
parser.add_argument("--expr", "-e", help="Python expression that should be interpreted")
args = parser.parse_args()
def interp(expr):
print("Program to be evaluated:\n{}".format(expr))
program_ast = ast.parse(expr)
# program_ast = ast.Num(n=1)
print("AST of the program: \n{}".format(ast.dump(program_ast)))
x = var()
evaluated_program = run(0, x, evalo(program_ast, x))
print("Evaluated program: {}".format(evaluated_program))
if args.path:
with open(args.path, "r") as fp:
program = fp.read()
if program[-1:] == "\n":
program = program[:-1]
interp(program)
if args.expr:
interp(args.expr)