-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.py
More file actions
144 lines (129 loc) · 4.33 KB
/
interpreter.py
File metadata and controls
144 lines (129 loc) · 4.33 KB
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
#!/usr/bin/env python
# https://www.codewars.com/kata/58e61f3d8ff24f774400002c/train/python
import sys
from collections import deque
def assembler_interpreter(program):
# * Init
pc = 0
callstack = deque()
registers = {}
labels = {}
cmpres = 0
output = ''
# * Preprocessing - delete comment stuff; process labels
# del comments
for idx, line in enumerate(program):
if ';' in line:
program[idx] = line[:line.index(';')]
# fuck commas
for idx, line in enumerate(program):
if ',' in line and not 'msg' in line:
program[idx] = ''.join(filter(lambda c: c != ',', line))
# deleting extra spaces
program = [line.strip() for line in program]
# del empty lines
def isnotempty(line): return line
program = list(filter(isnotempty, program))
# indexing labels
for idx, line in enumerate(program):
if line[-1]==':':
labels[line.strip()[:-1]] = idx
del program[idx]
# * Runtime
while pc < len(program):
splited_line = program[pc].split()
cmd, args = splited_line[0], splited_line[1:]
if cmd == 'mov':
if args[1] in registers:
registers[args[0]] = registers[args[1]]
else:
registers[args[0]] = int(args[1])
elif cmd == 'inc':
registers[args[0]] = registers[args[0]]+1
elif cmd == 'dec':
registers[args[0]] = registers[args[0]]-1
elif cmd == 'add':
if args[1] in registers:
registers[args[0]] = registers[args[0]]+registers[args[1]]
else:
registers[args[0]] = registers[args[0]]+int(args[1])
elif cmd == 'sub':
if args[1] in registers:
registers[args[0]] = registers[args[0]]-registers[args[1]]
else:
registers[args[0]] = registers[args[0]]-int(args[1])
elif cmd == 'div':
if args[1] in registers:
registers[args[0]] = registers[args[0]]//registers[args[1]]
else:
registers[args[0]] = registers[args[0]]//int(args[1])
elif cmd == 'mul':
if args[1] in registers:
registers[args[0]] = registers[args[0]]*registers[args[1]]
else:
registers[args[0]] = registers[args[0]]*int(args[1])
elif cmd == 'jmp':
pc = labels[args[0]]
continue
elif cmd == 'cmp':
if args[1] in registers:
cmpres = registers[args[0]]-registers[args[1]]
else:
cmpres = registers[args[0]]-int(args[1])
elif cmd == 'jne':
if cmpres != 0:
pc = labels[args[0]]
continue
elif cmd == 'je':
if cmpres == 0:
pc = labels[args[0]]
continue
elif cmd == 'jge':
if cmpres >= 0:
pc = labels[args[0]]
continue
elif cmd == 'jg':
if cmpres > 0:
pc = labels[args[0]]
continue
elif cmd == 'jle':
if cmpres <= 0:
pc = labels[args[0]]
continue
elif cmd == 'jl':
if cmpres < 0:
pc = labels[args[0]]
continue
elif cmd == 'call':
callstack.append(pc)
pc = labels[args[0]]
continue
elif cmd == 'ret':
pc = callstack.pop()
elif cmd == 'msg':
msg = program[pc][3:]
strliteral = False
for char in msg:
if not strliteral and char=="'":
strliteral = True
elif strliteral and char=="'":
strliteral = False
elif strliteral:
output = output+char
elif not strliteral and char.isalpha():
output = output+str(registers[char])
elif cmd == 'end':
return output
pc = pc+1
return -1
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: interpreter.py [program path]")
exit(-1)
try:
f = open(sys.argv[1], 'r')
except:
print("Wrong arg!")
exit(-1)
prog = f.read().splitlines()
output = assembler_interpreter(prog)