/
dipper.py
135 lines (103 loc) · 3.54 KB
/
dipper.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
Dipper entry-point script
"""
import sys
sys.path.insert(0, "./pypy-source")
import os
try:
from rpython.rlib.objectmodel import we_are_translated
except ImportError:
def we_are_translated():
return False
from dip import parser, compiler, interpreter, basicio
from dip.errors import error_message, error_from_exception
from dip.namespace import Module
def main(argv):
argc = len(argv)
# debugging flags
debug = 0
debug_parser = False
debug_compiler = False
debug_interpreter = False
if argc == 1:
print "Usage: %s [-pci] <filename>.dip\n" % argv[0]
print " -p: Debug parser/ast"
print " -c: Debug compiler/bytecode"
print " -i: Debug interpreter/execution"
return 1
elif argc == 2:
debug = 0
filename = argv[1]
dip_args = []
else:
assert argc > 2
if argv[1].startswith("-"):
# figure out debug flags based on the first argument
for ch in argv[1]:
if ch == "p":
debug_parser = True
if ch == "c":
debug_compiler = True
elif ch == "i":
debug_interpreter = True
filename = argv[2]
dip_args = argv[2:]
else:
filename = argv[1]
dip_args = argv[1:]
if not os.path.exists(filename):
print "Specified file '%s' does not exist." % filename
return 1
# read in the file contents
data = basicio.readall(filename)
# create a parser
dip = parser.DipperParser(debug=debug_parser)
# just a debugging step so we can see the same input the parser sees
#print "=============== code ==================="
#print ">>> Source code (after small preprocessing step) <<<"
#print dip._prepSource(data)
if debug_parser > 0:
print "============== parsing ================="
try:
tree = dip.parse(data, filename=filename)
except Exception as e:
print "Error parsing file '%s': %s" % (filename, str(e))
return 1
if tree is None:
print "Error parsing file '%s'" % filename
return 1
if debug_parser:
print ">>> AST <<<"
print tree.show()
if debug_compiler:
print "============= compiling ================"
vm = interpreter.VirtualMachine(dip_args, debug=debug_interpreter)
mainmodule = Module.from_ast(filename, "main", tree)
if debug_compiler:
print mainmodule.toString()
for name, func in mainmodule.funcs.items():
print
print "___ Function '%s' ___" % name
print func.toString()
vm.setglobals(mainmodule)
if debug_parser or debug_compiler or debug_interpreter:
print "============= executing ================"
try:
vm.run()
except interpreter.InterpreterError as e:
print error_message(filename, e.getsource(), e.getmessage())
return 1
except Exception as e:
print error_from_exception(filename, (-1, -1), e)
raise
return 0
def target(driver, args):
return main, None
if we_are_translated():
from rpython.jit.codewriter.policy import JitPolicy
def jitpolicy(driver):
return JitPolicy()
# this only runs if the script is being run from a regular Python interpreter,
# so just call the RPython main function
if __name__ == '__main__':
main(sys.argv)