forked from Smithers888/BlueSpace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimiser.py
263 lines (249 loc) · 11.1 KB
/
optimiser.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
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
#!/usr/bin/env python3
# Copyright (C) 2014 Christopher Smith
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name of the author shall not be used
# in advertising or otherwise to promote the sale, use or other dealings in
# this Software without prior written authorization from the author.
_arithmap = {
'\t ': '+',
'\t \t': '-',
'\t \n': '*',
'\t \t ': '//',
'\t \t\t': '%',
}
class _optimiser:
def __init__(self):
self.result = [
'#!/usr/bin/env python3',
'import sys',
'stack = []',
'heap = {}',
'def run(f):',
' while f is not None:',
' f = f()',
'def start():',
]
self.xvars = []
self.reachable = True
self.needpass = True
self.checkheap = False
def _stackval(self, i):
j = len(self.xvars) - i - 1
if j >= 0:
if self.xvars[j] == None:
return 'x' + str(j)
else:
return str(self.xvars[j])
else:
return 'stack[' + str(j) + ']'
def _stackvar(self, i):
j = len(self.xvars) - i - 1
if j >= 0:
return 'x' + str(j)
else:
return 'stack[' + str(j) + ']'
def _dumpxs(self):
for i in range(len(self.xvars) - 1, -1, -1):
self.result.append(' stack.append(' + self._stackval(i) + ')')
self.xvars.clear()
self.checkheap = False
def _dumpxs2(self):
for i in range(len(self.xvars) - 1, -1, -1):
self.result.append(' stack.append(' + self._stackval(i) + ')')
def addstatement(self, stmt):
rep = stmt.rep
if not self.reachable:
if rep == '\n ': # Label
self.xvars.clear()
self.result.append('def ' + stmt.param.name() + '():')
self.reachable = True
self.needpass = True
return
if self.needpass:
self.needpass = False
if rep == '\n\t\n': # Return
self.result.append(' pass')
self.reachable = False
return
if rep == ' ': # Push
self.xvars.append(stmt.param.value)
elif rep == ' \n ': # Duplicate
if len(self.xvars) == 0:
self.xvars.append('stack[-1]')
elif self.xvars[-1] is None:
self.result.append(' x' + str(len(self.xvars)) + ' = ' + self._stackval(0))
self.xvars.append(None)
else:
self.xvars.append(self.xvars[-1])
elif rep == ' \t ': # Copy
value = stmt.param.value
if len(self.xvars) <= value or self.xvars[-1 - value] is None:
self.result.append(' x' + str(len(self.xvars)) + ' = ' + self._stackval(value))
self.xvars.append(None)
else:
self.xvars.append(self.xvars[-1 - value])
elif rep == ' \n\t': # Swap
if len(self.xvars) == 0:
self.result.append(' stack[-1], stack[-2] = stack[-2], stack[-1]')
elif len(self.xvars) == 1:
self.result.append(' x0, stack[-1] = stack[-1], ' + self._stackval(0))
self.xvars[0] = None
else:
self.result.append(' ' + self._stackvar(0) + ', ' + self._stackvar(1) + ' = ' + self._stackval(1) + ', ' + self._stackval(0))
self.xvars[-1] = None
self.xvars[-2] = None
elif rep == ' \n\n': # Discard
if len(self.xvars) == 0:
self.result.append(' stack.pop()')
else:
self.xvars.pop()
elif rep == ' \t\n': # Slide
self._dumpxs()
self.result.append(' del stack[-' + str(stmt.param.value + 1) + ':-1]')
elif rep.startswith('\t '): # Arithmetic
if len(self.xvars) == 0:
self.result.append(' x0 = stack.pop()')
self.result.append(' stack[-1] ' + _arithmap[rep] + '= x0')
elif len(self.xvars) == 1:
self.result.append(' stack[-1] ' + _arithmap[rep] + '= ' + self._stackval(0))
self.xvars.pop()
elif self.xvars[-2] is None:
self.result.append(' ' + self._stackvar(1) + ' ' + _arithmap[rep] + '= ' + self._stackval(0))
self.xvars.pop()
elif self.xvars[-1] is None:
self.result.append(' ' + self._stackvar(1) + ' = ' + self._stackval(1) + _arithmap[rep] + ' ' + self._stackval(0))
self.xvars[-2] = None
self.xvars.pop()
else:
self.xvars[-2] = '(' + str(self.xvars[-2]) + ' ' + _arithmap[rep] + ' ' + str(self.xvars[-1]) + ')'
self.xvars.pop()
elif rep == '\t\t ': # Store
if self.checkheap:
for i in len(self.xvars):
if self.xvars[i] is not None and 'heap' in self.xvars[i]:
self.result.append(' x' + str(i) + ' = ' + str(self.xvars[i]))
self.xvars[i] = None
self.checkheap = False
if len(self.xvars) == 0:
self.result.append(' x0 = stack.pop()')
self.result.append(' heap[stack.pop()] = x0')
elif len(self.xvars) == 1:
self.result.append(' heap[stack.pop()] = ' + self._stackval(0))
self.xvars.pop()
else:
self.result.append(' heap[' + self._stackval(1) + '] = ' + self._stackval(0))
self.xvars.pop()
self.xvars.pop()
elif rep == '\t\t\t': # Retrieve
if len(self.xvars) == 0:
self.result.append(' stack[-1] = heap[stack[-1]]')
else:
self.xvars[-1] = 'heap[' + self._stackval(0) + ']'
self.checkheap = True
elif rep == '\n ': # Label
self._dumpxs()
self.result.append(' return ' + stmt.param.name())
self.result.append('def ' + stmt.param.name() + '():')
self.needpass = True
elif rep == '\n \t': # Call
self._dumpxs()
self.result.append(' run(' + stmt.param.name() + ')')
elif rep == '\n \n': # Jump
self._dumpxs()
self.result.append(' return ' + stmt.param.name())
self.reachable = False
elif rep == '\n\t ': # JumpZero
if len(self.xvars) >= 1:
self.result.append(' if ' + self._stackval(0) + ' == 0:')
self.xvars.pop()
self._dumpxs2()
else:
self.result.append(' if stack.pop() == 0:')
self.result.append(' return ' + stmt.param.name())
elif rep == '\n\t\t': # JumpNegative
if len(self.xvars) >= 1:
self.result.append(' if ' + self._stackval(0) + ' < 0:')
self.xvars.pop()
self._dumpxs2()
else:
self.result.append(' if stack.pop() < 0:')
self.result.append(' return ' + stmt.param.name())
elif rep == '\n\t\n': # Return
self._dumpxs()
self.reachable = False
elif rep == '\n\n\n': # End
self.result.append(' sys.exit()')
self.reachable = False
elif rep == '\t\n ': # OutputChar
if len(self.xvars) >= 1:
if isinstance(self.xvars[-1], int):
if self.xvars[-1] == 0x27:
self.result.append(' sys.stdout.write("\'")')
elif 0x20 <= self.xvars[-1] <= 0x7f:
self.result.append(" sys.stdout.write('" + chr(self.xvars[-1]) + "')")
elif 0x00 <= self.xvars[-1] <= 0xff:
self.result.append(" sys.stdout.write('\\x{:02x}')".format(self.xvars[-1]))
else:
self.result.append(' sys.stdout.write(chr(' + str(self.xvars[-1]) + '))')
else:
self.result.append(' sys.stdout.write(chr(' + self._stackval(0) + '))')
self.xvars.pop()
else:
self.result.append(' sys.stdout.write(chr(stack.pop()))')
elif rep == '\t\n \t': # OutputNum
if len(self.xvars) >= 1:
if isinstance(self.xvars[-1], int):
self.result.append(" sys.stdout.write('" + str(self.xvars[-1]) + "')")
else:
self.result.append(' sys.stdout.write(str(' + self._stackval(0) + '))')
self.xvars.pop()
else:
self.result.append(' sys.stdout.write(str(stack.pop()))')
elif rep == '\t\n\t ': # ReadChar
self.result.append(' sys.stdout.flush()')
self.result.append(' try:')
self.result.append(' x' + str(len(self.xvars)) + ' = ord(sys.stdin.read(1))')
self.result.append(' except EOFError:')
self.result.append(' x' + str(len(self.xvars)) + ' = -1')
if len(self.xvars) >= 1:
self.result.append(' heap[' + self._stackval(0) + '] = x' + str(len(self.xvars)))
self.xvars.pop()
else:
self.result.append(' heap[stack.pop()] = x0')
elif rep == '\t\n\t\t': # ReadNum
self.result.append(' sys.stdout.flush()')
if len(self.xvars) >= 1:
self.result.append(' heap[' + self._stackval(0) + '] = int(sys.stdin.readline())')
self.xvars.pop()
else:
self.result.append(' heap[stack.pop()] = int(sys.stdin.readline())')
def finish(self):
if self.reachable:
self.result.append(' raise IndexError')
self.result.extend((
'run(start)',
'raise IndexError',
))
def optimise(program):
result = _optimiser()
for stmt in program:
result.addstatement(stmt)
result.finish()
return '\n'.join(result.result)