-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathir.py
More file actions
66 lines (55 loc) · 1.55 KB
/
Copy pathir.py
File metadata and controls
66 lines (55 loc) · 1.55 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
'''
IR: Instruction Representation
'''
class Instr:
def __init__(self, raw):
self.raw = raw.strip()
self.label = None
self.op = None
self.arg1 = None
self.arg2 = None
self.result = None
self.parse()
def parse(self):
line = self.raw
if line.endswith(":"): #label
self.label = line[:-1]
return
# print statement
if line.startswith("print"):
self.op = "print"
self.arg1 = line.split()[1]
return
# if-goto
if line.startswith("if"):
# if a < b goto L3
parts = line.split()
self.op = "if"
self.arg1 = parts[1]
self.relop = parts[2]
self.arg2 = parts[3]
self.target = parts[5]
return
# goto
if line.startswith("goto"):
self.op = "goto"
self.target = line.split()[1]
return
# assignment
if "=" in line:
left, right = map(str.strip, line.split("="))
self.result = left
parts = right.split()
if len(parts) == 1:
self.op = "="
self.arg1 = parts[0]
else:
self.arg1 = parts[0]
self.op = parts[1]
self.arg2 = parts[2]
def is_label(self):
return self.label is not None
def is_jump(self):
return self.op in ["if", "goto"]
def is_conditional(self):
return self.op == "if"