-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitespace.py
353 lines (336 loc) · 12.2 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
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
import sys
import os.path
DEBUG = 0
def debugPrint(arg):
if DEBUG:
print(arg,end='')
if len(sys.argv) < 2:
print("No file to run")
exit()
elif len(sys.argv) > 2:
print("Too many input arguments")
exit()
elif sys.argv[1][-3:] != '.ws':
print("Wrong filetype")
exit()
elif not os.path.isfile(sys.argv[1]):
print("File not found")
exit()
with open(sys.argv[1]) as f:
text = f.read()
chars = []
for c in text:
if c in [' ', '\t', '\n']:
chars.append(c)
heap = {}
jumps = {}
#jumps = []
stack = []
calls = []
charPointer = 0
imp = ['0','0']
command = ['0','0']
parse = True
#print(chars)
#print(len(chars))
while charPointer<len(chars) and parse:
imp[0] = chars[charPointer]
charPointer += 1
if imp == [' ','0']: # Stack manipulation
#debugPrint(f'Stack manipulation ')
command[0] = chars[charPointer]
charPointer+=1
if command == [' ','0']:
while chars[charPointer] != '\n':
charPointer += 1
charPointer += 1
else:
command[1] = chars[charPointer]
charPointer += 1
elif imp == ['\n','0']: #Flow control
command[0] = chars[charPointer]
charPointer += 1
command[1] = chars[charPointer]
charPointer += 1
if command == [' ',' ']: # Set label
number = 0
while chars[charPointer] != '\n':
number = number * 2
if chars[charPointer] == '\t':
number = number + 1
charPointer += 1
debugPrint(f'Label created: {number} at {charPointer}\n')
jumps[number] = charPointer
debugPrint(f'{jumps}\n')
charPointer += 1
debugPrint(f'{jumps}\n')
elif command == [' ','\t']: # call subroutine
while chars[charPointer] != '\n':
charPointer += 1
charPointer += 1
elif command == [' ','\n']: #Jump to label
while chars[charPointer] != '\n':
charPointer += 1
charPointer += 1
elif command == ['\t',' ']: #Jump to label if top of stack is 0
while chars[charPointer] != '\n':
charPointer += 1
charPointer += 1
elif command == ['\t','\t']: #Jump to label if top of stack is < 0
while chars[charPointer] != '\n':
charPointer += 1
charPointer += 1
elif command == ['\t','\n']: #End subroutine and transfer control to caller
pass
#charPointer += 1
elif command == ['\n','\n']: #End program
#print("Here too?")
parse = False
else:
imp[1] = chars[charPointer]
charPointer += 1
if imp ==['\t',' ']: #Arithmetic
command[0] = chars[charPointer]
charPointer += 1
command[1] = chars[charPointer]
charPointer += 1
elif imp == ['\t','\t']: #Heap accsess
command[0] = chars[charPointer]
charPointer += 1
elif imp == ['\t','\n']: #I/O
command[0] = chars[charPointer]
charPointer += 1
command[0] = chars[charPointer]
charPointer += 1
else:
pass
#print(1)
#exit()
imp = ['0','0']
command = ['0','0']
charPointer = 0
parse = True
#print(jumps)
while charPointer<len(chars) and parse:
#if len(stack)>0 and stack[-1] > 11:
# exit()
#print('here')
imp[0] = chars[charPointer]
charPointer += 1
if imp == [' ','0']: #Stack Manipulation
debugPrint(f'Stack manipulation: ')
command[0] = chars[charPointer]
charPointer += 1
if command == [' ','0']: # Push number to stack
sign = 0
number = 0
while chars[charPointer] != '\n':
#print(number)
if not sign:
if chars[charPointer] == ' ':
sign = 1
else:
sign = -1
else:
number = number * 2
if chars[charPointer] == '\t':
number += 1
charPointer += 1
charPointer += 1
#print(number)
debugPrint(f'Push {sign*number} on to stack\n')
stack.append(sign*number)
else:
command[1] = chars[charPointer]
charPointer += 1
if command == ['\n',' ']: #Duplicate top number
debugPrint(f'Duplicate top number\n')
stack.append(stack[-1])
elif command == ['\n','\t']: #Swap top numbers
debugPrint(f'swap top numbers\n')
'''
stack[-1] = a
stack[-2] = b
a = a_init - b_init
a = a - b
b = a_init - b_inint + b_inint = a_inint
b = a + b
a = a_init - (a_inint - b_inint) = b_init
a = b - a
'''
stack[-1] = stack[-1] - stack[-2]
stack[-2] = stack[-1] + stack[-2]
stack[-1] = stack[-2] - stack[-1]
elif command == ['\n','\n']: #Discard top number
debugPrint(f'discard top number\n')
stack.pop()
else: # No correct stack manipulation
debugPrint(f'No correct stack manipulation\n')
exit()
debugPrint(f'Stack: {stack}\n')
elif imp == ['\n','0']: #Flow control
debugPrint(f'Flow control: ')
command[0] = chars[charPointer]
charPointer += 1
command[1] = chars[charPointer]
charPointer += 1
if command == [' ',' ']: # Set label
debugPrint(f'passed a label\n')
while chars[charPointer] != '\n':
charPointer += 1
charPointer += 1
elif command == [' ','\t']: # Call a subroutine
debugPrint('call subroutine')
number = 0
while chars[charPointer] != '\n':
number = number * 2
if chars[charPointer] == '\t':
number += 1
charPointer += 1
calls.append(charPointer)
debugPrint(f'({number})\n')
charPointer = jumps[number]
'''
for jump in jumps:
if jump[0] == number:
charPointer = jump[1]
'''
charPointer += 1
elif command == [' ','\n']: #Jump to label
debugPrint(f'Jump to label ')
number = 0
while chars[charPointer] != '\n':
number = number * 2
if chars[charPointer] == '\t':
number += 1
charPointer += 1
debugPrint(f'({number})\n')
charPointer = jumps[number]
'''
for jump in jumps:
if jump[0] == number:
charPointer = jump[1]
'''
#print(charPointer)
charPointer += 1
elif command == ['\t',' ']: #Jump to label if top of stack is 0
debugPrint(f'Jump to label ')
number = 0
while chars[charPointer] != '\n':
number = number * 2
if chars[charPointer] == '\t':
number += 1
charPointer += 1
debugPrint(f'({number}) if stack ({stack[-1]}) is 0 ({stack[-1] == 0})\n')
if stack.pop() == 0:
charPointer = jumps[number]
'''
for jump in jumps:
if jump[0] == number:
charPointer = jump[1]
'''
charPointer += 1
elif command == ['\t','\t']: #Jump to label if top of stack is < 0
debugPrint(f'Jump to label ')
number = 0
while chars[charPointer] != '\n':
number = number * 2
if chars[charPointer] == '\t':
number += 1
charPointer += 1
debugPrint(f'({number}) if stack ({stack[-1]}) is < 0 ({stack[-1] < 0})\n')
print('in here')
if stack.pop() < 0:
charPointer = jumps[number]
'''
for jump in jumps:
if jump[0] == number:
charPointer = jump[1]
'''
charPointer += 1
elif command == ['\t','\n']: #End subroutine and transfer control to caller
charPointer = calls.pop()
debugPrint(f'End subroutine and transfer control to caller ({charPointer})\n')
charPointer += 1
elif command == ['\n','\n']: #End program
debugPrint(f'End of program\n')
parse = 0
print()
exit()
else: # No correct flow control
debugPrint('No correct flow control\n')
exit()
else:
imp[1] = chars[charPointer]
charPointer += 1
if imp == ['\t',' ']: #Arithmetic
debugPrint(f'Arithmetic: ')
command[0] = chars[charPointer]
charPointer += 1
command[1] = chars[charPointer]
charPointer += 1
if command == [' ',' ']: #Addition
debugPrint(f'{stack[-2]} + {stack[-1]}\n')
stack.append(stack.pop() + stack.pop())
elif command == [' ','\t']: #Subtraction
debugPrint(f'{stack[-2]} - {stack[-1]}\n')
stack.append(-stack.pop() + stack.pop())
elif command == [' ','\n']: #Multiplication
debugPrint(f'{stack[-2]} * {stack[-1]}\n')
stack.append(stack.pop() * stack.pop())
elif command == ['\n',' ']: #Integer division
debugPrint(f'{stack[-2]} / {stack[-1]}\n')
stack.append(int(1//(stack.pop()/stack.pop())))
elif command == ['\t','\t']: #Modulo
debugPrint(f'{stack[-2]} % {stack[-1]}\n')
mod = stack.pop()
stack.append(stack.pop()%mod)
else: #No correct arithmetic
exit()
debugPrint(f'Stack: {stack}\n')
elif imp == ['\t','\t']: # Heap access
debugPrint(f'Heap access: ')
command[0] = chars[charPointer]
charPointer += 1
if command == [' ','0']: # Store in heap
number = stack.pop()
location = stack.pop()
heap[location] = number
debugPrint(f'store {number} at {location} in heap\n')
elif command == ['\t','0']:
location = stack.pop()
stack.append(heap[location])
debugPrint(f'retreve {stack[-1]} from heap location {location}\n')
else: # no correct heap access
exit()
debugPrint(f'Stack: {stack}\n')
elif imp == ['\t','\n']: # I/O
debugPrint(f'I/O: ')
command[0] = chars[charPointer]
charPointer += 1
command[1] = chars[charPointer]
charPointer += 1
if command == [' ',' ']: #Output char at top of stack
debugPrint(f'print char: ')
print(f'{chr(stack.pop())}',end='')
debugPrint(f'\n')
elif command == [' ','\t']: #Output number at top of stack
debugPrint(f'print number: ')
print(f'{stack.pop()}',end='')
debugPrint(f'\n')
elif command == ['\t',' ']: #read a char and place it in location given by top if stack
location = stack.pop()
debugPrint(f'read char and store it in heap location {location}: ')
heap[location] = ord(input())
elif command == ['\t','\t']: #read a number and place it in lication given by top of stack
location = stack.pop()
debugPrint(f'read number and store it in heap location {location}: ')
heap[location] = int(input())
else: #No correct I/O
exit()
debugPrint(f'Stack: {stack}\n')
else: # No correct imp
exit()
imp = ['0','0']
command = ['0','0']
exit()