forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl2.py
30 lines (26 loc) · 868 Bytes
/
l2.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
import json
import sys
def add_prints():
"""
Adds an instruction that prints the number 42 before every jump instruction
If another instruction stores a different value in dest "v" before the print
then that value will be printed instead.
"""
prog = json.load(sys.stdin)
set_var = {"dest": "v",
"op": "const",
"type": "int",
"value": 42}
print_var = {"args": ["v"],
"op": "print"}
for func in prog['functions']:
instrs_modified = [set_var]
for instr in func['instrs']:
if 'op' in instr:
if instr['op'] == 'jmp':
instrs_modified .append(print_var)
instrs_modified.append(instr)
func['instrs'] = instrs_modified
print(json.dumps(prog))
if __name__ == '__main__':
add_prints()