-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
80 lines (66 loc) · 2.85 KB
/
Copy pathbenchmark.py
File metadata and controls
80 lines (66 loc) · 2.85 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class TACSimulator:
def __init__(self, debug=False):
self.debug = debug
self.env = {}
def simulate(self, tac_instructions):
"""Simulate TAC instructions. Returns number of instructions executed."""
ip = 0
instructions_executed = 0
# Build label map
labels = {}
for i, instr in enumerate(tac_instructions):
if instr.endswith(':'):
labels[instr[:-1]] = i
while ip < len(tac_instructions):
instr = tac_instructions[ip]
instructions_executed += 1
ip += 1
if instr.endswith(':') or instr.startswith('func ') or instr == 'endfunc':
continue # Labels and function markers are no-ops
if self.debug:
print(f"[TAC] {instr}")
parts = instr.split()
if len(parts) == 0:
continue
if parts[0] == 'goto':
ip = labels[parts[1]]
continue
if parts[0] == 'ifFalse':
cond_var = parts[1]
target = parts[3]
val = self.env.get(cond_var, False)
if not val:
ip = labels[target]
continue
if parts[0] == 'return':
break # Halt simulation on return
# Assignment: x = y op z OR x = y
if len(parts) >= 3 and parts[1] == '=':
dest = parts[0]
left = self._eval_val(parts[2])
if len(parts) == 3:
self.env[dest] = left
elif len(parts) == 5:
op = parts[3]
right = self._eval_val(parts[4])
if op == '+': self.env[dest] = left + right
elif op == '-': self.env[dest] = left - right
elif op == '*': self.env[dest] = left * right
elif op == '/': self.env[dest] = left / right
elif op == '<': self.env[dest] = left < right
elif op == '>': self.env[dest] = left > right
elif op == '<=': self.env[dest] = left <= right
elif op == '>=': self.env[dest] = left >= right
elif op == '==': self.env[dest] = left == right
elif op == '!=': self.env[dest] = left != right
return instructions_executed
def _eval_val(self, val):
if val == 'True': return True
if val == 'False': return False
try:
return int(val)
except ValueError:
try:
return float(val)
except ValueError:
return self.env.get(val, 0) # variable lookup