-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitespace.py
313 lines (306 loc) · 7.97 KB
/
whitespace.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
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
# Copyright (c) 2014 MikeCAT
#
# This software is provided 'as-is', without any express or implied warranty.
# In no event will the authors be held liable for any damages arising from the
# use of this software.
#
# Permission is granted to anyone to use this software for any purpose, including
# commercial applications, and to alter it and redistribute it freely, subject to
# the following restrictions:
#
# 1. The origin of this software must not be misrepresented; you must not claim
# that you wrote the original software. If you use this software in a product,
# an acknowledgment in the product documentation would be appreciated but is
# not required.
#
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
#
# 3. This notice may not be removed or altered from any source distribution.
import sys
# check if the stack contains at least needed elements
def check_stack(stack,needed):
if len(stack)<needed:
print("stack underflow")
quit()
return True
# execute translated programs
def run_whitespace(program,input,realtime_io=False):
i=0
commands=program[0]
labels=program[1]
stack=[]
heap={}
subroutine_stack=[]
input_reading_pos=0
now_input=[]
output=""
while i<len(commands):
if commands[i][0]==11:
stack.append(commands[i][1])
elif commands[i][0]==12:
check_stack(stack,1)
stack.append(stack[-1])
elif commands[i][0]==13:
check_stack(stack,commands[i][1]+1)
stack.append(stack[-1-commands[i][1]])
elif commands[i][0]==14:
check_stack(stack,2)
temp=stack[-2]
stack[-2]=stack[-1]
stack[-1]=temp
elif commands[i][0]==15:
check_stack(stack,1)
stack.pop()
elif commands[i][0]==16:
check_stack(stack,commands[i][1]+1)
stack[-1-commands[i][1]:-1]=[]
elif commands[i][0]==21:
check_stack(stack,2)
stack[-2]+=stack[-1]
stack.pop()
elif commands[i][0]==22:
check_stack(stack,2)
stack[-2]-=stack[-1]
stack.pop()
elif commands[i][0]==23:
check_stack(stack,2)
stack[-2]*=stack[-1]
stack.pop()
elif commands[i][0]==24:
check_stack(stack,2)
stack[-2]//=stack[-1]
stack.pop()
elif commands[i][0]==25:
check_stack(stack,2)
stack[-2]%=stack[-1]
stack.pop()
elif commands[i][0]==31:
check_stack(stack,2)
heap[stack[-2]]=stack[-1]
stack.pop()
stack.pop()
elif commands[i][0]==32:
check_stack(stack,1)
if stack[-1] in heap:
stack[-1]=heap[stack[-1]]
else:
stack[-1]=0
elif commands[i][0]==41:
True # do nothing
elif commands[i][0]==42:
subroutine_stack.append(i)
if commands[i][1] not in labels:
print("unknown label")
quit()
i=labels[commands[i][1]]
elif commands[i][0]==43:
if commands[i][1] not in labels:
print("unknown label")
quit()
i=labels[commands[i][1]]
elif commands[i][0]==44:
check_stack(stack,1)
if stack[-1]==0:
if commands[i][1] not in labels:
print("unknown label")
quit()
i=labels[commands[i][1]]
stack.pop()
elif commands[i][0]==45:
check_stack(stack,1)
if stack[-1]<0:
if commands[i][1] not in labels:
print("unknown label")
quit()
i=labels[commands[i][1]]
stack.pop()
elif commands[i][0]==46:
if len(subroutine_stack)==0:
print("return without call")
quit()
i=subroutine_stack[-1]
subroutine_stack.pop()
elif commands[i][0]==47:
break
elif commands[i][0]==51:
check_stack(stack,1)
if realtime_io:
sys.stdout.write(chr(stack[-1]))
sys.stdout.flush()
output=output+chr(stack[-1])
stack.pop()
elif commands[i][0]==52:
check_stack(stack,1)
if realtime_io:
sys.stdout.write(str(stack[-1]))
sys.stdout.flush()
output=output+str(stack[-1])
stack.pop()
elif commands[i][0]==53:
check_stack(stack,1)
if realtime_io:
if now_input==[]:
heap[stack[-1]]=ord(sys.stdin.read(1))
else:
heap[stack[-1]]=ord(now_input)
now_input=[]
else:
if input_reading_pos<len(input):
heap[stack[-1]]=ord(input[input_reading_pos])
input_reading_pos+=1
else:
heap[stack[-1]]=-1
stack.pop()
elif commands[i][0]==54:
check_stack(stack,1)
now_number=0
sign=1
if realtime_io:
while now_input==[] or now_input.isspace():
now_input=sys.stdin.read(1)
if now_input=="-":
sign=-1
now_input=sys.stdin.read(1)
while now_input.isdigit():
now_number=now_number*10+int(now_input)
now_input=sys.stdin.read(1)
else:
while input_reading_pos<len(input) and input[input_reading_pos].isspace():
input_reading_pos+=1
if input_reading_pos<len(input) and input[input_reading_pos]=="-":
sign=-1
input_reading_pos+=1
while input_reading_pos<len(input) and input[input_reading_pos].isdigit():
now_number=now_number*10+int(input[input_reading_pos])
input_reading_pos+=1
heap[stack[-1]]=sign*now_number
stack.pop()
i+=1
return output
# read the next number
def read_number_ws(str):
ret=0
i=1
while i<len(str) and str[i]!="\n":
ret*=2
if str[i]=="\t":
ret+=1
i+=1
if str[0]=="\t":
ret=-ret
return ret
# read the next label string
def read_label_ws(str):
return str[:str.index("\n")]
# translate Whitespace program into a list of commands and labels
def compile_whitespace(pstr_raw):
pstr=""
for c in pstr_raw:
if c==" " or c=="\t" or c=="\n":
pstr=pstr+c
i=0
program=[]
labels={}
while i<len(pstr):
pi=i
if pstr[i]==" ":
if pstr[i+1]==" ":
program.append((11,read_number_ws(pstr[i+2:])))
i+=2+len(read_label_ws(pstr[i+2:]))+1
elif pstr[i+1]=="\n":
if pstr[i+2]==" ":
program.append((12,0))
i+=3
elif pstr[i+2]=="\t":
program.append((14,0))
i+=3
elif pstr[i+2]=="\n":
program.append((15,0))
i+=3
elif pstr[i+1]=="\t":
if pstr[i+2]==" ":
program.append((13,read_number_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i+2]=="\n":
program.append((16,read_number_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i]=="\t" and pstr[i+1]==" ":
if pstr[i+2]==" ":
if pstr[i+3]==" ":
program.append((21,0))
i+=4
elif pstr[i+3]=="\t":
program.append((22,0))
i+=4
elif pstr[i+3]=="\n":
program.append((23,0))
i+=4
elif pstr[i+2]=="\t":
if pstr[i+3]==" ":
program.append((24,0))
i+=4
elif pstr[i+3]=="\t":
program.append((25,0))
i+=4
elif pstr[i]=="\t" and pstr[i+1]=="\t":
if pstr[i+2]==" ":
program.append((31,0))
i+=3
elif pstr[i+2]=="\t":
program.append((32,0))
i+=3
elif pstr[i]=="\n":
if pstr[i+1]==" ":
if pstr[i+2]==" ":
labels[read_label_ws(pstr[i+3:])]=len(program)
program.append((41,read_label_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i+2]=="\t":
program.append((42,read_label_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i+2]=="\n":
program.append((43,read_label_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i+1]=="\t":
if pstr[i+2]==" ":
program.append((44,read_label_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i+2]=="\t":
program.append((45,read_label_ws(pstr[i+3:])))
i+=3+len(read_label_ws(pstr[i+3:]))+1
elif pstr[i+2]=="\n":
program.append((46,0))
i+=3
elif pstr[i+1]=="\n" and pstr[i+2]=="\n":
program.append((47,0))
i+=3
elif pstr[i]=="\t" and pstr[i+1]=="\n":
if pstr[i+2]==" ":
if pstr[i+3]==" ":
program.append((51,0))
i+=4
elif pstr[i+3]=="\t":
program.append((52,0))
i+=4
elif pstr[i+2]=="\t":
if pstr[i+3]==" ":
program.append((53,0))
i+=4
elif pstr[i+3]=="\t":
program.append((54,0))
i+=4
if pi==i:
print("invalid program")
quit()
return (program,labels)
if len(sys.argv)==2:
f=open(sys.argv[1],"r")
program=""
for line in f:
program=program+line
f.close()
compiled_program=compile_whitespace(program)
run_whitespace(compiled_program,"",True)
else:
print("Usage: "+sys.argv[0]+" <program file>")