-
Notifications
You must be signed in to change notification settings - Fork 1
/
plock.py
397 lines (346 loc) · 8.58 KB
/
plock.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
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
from sys import argv, stderr
def interpreter():
global stack,ref,pointer,functionMapper,program
stack = []
ref = []
pointer = 0
refrer()
runnableProgram = [code(val,i) for i,val in enumerate(program)]
stack_strict(runnableProgram)
while pointer < len(runnableProgram):
a,*i = runnableProgram[pointer]
functionMapper[a](*i)
pointer += 1
def refrer():
global ref, program
ref_stack = []
for i in range(len(program)):
if program[i] == 'start':
ref_stack.append(i)
elif program[i] == 'end':
ref.append((ref_stack.pop(),i))
def find(num):
global ref
for i,j in ref:
if i == num: return j
if j == num: return i
def lexer_file(programFile):
global program
try:
word = []
with open(programFile) as f:
word = f.readlines()
word = [i.split(sep="//")[0] for i in word]
for i,val in enumerate(word):
quote_count = val.count('"')
if not quote_count % 2 == 0:
stderr.write("ERROR: Missing quotation mark at line {}\n".format(i+1))
exit(1)
programCode = []
for i in word:
programCode.extend(i.split())
adding_stack = []
add_to_stack = False
for i in programCode:
adding_stack.append(i)
if i.count('"') == 1:
add_to_stack = not add_to_stack
if not add_to_stack:
program.append(' '.join(adding_stack))
adding_stack.clear()
except FileNotFoundError:
stderr.write("ERROR: Specified File not found {}\n".format(programFile))
exit(1)
stackConsumption ={
'DUMP' : (-1,1),
'ADD' : (-1,2),
'SUB' : (-1,2),
'MULT' : (-1,2),
'START' : (-1,1),
'END' : (-1,1),
'DIV' : (-1,2),
'EQ' : (-1,2),
'GT' : (-1,2),
'LT' : (-1,2),
'MOD' : (-1,2),
'DUP' : ( 1,1),
'PUSH' : ( 1,0),
'SWAP' : ( 0,2),
'DROP' : (-1,1),
'OVER' : ( 1,2),
'NOT' : ( 0,1),
'AND' : (-1,2),
'OR' : (-1,2),
'SHL' : (-1,2),
'SHR' : (-1,2),
'MEM' : ( 1,0),
'READ8' : ( 0,1),
'WRITE8' : (-2,2),
'READ16' : ( 0,1),
'WRITE16' : (-2,2),
'READ32' : ( 0,1),
'WRITE32' : (-2,2),
'READ64' : ( 0,1),
'WRITE64' : (-2,2),
'PUTS' : ( 0,1),
'PRINTF' : ( 0,1),
'SCANF' : ( 0,1),
'PUSHSTR' : ( 1,0),
}
def stack_strict(program):
global stackConsumption
stack_counter = 0
for pp,i in enumerate(program):
x = stackConsumption[i[0]]
if stack_counter < x[1]:
stderr.write("ERROR: Trying to read from empty stack at operation {}, {}\n".format(pp, i[0]))
exit(1)
stack_counter+=x[0]
opToFunctionCode = {
'.': 'DUMP' ,
'+': 'ADD' ,
'-': 'SUB' ,
'*': 'MULT' ,
'start': 'START' ,
'end': 'END' ,
'/': 'DIV' ,
'==': 'EQ' ,
'>': 'GT' ,
'<': 'LT' ,
'%': 'MOD' ,
'dup': 'DUP' ,
'swap': 'SWAP' ,
'drop': 'DROP' ,
'over': 'OVER' ,
'not': 'NOT' ,
'and': 'AND' ,
'or': 'OR' ,
'<<': 'SHL' ,
'>>': 'SHR' ,
'mem': 'MEM' ,
'read8': 'READ8' ,
'write8': 'WRITE8' ,
'read16': 'READ16' ,
'write16': 'WRITE16',
'read32': 'READ32' ,
'write32': 'WRITE32',
'read64': 'READ64' ,
'write64': 'WRITE64',
'puts': 'PUTS' ,
'printf': 'PRINTF' ,
'scanf': 'SCANF' ,
}
def code(op,pp):
global functionMapper, opToFunctionCode
#-----------Development Only -----------
global stackConsumption
assert len(opToFunctionCode) == len(stackConsumption)-2, "Discrepancy in opToFunctionCode"
#---------------------------------------
x = (0,)
if op in opToFunctionCode: x = (opToFunctionCode[op],)
elif op[0] == '"' : return ('PUSHSTR',op[1:-1])
else:
try:
x = int(op)
return ('PUSH',x)
except ValueError:
stderr.write("ERROR: Unknown instruction {} at instruction number {}\n".format(op,pp))
exit(1)
if op == 'start' or op == 'end': x = (*x, find(pp))
return x;
def start(val):
global stack
global pointer
if stack.pop() == 0:
pointer = val
def swap():
global stack
(stack[-1] ,stack[-2]) = (stack[-2] , stack[-1])
def over():
global stack
stack.append(stack[-2])
def drop():
global stack
stack.pop()
def mod():
global stack
a= stack.pop()
stack.append(stack.pop()%a)
def dup():
global stack
stack.append(stack[-1])
def end(val):
global stack, pointer
if stack.pop() == 0:
pointer = val
def push(val):
global stack
stack.append(val)
def pushstr(val):
global stack,memory
x = bytearray(val,'utf-8')
x.append(0x00)
stack.append(len(memory))
memory.extend(x)
def dump():
global stack
print(stack.pop())
def gt():
global stack
if stack.pop() < stack.pop() : stack.append(1)
else: stack.append(0)
def lt():
global stack
if stack.pop() > stack.pop() : stack.append(1)
else: stack.append(0)
def eq():
global stack
if stack.pop() == stack.pop(): stack.append(1)
else: stack.append(0)
def Not():
global stack
stack[-1] = int(not stack[-1])
def And():
global stack
a = stack.pop()
stack[-1] &= a
def Or():
global stack
a = stack.pop()
stack[-1] |= a
def div():
global stack
a = stack.pop()
stack.append(stack.pop()//a)
def mult():
global stack
stack.append(stack.pop()*stack.pop())
def sub():
global stack
a = stack.pop()
stack.append(stack.pop()-a)
def add():
global stack
stack.append(stack.pop()+stack.pop())
def mem():
global stack
stack.append(0) #Hard coded for now, must change in compilation mode
def write8():
global stack, memory
a = stack.pop()
memory[stack.pop()] = a
def write16():
global stack, memory
a = stack.pop()
b = stack.pop()
for i,val in enumerate(a.to_bytes(2,'big')):
memory[b+i] =val
def write32():
global stack, memory
a = stack.pop()
b = stack.pop()
for i,val in enumerate(a.to_bytes(4,'big')):
memory[b+i] =val
def write64():
global stack, memory
a = stack.pop()
b = stack.pop()
for i,val in enumerate(a.to_bytes(8,'big')):
memory[b+i] =val
def read8():
global stack, memory
stack.append(memory[stack.pop()])
def read16():
global stack, memory
a = stack.pop()
stack.append(int.from_bytes(memory[a:a+2],'big'))
def read32():
global stack, memory
a = stack.pop()
stack.append(int.from_bytes(memory[a:a+4],'big'))
def read64():
global stack, memory
a = stack.pop()
stack.append(int.from_bytes(memory[a:a+8],'big'))
def shl():
global stack
a = stack.pop()
stack[-1] = stack[-1] << a
def shr():
global stack
a = stack.pop()
stack[-1] = stack[-1] >> a
#Switch these to external functions
def printf():
a = stack.pop()
x = memory[a]
buf = []
while not x == 0:
buf.append(chr(x))
a += 1
x = memory[a]
print(''.join(buf),end="")
stack.append(len(buff))
def puts():
a = stack.pop()
x = memory[a]
buf = []
while not x == 0:
buf.append(chr(x))
a += 1
x = memory[a]
print(''.join(buf))
stack.append(len(buf)+1)
def scanf():
pass
functionMapper ={
'DUMP' :dump,
'ADD' :add,
'SUB' :sub,
'MULT' :mult,
'START' :start,
'END' :end,
'DIV' :div,
'EQ' :eq,
'GT' :gt,
'LT' :lt,
'MOD' :mod,
'DUP' :dup,
'PUSH' :push,
'SWAP' :swap,
'DROP' :drop,
'OVER' :over,
'NOT' :Not,
'AND' :And,
'OR' :Or,
'MEM' :mem,
'WRITE8' :write8,
'READ8' :read8,
'WRITE16' :write16,
'READ16' :read16,
'WRITE32' :write32,
'READ32' :read32,
'WRITE64' :write64,
'READ64' :read64,
'PUSHSTR' :pushstr,
'PUTS' :puts,
'PRINTF' :printf,
'SCANF' :scanf,
'SHL' :shl,
'SHR' :shr,
}
stack = []
ref = []
pointer = 0
MAX_MEMORY = 64000
memory = bytearray(MAX_MEMORY)
program = []
if __name__ == "__main__":
if len(argv) < 2:
stderr.write("ERROR: No argument given\nUSAGE: plock.py <FILENAME>\n")
exit(1)
lexer_file(argv[1])
#Development only
assert len(functionMapper) == len(stackConsumption), "Discrepancy in functionMapper"
assert len(stackConsumption) == len(stackConsumption), "Discrepancy in stackConsumption"
interpreter()