-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtargetcode.py
More file actions
36 lines (36 loc) · 860 Bytes
/
Copy pathtargetcode.py
File metadata and controls
36 lines (36 loc) · 860 Bytes
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
import re
code=[]
while True:
line=input()
if not line:
break
code.append(line)
reg_map={}
r=1
out=[]
for line in code:
lhs,rhs=line.split("=",1)
lhs=lhs.strip()
rhs=rhs.strip()
parts=re.findall(r'\w+|[\+\-\*/]',rhs)
if len(parts)==1:
op1=parts[0]
src=reg_map.get(op1,op1)
if lhs.startswith("t"):
reg=f"R{r}"
reg_map[lhs]=reg
out.append(f"MOV {reg},{src}")
r+=1
else:
out.append(f"MOV {lhs},{src}")
else:
x,op,y=parts
reg=f"R{r}"
r+=1
reg_map[lhs]=reg
src1=reg_map.get(x,x)
src2=reg_map.get(y,y)
ops = {'+':'ADD','-':'SUB','*':'MUL','/':'DIV'}
out.append(f"{ops[op]} {reg},{src1},{src2}")
r+=1
print("\n".join(out))