-
Notifications
You must be signed in to change notification settings - Fork 2
/
flatten.py
180 lines (169 loc) · 5.24 KB
/
flatten.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
174
175
176
177
178
179
180
from errors import *
from parse import *
from nodes import *
from func import *
from loop import *
from ifelse import *
class FlatNode(object):
pass
class Code(FlatNode):
def __init__(self, words):
self.words = words
class GoTo(FlatNode):
def __init__(self, index):
self.index = index
class Branch(FlatNode):
"""Branch-If-Zero object"""
def __init__(self, index):
self.index = index
class LabdaNode(FlatNode):
def __init__(self, index):
self.index = index
class Marker(object):
pass
class SingleInstruction(object):
def __init__(self, opcode, ref):
self.opcode = opcode
self.ref = ref
def __repr__(self):
return str(self.opcode) + ' ' + str(self.ref)
def flatten(tree, acc=None):
if acc is None:
acc = []
if isinstance(tree, list):
for branch in tree:
flatten(branch, acc)
return acc
for branch in tree.children:
if isinstance(branch, list):
for b in branch:
flatten(b, acc)
if isinstance(branch, Statement):
acc.append(SingleInstruction('LINE_NUMBER', branch.linenr))
if isinstance(branch, Word):
if acc and isinstance(acc[-1], Code):
acc[-1].words.append(branch)
else:
acc.append(Code([branch]))
elif isinstance(branch, WordList):
if isinstance(branch, Line):
acc.append(SingleInstruction('LINE_NUMBER', branch.linenr))
if acc and isinstance(acc[-1], Code):
acc[-1].words.extend(branch.children)
else:
acc.append(Code(list(branch.children)))
elif isinstance(branch, LabdaStatement):
m = Marker()
acc.append(LabdaNode(m))
for argument in branch.arguments:
acc.append(SingleInstruction('SET_LOCAL', argument))
flatten(branch.body, acc)
acc.append(SingleInstruction('RETURN', 0))
acc.append(m)
if isinstance(branch, LocalFuncStatement):
acc.append(SingleInstruction('SET_LOCAL', branch.name))
elif isinstance(branch, FuncStatement):
name = branch.name
if '!' in name:
if name.count('!') > 1 or name.endswith('!'):
raise DejaSyntaxError('methods need exactly one method name')
if name.startswith('!'):
name = 'eva' + name
base, method = name.split('!')
acc.append(SingleInstruction('PUSH_LITERAL', method))
acc.append(SingleInstruction('PUSH_WORD', base))
acc.append(SingleInstruction('SET_DICT', 0))
else:
acc.append(SingleInstruction('SET_GLOBAL', name))
elif isinstance(branch, WhileStatement):
m1 = Marker()
m2 = Marker()
acc.append(SingleInstruction('ENTER_SCOPE', 0))
acc.append(m1)
flatten(branch.conditionclause, acc)
acc.append(Branch(m2))
flatten(branch.body, acc)
acc.append(GoTo(m1))
acc.append(m2)
acc.append(SingleInstruction('LEAVE_SCOPE', 0))
elif isinstance(branch, ForStatement):
m1 = Marker()
m2 = Marker()
flatten(branch.forclause, acc)
acc.append(m1)
acc.append(SingleInstruction('DUP', 0))
acc.append(Branch(m2))
acc.append(SingleInstruction('ENTER_SCOPE', 0))
acc.append(SingleInstruction('SET_LOCAL', '#f'))
acc.append(SingleInstruction('SET_LOCAL', '#h'))
acc.append(SingleInstruction('SET_LOCAL', branch.countername))
flatten(branch.body, acc)
acc.append(SingleInstruction('PUSH_WORD', '#h'))
acc.append(SingleInstruction('PUSH_WORD', '#f'))
acc.append(SingleInstruction('LEAVE_SCOPE', 0))
acc.append(GoTo(m1))
acc.append(m2)
acc.append(SingleInstruction('DROP', 0))
elif isinstance(branch, RepeatStatement):
m1 = Marker()
m2 = Marker()
flatten(branch.forclause, acc)
acc.append(SingleInstruction('ENTER_SCOPE', 0))
acc.append(SingleInstruction('SET_LOCAL', '#r'))
acc.append(m1)
acc.append(SingleInstruction('PUSH_WORD', '#r'))
acc.append(Branch(m2))
flatten(branch.body, acc)
acc.append(SingleInstruction('PUSH_WORD', '#r'))
acc.append(SingleInstruction('PUSH_WORD', '--'))
acc.append(SingleInstruction('SET_LOCAL', '#r'))
acc.append(GoTo(m1))
acc.append(m2)
acc.append(SingleInstruction('LEAVE_SCOPE', 0))
elif isinstance(branch, IfStatement):
m_end = Marker()
m = Marker()
acc.append(SingleInstruction('ENTER_SCOPE', 0))
flatten(branch.ifclause.conditionclause, acc)
acc.append(Branch(m))
flatten(branch.ifclause, acc)
acc.append(GoTo(m_end))
acc.append(m)
for elseifclause in branch.elseifclauses:
m = Marker()
flatten(elseifclause.conditionclause, acc)
acc.append(Branch(m))
flatten(elseifclause, acc)
acc.append(GoTo(m_end))
acc.append(m)
if branch.elseclause:
flatten(branch.elseclause, acc)
acc.append(m_end)
acc.append(SingleInstruction('LEAVE_SCOPE', 0))
elif isinstance(branch, TryStatement):
m_body = Marker()
m_end = Marker()
acc.append(SingleInstruction('ENTER_ERRHAND', m_body))
for handler in branch.catchclauses:
h_start = Marker()
h_end = Marker()
for ex in handler.exceptions:
acc.extend([
SingleInstruction('DUP', 0),
SingleInstruction('PUSH_LITERAL', ex),
SingleInstruction('JMPEQ', h_start),
])
acc.pop()
acc.extend([
SingleInstruction('JMPNE', h_end),
h_start,
SingleInstruction('DROP', 0),
])
flatten(handler, acc)
acc.extend([GoTo(m_end), h_end])
acc.append(SingleInstruction('RERAISE', 0))
acc.append(m_body)
flatten(branch.tryclause, acc)
acc.append(SingleInstruction('LEAVE_ERRHAND', 0))
acc.append(m_end)
return acc