-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathpxic_reader.py
More file actions
251 lines (192 loc) · 5.79 KB
/
pxic_reader.py
File metadata and controls
251 lines (192 loc) · 5.79 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import pixie.vm2.interpreter as ast
import pixie.vm2.code as code
from pixie.vm2.keyword import keyword
from pixie.vm2.symbol import symbol
from pixie.vm2.primitives import true, false, nil
from pixie.vm2.array import Array
from pixie.vm2.string import char_cache
import pixie.vm2.rt as rt
class Reader(object):
def __init__(self, filename):
self._file = open(filename, "rb")
self._cache = []
def __del__(self):
self._file.close()
def read(self):
try:
return ord(self._file.read(1)[0])
except IndexError:
raise EOFError
def get_cache_idx(self):
idx = len(self._cache)
self._cache.append(None)
return idx
def set_cache(self, idx, obj):
self._cache[idx] = obj
def get_cache(self, idx):
return self._cache[idx]
bytecodes = ["CACHED_OBJECT",
"INT",
"FLOAT",
"INT_STRING",
"STRING",
"TRUE",
"FALSE",
"NIL",
"VAR",
"KEYWORD",
"SYMBOL",
"NEW_CACHED_OBJECT",
"DO",
"INVOKE",
"VAR",
"CONST",
"FN",
"LOOKUP",
"IF",
"LET",
"META",
"LINE_META",
"VAR_CONST",
"CHAR",
"VECTOR",
"RECUR"]
for idx, x in enumerate(bytecodes):
globals()[x] = idx
def read_utf8_char(os):
ch = os.read()
if ch <= 0x7F:
n = ch
bytes = 1
elif (ch & 0xE0) == 0xC0:
n = ch & 31
bytes = 2
elif (ch & 0xF0) == 0xE0:
n = ch & 15
bytes = 3
elif (ch & 0xF8) == 0xF0:
n = ch & 7
bytes = 4
else:
raise AssertionError("Bad unicode character " + str(ch))
i = bytes - 1
while i > 0:
i -= 1
n = (n << 6) | (os.read() & 0x3F)
return unichr(n)
def read_raw_int(os):
return os.read() | (os.read() << 8) | (os.read() << 16) | (os.read() << 24)
def read_raw_string(os):
buf = []
for x in range(read_raw_int(os)):
buf.append(read_utf8_char(os))
return u"".join(buf)
def read_raw_list(os):
vals = [None] * read_raw_int(os)
for x in range(len(vals)):
vals[x] = read_object(os)
return vals
def read_object(os):
tag = os.read()
if tag == DO:
statements = [None] * read_raw_int(os)
for x in range(len(statements)):
statements[x] = read_object(os)
meta = read_object(os)
return ast.Do(statements, meta)
elif tag == INVOKE:
args = [None] * read_raw_int(os)
for x in range(len(args)):
args[x] = read_object(os)
meta = read_object(os)
return ast.Invoke(args, meta=meta)
elif tag == RECUR:
args = [None] * read_raw_int(os)
for x in range(len(args)):
args[x] = read_object(os)
meta = read_object(os)
return ast.Recur(args, meta=meta)
elif tag == NEW_CACHED_OBJECT:
idx = os.get_cache_idx()
o = read_object(os)
os.set_cache(idx, o)
return o
elif tag == VAR:
ns = read_raw_string(os)
var_name = read_object(os)
meta = read_object(os)
return ast.VDeref(ns, var_name, meta)
elif tag == VAR_CONST:
ns = read_raw_string(os)
name = read_object(os)
meta = read_object(os)
return ast.VarConst(ns, name, meta)
elif tag == CONST:
return ast.Const(read_object(os))
elif tag == CACHED_OBJECT:
return os.get_cache(read_raw_int(os))
elif tag == FN:
name_str = read_raw_string(os)
name = keyword(name_str)
args = read_raw_list(os)
closed_overs = read_raw_list(os)
body = read_object(os)
meta = read_object(os)
return ast.Fn(name=name, args=args, body=body, closed_overs=closed_overs, meta=meta)
elif tag == LOOKUP:
return ast.Lookup(read_object(os), read_object(os))
elif tag == KEYWORD:
return keyword(read_raw_string(os))
elif tag == SYMBOL:
return symbol(read_raw_string(os))
elif tag == FALSE:
return false
elif tag == TRUE:
return true
elif tag == NIL:
return nil
elif tag == INT:
return rt.wrap(read_raw_int(os))
elif tag == INT_STRING:
s = read_raw_string(os)
return rt.wrap(int(s))
elif tag == IF:
return ast.If(read_object(os),
read_object(os),
read_object(os),
read_object(os))
elif tag == LET:
bc = read_raw_int(os)
names = [None] * bc
values = [None] * bc
for idx in range(bc):
names[idx] = read_object(os)
values[idx] = read_object(os)
body = read_object(os)
meta = read_object(os)
return ast.Let(names=names, bindings=values, body=body, meta=meta)
elif tag == META:
line = read_object(os)
assert isinstance(line, ast.Meta)
col = read_raw_int(os)
return ast.Meta(line._c_line_tuple, col)
elif tag == LINE_META:
file = read_raw_string(os)
line = read_raw_string(os)
line_number = read_raw_int(os)
return ast.Meta((line, file, line_number), 0)
elif tag == STRING:
str = read_raw_string(os)
return rt.wrap(str)
elif tag == CHAR:
str = read_raw_string(os)
return char_cache.intern(ord(str[0]))
elif tag == VECTOR:
cnt = read_raw_int(os)
lst = [None] * cnt
for x in range(cnt):
lst[x] = read_object(os)
return Array(lst)
raise AssertionError("No valid handler for TAG " + bytecodes[tag])
def read_file(filename):
return read_object(Reader(filename))