forked from overmind2/sanya
/
targetscheme.py
executable file
·174 lines (147 loc) · 4.89 KB
/
targetscheme.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python
import sys
import __pypy_path__
from pypy.rlib.objectmodel import we_are_translated
from pypy.rlib.streamio import fdopen_as_stream, open_file_as_stream
from sanya import chunkio
from sanya.compilation import compile_list_of_expr
from sanya.config import DEBUG
from sanya.objectmodel import w_unspecified
from sanya.parser import parse_string
from sanya.stdlib import open_lib
from sanya.vm import VM
if not we_are_translated():
# we can have better repl when running in top of cpython
import readline
def jitpolicy(driver):
from pypy.jit.codewriter.policy import JitPolicy
return JitPolicy()
def filename_to_expr_list(filename):
stream = open_file_as_stream(filename, 'r')
content = stream.readall()
expr_list = parse_string(content)
stream.close()
return expr_list
def run_file(filename):
vm = VM()
open_lib(vm)
vm.bootstrap(compile_list_of_expr(filename_to_expr_list(filename)))
vm.run()
def disassemble_file(filename):
"""NOT_RPYTHON"""
print compile_list_of_expr(filename_to_expr_list(filename))
def generate_header(filename):
"""NOT_RPYTHON"""
from instrset import op_map
with open(filename, 'w') as f:
hpro = '_' + filename.upper().replace('.', '_') + '_'
f.write('#ifndef %s\n' % hpro)
f.write('#define %s\n' % hpro)
f.write('\n')
f.write('// Automatically generated from sanya/instrset.py.\n')
f.write('\n')
for name, val in op_map.iteritems():
f.write('#define OP_%s (%s)\n' % (name, val))
f.write('\n')
f.write('#endif // %s\n' % hpro)
def compile_file(filename, outfname):
"""maybe rpython..."""
w_skel = compile_list_of_expr(filename_to_expr_list(filename))
outf = open_file_as_stream(outfname, 'w')
chunkio.dump(w_skel, outf)
outf.close()
def load_compiled_chunk(filename):
vm = VM()
open_lib(vm)
stream = open_file_as_stream(filename, 'r')
w_skel = chunkio.load(stream)
stream.close()
vm.bootstrap(w_skel)
vm.run()
def repl():
stdin = fdopen_as_stream(0, 'r')
stdout = fdopen_as_stream(1, 'a')
vm = VM()
open_lib(vm)
while True:
if we_are_translated():
# RPython -- cannot use readline
stdout.write('> ')
stdout.flush()
raw_line_of_code = stdin.readline()
else:
# CPython -- use readline
try:
raw_line_of_code = raw_input('> ')
except (EOFError, KeyboardInterrupt):
raw_line_of_code = ''
if not raw_line_of_code:
break # handle EOF
raw_line_of_code = raw_line_of_code.strip('\n') # RPy
if not raw_line_of_code:
continue # handle plain ENTER
expr_list = parse_string(raw_line_of_code)
if not expr_list:
continue # handle whitespace in RPy
w_skel = compile_list_of_expr(expr_list)
# some hack so as to not return the vm?
# to view code
if DEBUG:
print w_skel
# to view code
if DEBUG:
continue
vm.bootstrap(w_skel)
vm.run()
w_result = vm.exit_value
if w_result is not w_unspecified:
print w_result.to_string()
def entry_point(argv):
if len(argv) == 1:
repl()
return 0
elif len(argv) == 2:
run_file(argv[1])
return 0
else:
# more utilties for both RPython and CPython
op = argv[1]
fname = argv[2]
if op == '-c': # compile the code and dump to another file
outfname = argv[3]
compile_file(fname, outfname)
return 0
elif op == '-r': # run compiled chunk
load_compiled_chunk(fname)
return 0
# end of RPython-compatible functionalities
if not we_are_translated():
op = argv[1]
fname = argv[2]
if op == '-d': # disassemble
disassemble_file(fname)
elif op == '-g': # generate .hpp instruction definations
generate_header(fname)
elif op == '-v': # view compiled chunk
stream = open_file_as_stream(fname, 'r')
w_skel = chunkio.load(stream)
stream.close()
print w_skel
elif op == '-t': # do cps transform
from sanya.transform import transform_list_of_expr
expr_list = filename_to_expr_list(fname)
print transform_list_of_expr(expr_list)
else:
print 'what do you want to do?'
return
print 'usage:'
print ' %s to start repl' % argv[0]
print ' %s [filename] to run file' % argv[0]
print ' %s -c [filename] [output] to compile file' % argv[0]
print ' %s -r [filename] to run compiled file' % argv[0]
return 0
def target(driver, args):
driver.exe_name = 'c-scheme'
return entry_point, None
if __name__ == '__main__':
entry_point(sys.argv)