forked from kcecireyes/JIT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jit_interpreter.py
executable file
·59 lines (51 loc) · 1.87 KB
/
jit_interpreter.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
49
50
51
52
53
54
55
56
57
58
59
from jit_parser import *
from pprint import pprint
from jit_ast_visitor import *
import os.path
class Interpreter:
def __init__(self, output_filename=None):
self.visitor = AstVisitor()
if output_filename == None:
self.output = None
exec "import jitlib" in globals()
else:
self.output = open(output_filename, 'w')
self.output.write("#!/usr/bin/python\n")
self.output.write("import os.path, sys\n")
self.output.write('sys.path.append("'+os.path.dirname(os.path.realpath(__file__))+'")\n')
self.output.write("from jitlib.node import Node\n")
self.output.write("\n")
self.output.write("\n")
self.output.write("\n")
self.output.write("\n")
self.output.write("\n")
self.output.write("\n")
def execute_ast(self, ast_node, debug=False):
code = ast_node.accept(self.visitor)
if debug:
print "code = " + code
if self.output == None:
exec code in globals()
else:
self.output.write(code)
# if hasattr(ast_node, 'right'):
# ast_node.right = self.execute_ast(ast_node.right)
#
# if hasattr(ast_node, 'left'):
# ast_node.left = self.execute_ast(ast_node.left)
#
# if (ast_node.type == "fun"):
# if ast_node.subtype == "say":
# print ast_node.child.value
# elif ast_node.subtype == "listen":
# raw_input("User input: ")
def execute_txt(self, code, line=0, debug=False):
parser = Parser()
ast = parser.parser.parse(code)
if debug:
print "AST:"
for line in str(ast).split('\n'):
print "\t"+line
self.execute_ast(ast, debug=debug)
if debug:
print "\n"