-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreterMain.py
executable file
·76 lines (69 loc) · 2.57 KB
/
interpreterMain.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
#!/usr/bin/env python2
import os
import sys
from collections import OrderedDict
def setcodes(num, registers):
registers["n"] = False
registers["z"] = False
registers["p"] = False
if num < 0: registers["n"] = True
if num == 0: registers["z"] = True
if num > 0: registers["p"] = True
def execute(lines):
registers = OrderedDict([("r0",0), ("r1",0), ("r2",0), ("r3",0), ("r4",0), ("r5",0), ("r6",0), ("r7",0), ("pc",0), ("p",0), ("z",0), ("n",0)])
memory = [int(x) for x in lines.pop(0).split(',')]
registers["pc"] = 0
while registers["pc"] < len(lines):
line = lines[registers["pc"]]
words = line.split(" ")
if words[0] == "zero_reg":
registers[words[1]] = 0
setcodes(0, registers)
elif words[0] == "add":
try:
num = int(words[3])
except ValueError:
num = registers[words[3]]
registers[words[5]] = registers[words[1]] + num
setcodes(registers[words[5]], registers)
elif words[0] == "sub":
try:
num = int(words[3])
except ValueError:
num = registers[words[3]]
registers[words[5]] = registers[words[1]] - num
setcodes(registers[words[5]], registers)
elif words[0] == "load":
registers[words[1]] = memory[registers[words[3][1:3]] + int(words[3][3:].strip("]"))]
setcodes(registers[words[1]], registers)
elif words[0] == "store":
memory[registers[words[3][1:3]] + int(words[3][4:].strip("]"))] = registers[words[1]]
setcodes(memory[registers[words[3][1:3]] + int(words[3][4:].strip("]"))], registers)
elif words[0][:2] == "br":
for letter in words[0][3:]:
if registers[letter]: registers["pc"] = registers["pc"] + int(words[1])
registers["pc"] += 1
print ((line if line else "Result") + ":").ljust(25),\
" ".join(": ".join(str(i) for i in item).ljust(10) for item in registers.items()),\
memory
def run(fp):
program_contents = []
while True:
read = os.read(fp, 4096).split("\n")
if len(read) == 1:
break
program_contents += read
execute(program_contents)
os.close(fp)
def entry_point(argv):
try:
filename = argv[1]
except IndexError:
print("You must supply a filename")
return 1
run(os.open(filename, os.O_RDONLY, 0o0777))
return 0
def target(*args):
return entry_point, None
if __name__ == "__main__":
entry_point(sys.argv)