-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.py
executable file
·78 lines (74 loc) · 2.09 KB
/
asm.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
def get_ops():
""" Builds an opcode name <-> value dictionary """
li = ["EOF","ADD","SUB","MUL","DIV","POW","BITAND","BITOR","CMP","GET", \
"SET","NUMBER","STRING","GGET","GSET","MOVE","DEF","PASS", \
"JUMP","CALL","RETURN","IF","DEBUG","EQ","LE","LT","DICT", \
"LIST","NONE","LEN","LINE","PARAMS","IGET","FILE","NAME", \
"NE","HAS","RAISE","SETJMP","MOD","LSH","RSH","ITER","DEL", \
"REGS","BITXOR", "IFN", "NOT", "BITNOT"]
dic = {}
for i in li:
dic[i] = li.index(i)
return dic
def prepare(x):
""" Prepares the line for processing by breaking it into tokens,
removing empty tokens and stripping whitespace """
try:
ind = x.index('"')
except:
ind = -1
if ind != -1:
d = x[ind:]
x = x[:ind]
x = x.split(' ')
tmp = []
final = []
for i in x:
if i:
if i[0] != ':':
tmp.append(i)
for i in tmp[:4]:
final.append(i)
if not d:
d = "".join(tmp[4:])
final.append(d.strip())
return final
def dequote(x):
""" Removes outermost quotes from a string, if they exist """
if x[0] == '"' and x[len(x)-1] == '"':
return x[1:len(x)-1]
return x
def assemble(asmc):
asmc = asmc.strip()
asmc = asmc.split('\n')
bc = []
ops = get_ops()
for line in asmc:
current = prepare(line)
i,a,b,c,d = current
a = int(a)
b = int(b)
c = int(c)
bc.append(chr(ops[i]))
bc.append(chr(a))
bc.append(chr(b))
bc.append(chr(c))
if i == "LINE":
n = a * 4
d = dequote(d)
text = d
text += chr(0) * (n - len(d))
bc.append(text)
if i == "STRING":
d = dequote(d)
text = d + "\0"*(4-len(d)%4)
bc.append(text)
elif i == "NUMBER":
d = int(d)
bc.append(fpack(d))
bc = "".join(bc)
return bc
if __name__ == '__main__':
asmc = load(ARGV[1])
bc = assemble(asmc)
save(ARGV[2], bc)