-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbytecode.py
More file actions
505 lines (415 loc) · 12.7 KB
/
bytecode.py
File metadata and controls
505 lines (415 loc) · 12.7 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#### Bytecode Protocol ####
#### ####
#### Version 1.5 ####
#### By featheryleaf ####
#### 2013.11 ####
from collections import deque
print('import bytecode.')
### Class Defines ###
## Exception Class
class SchemeError(Exception) : pass
class SchemeSyntaxError(SyntaxError, SchemeError): pass
class SchemeTypeError(TypeError, SchemeError) : pass
class SchemeValueError(ValueError, SchemeError) : pass
class SchemeIOError(IOError, SchemeError) : pass
## Variable Class
Void = type(None)
Bool = bool
Number = (int, float, complex)
Scalar = (Void, Bool, int, float, complex)
class Char(str) : pass
class String(str) : pass
class Symbol(str) : pass
class Vector(list): pass
class Pair:
PROPER_LIST = 0
IMPROPER_LIST = 1
CIRCULAR_LIST = 2
def __init__(self, a, d):
self.data = [a, d]
def car(self):
return self.data[0]
def cdr(self):
return self.data[1]
def set_car(self, value):
self.data[0] = value
return None
def set_cdr(self, value):
self.data[1] = value
return None
def is_list(self):
a = b = self
while(1):
if isinstance(a, Pair):
a = a.cdr()
if isinstance(a, Pair):
if a is b:
return False
a = a.cdr()
b = b.cdr()
else:
return a is None
else:
return a is None
def is_circular(self):
hash = {id(self):1}
dfs = deque()
dfs.append(self)
while(len(dfs) > 0):
cur = dfs.popleft()
a = cur.car()
b = cur.cdr()
if (not isinstance(a, Scalar) and id(a) in hash) or (not isinstance(b, Scalar) and id(b) in hash):
return True
if a is not None:
hash[id(a)] = 1
if b is not None:
hash[id(b)] = 1
if isinstance(a, Pair):
dfs.append(a)
if isinstance(b, Pair):
dfs.append(b)
else:
return False
def __eq__(self, other):
return isinstance(other, Pair) and self.data == other.data
def __str__(self):
string = '('
if True:#not self.is_circular():
a = self
while isinstance(a, Pair):
if a is not self:
string += ' '
string += str(a.car())
a = a.cdr()
if a is not None:
string += ' . ' + str(a)
string += ')'
else: # CIRCULAR_LIST
string = '(...)'
return string
def list2pair(li):
if not li: # li = []
return None
elif not isinstance(li[0], list):
return Pair(li[0], list2pair(li[1:]))
else:
return Pair(list2pair(li[0]), list2pair(li[1:]))
Atom = (Void, Bool, int, float, complex, Char, String, Symbol, Vector, Pair)
Tail = [None]
def Pair_Assertion():
pair = list2pair([1, 2])
assert pair == Pair(1, Pair(2, None))
assert not pair.is_circular()
assert pair.is_list()
assert str(pair) == '(1 2)'
pair = list2pair([1, 2, 3, 4])
assert pair == Pair(1, Pair(2, Pair(3, Pair(4, None))))
assert not pair.is_circular()
assert pair.is_list()
assert str(pair) == '(1 2 3 4)'
pair = list2pair([[1, 2], 3, 4])
assert pair == Pair(Pair(1, Pair(2, None)), Pair(3, Pair(4, None)))
assert not pair.is_circular()
assert pair.is_list()
assert str(pair) == '((1 2) 3 4)'
pair = Pair(1, 2)
assert not pair.is_circular()
assert not pair.is_list()
assert str(pair) == '(1 . 2)'
pair.set_cdr(pair)
assert pair.is_circular()
assert not pair.is_list()
assert pair.car() == 1
assert pair.cdr() == pair
assert str(pair) == '(...)'
pair = list2pair([1, 2])
pair.set_car(pair)
assert pair.is_circular()
assert pair.is_list()
assert pair.car() == pair
assert pair.cdr() == Pair(2, None)
assert str(pair) == '(...)'
pair.set_cdr(pair)
assert pair.is_circular()
assert not pair.is_list()
assert pair.car() == pair
assert pair.cdr() == pair
assert str(pair) == '(...)'
pair1 = list2pair([1, 2])
pair2 = list2pair([3, 4])
pair1.set_car(pair2)
pair2.set_cdr(pair1)
assert pair1.is_circular()
assert pair1.is_list()
assert pair1.car() == pair2
assert pair1.cdr() == Pair(2, None)
assert str(pair1) == '(...)'
assert pair2.is_circular()
assert pair2.is_list()
assert pair2.car() == 3
assert pair2.cdr() == pair1
assert str(pair2) == '(...)'
### Predefines of bytecode protocol ###
## Instruction Type Code
NOP = 0 # NOP : <no operation>
PUSH = 1 # PUSH %var : stack.append(%var)
POP = 2 # POP %var : %var = stack.pop()
POPN = 3 # POPN n : cur-continuation[0:n] = stack[-n:].reverse(); del stack[-n:]
POPL = 4 # POPL %var : %var = Pair(stack[-?:].reverse()); del stack[-?:]
ALLOC = 6 # ALLOC n : cur-continuation.local-var alloc n elem
CALL = 8 # CALL n : call stack[-1] with args stack[-n-1:-1]; del stack[-n-1:]
NCAL = 9 # NCAL %nproc n : call native-proc %nproc with arg stack[-n:]; del stack[-n:]
TCAL = 10 # TCAL n : call without making new continuation
RET = 12 # RET : cur-continuation = cur-continuation.ret-continuation
JUMP = 17 # JUMP COND offset : if (cond(stack[-1])) PC += offset; stack.pop()
## Variable Storage Code
NULL = 0
IMM = 1
GLOBAL = 2
LOCAL = 3
# NATIVE = 4 #not used
CC = 5
## Special Variable (for SPECIAL variable)
# PC = 0 # not used
# CC = 1
## Condition Code (for JUMP instruction)
ALWAYS = 0
TRUE = 1
FALSE = 2
NEVER = 3
## Native Procedure (for NCAL instruction)
PASS = 0
IS_EQ = 4
IS_EQV = 5
IS_EQUAL = 6
NOT = 8
IS_BOOL = 24
ADD = 51
SUB = 52
MUL = 53
DIV = 54
ABS = 55
MOD = 56
MAX = 57
MIN = 58
EQ = 60
LS = 62
LE = 63
BG = 64
BE = 65
IS_ZERO = 66
IS_POS = 67
IS_NEG = 68
IS_ODD = 69
IS_EVEN = 70
IS_INT = 120
IS_RAT = 121
IS_REAL = 122
IS_COMPLEX = 123
IS_NUM = 128
IS_CHAR = 240
MAKE_STR = 310
STR = 311
SUB_STR = 312
STR_LEN = 314
IS_STR = 320
IS_VECTOR = 420
IS_PAIR = 480
IS_LIST = 481
CONS = 482
LIST = 483
CAR = 486
CDR = 487
SET_CAR = 488
SET_CDR = 489
IS_NULL = 500
IS_PORT = 540
IS_SYMBOL = 600
IS_PROC = 810
OPEN_IFILE = 811
OPEN_OFILE = 812
READ = 824
WRITE = 828
DISPLAY = 829
# Special Procedure (not for NCAL instruction)
SPECIAL_PROC_BOUND = 1000
SET = 1001
BEGIN = 1010
CALL_CC = 1020
### Global Variable & Procedure Define ###
## Procedure Class
class Procedure:
proc_id = 0
def push_env(self):
self.bytecode_list = []
self.parent = Global.cur_proc
if self.parent:
self.parent.bytecode_list = Global.bytecode_list
self.id = Procedure.proc_id
Procedure.proc_id += 1
Global.cur_proc = self
Global.bytecode_list = self.bytecode_list
Global.symbol_table.append([{}, 0])
Global.add_const_table(self)
def pop_env(self):
Global.bytecode_list.insert(0, [ALLOC, Global.symbol_table[-1][1]]) # proc head
Global.bytecode_list.append([RET]) # proc tail
self.bytecode_list = Global.bytecode_list
Global.cur_proc = self.parent
if Global.cur_proc != None:
Global.bytecode_list = Global.cur_proc.bytecode_list
else:
Global.bytecode_list = []
del Global.symbol_table[-1]
def __str__(self):
string = '<' + str(self.id) + '>\n'
for bytecode in self.bytecode_list:
string += str(bytecode) + '\n'
return string
class NativeProc:
def __init__(self, symbol):
if isinstance(symbol, Symbol):
if not symbol in Global.native_proc:
raise SchemeValueError('"%s" is not a native procedure!' % symbol)
self.symbol = symbol
self.id = Global.native_proc[symbol]
self.bytecode_list = [[NCAL, self.id]]
self.parent = None
elif isinstance(symbol, str):
pass
####
def __str__(self):
return str(self.symbol)
## Global Variable Defines
class Global:
global_proc = None
cur_proc = None
bytecode_list = []
const_table = []
const_hash_table = {}
symbol_table = []
native_proc = {
Symbol('+'): ADD,
Symbol('-'): SUB,
Symbol('*'): MUL,
Symbol('/'): DIV,
Symbol('abs'): ABS,
Symbol('max'): MAX,
Symbol('min'): MIN,
Symbol("%"): MOD,
Symbol('='): EQ,
Symbol('<'): LS,
Symbol('<='): LE,
Symbol('>'): BG,
Symbol('>='): BE,
Symbol('zero?'): IS_ZERO,
Symbol('negative?'): IS_NEG,
Symbol('positive?'): IS_POS,
Symbol('odd?'): IS_ODD,
Symbol('even?'): IS_EVEN,
Symbol('integer?'): IS_INT,
Symbol('rational?'): IS_RAT,
Symbol('real?'): IS_REAL,
Symbol('complex?'): IS_COMPLEX,
Symbol('number?'): IS_NUM,
Symbol('boolean?'): IS_BOOL,
Symbol('not'): NOT,
Symbol('eq?'): IS_EQ,
Symbol('eqv?'): IS_EQV,
Symbol('equal?'): IS_EQUAL,
Symbol('char?'): IS_CHAR,
Symbol('string?'): IS_STR,
Symbol('make-string'): MAKE_STR,
Symbol('string'): STR,
Symbol('string-length'): STR_LEN,
Symbol('substring'): SUB_STR,
Symbol('vector?'): IS_VECTOR,
Symbol('pair?'): IS_PAIR,
Symbol('list?'): IS_LIST,
Symbol('list'): LIST,
Symbol('cons'): CONS,
Symbol('car'): CAR,
Symbol('cdr'): CDR,
Symbol('set-car!'): SET_CAR,
Symbol('set-cdr!'): SET_CDR,
Symbol('null?'): IS_NULL,
Symbol('port?'): IS_PORT,
Symbol('procedure?'): IS_PROC,
Symbol('symbol?'): IS_SYMBOL,
Symbol('read'): READ,
Symbol('write'): WRITE,
Symbol('display'): DISPLAY,
Symbol('open-input-file'): OPEN_IFILE,
Symbol('open-output-file'): OPEN_OFILE,
Symbol('set!'): SET,
Symbol('begin'): BEGIN,
Symbol('call-with-current-continuation'): CALL_CC,
}
def __init__():
init()
def init():
Global.cur_proc = None
Global.bytecode_list = []
Global.const_table = []
Global.const_hash_table = {}
Global.symbol_table = []
Procedure.proc_id = 0
Global.global_proc = Procedure()
Global.global_proc.push_env()
def end():
Global.global_proc.pop_env()
Global.bytecode_list = [[PUSH, GLOBAL, 0], [CALL, 0]]
def print_global():
print('global_proc: ', Global.global_proc)
print('cur_proc: ', Global.cur_proc)
print('bytecode_list: ', Global.bytecode_list)
print('const_table: ', Global.const_table)
print('const_hash_table:', Global.const_hash_table)
print('symbol_table: ', Global.symbol_table)
def gen_bytecode(bytecode):
if isinstance(bytecode, list):
for code in bytecode:
if not isinstance(code, int):
break
else:
if (bytecode == [ALLOC, 0]) or bytecode == [POPN, 0]:
return
if bytecode == [POP, NULL] and len(Global.bytecode_list) and Global.bytecode_list[-1] == [PUSH, NULL]:
Global.bytecode_list.pop()
return
Global.bytecode_list.append(bytecode)
return
raise SchemeValueError('invalid bytecode format ( %s )!' % bytecode)
def find_symbol(symbol):
for rank in range(len(Global.symbol_table)):
table, _ = Global.symbol_table[- 1 - rank]
if symbol in table:
return (rank, table[symbol])
else: # symbol not in symbol_tables
return (None, None)
def add_symbol(symbol):
Global.symbol_table[-1][0][symbol] = Global.symbol_table[-1][1]
Global.symbol_table[-1][1] += 1
return (0, Global.symbol_table[-1][1] - 1)
def find_const_table(atom):
try:
if atom in Global.const_hash_table:
return Global.const_hash_table[atom]
except TypeError:
if id(atom) in Global.const_hash_table:
return Global.const_hash_table[id(atom)]
return None
def add_const_table(atom):
try:
if id(atom) in Global.const_hash_table:
return
elif atom in Global.const_hash_table:
return
else:
Global.const_hash_table[atom] = len(Global.const_table)
Global.const_table.append(atom)
return
except TypeError:
Global.const_hash_table[id(atom)] = len(Global.const_table)
Global.const_table.append(atom)