-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
76 lines (62 loc) · 1.12 KB
/
utils.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
def num(a, bitlen=8):
c = ""
bitstring = bin(a)[2:].zfill(bitlen)
for index, bit in enumerate(bitstring):
c += "²"
if bit == "1":
c += "+"
return c
def clear():
return "½"*8
def pair(a,b):
#can omit zfill here, but only if memory cell zero beforehand
return num(a,4)+num(b,4)
"""
print(num(255))
print(pair(1,0))
print(clear())
"""
def asm(p):
result = ""
i = 0
def read(n):
nonlocal i
end = i+n
data = p[i:end]
i = end
return data
def readi():
nonlocal i
num = ""
while p[i] in "0123456789":
num += p[i]
i += 1
return int(num)
def ass(c):
assert read(len(c)) == c
def readargs(n):
args = []
ass("(")
for j in range(n):
args.append(readi())
if j != n-1:
ass(",")
ass(")")
return args
while True:
if i >= len(p):
break
op = p[i]
if op in "ca":
read(1)
result += pair(*readargs(2)) + op + "\n"#num(0) + "\n"
elif op in "dm":
read(1)
result += num(readargs(1)[0]) + op + "\n"#num(0) + "\n"
elif op in "0123456789":
result += num(readi()) + "\n"
elif op in "\n \t":
read(1)
else:
result += read(1) + "\n"#num(0) + "\n"
return result