-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.py
executable file
·43 lines (37 loc) · 1.17 KB
/
transform.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
#!/usr/bin/env python3
import re
def new_class(name, body):
new_name = name[0].upper() + name[1:]
new_body = body.replace("this.memory", "memory").replace("this.", "cpu.")
new_body = re.sub(r"memory\[([^\]]+)\]", r"memory.getByte(\1)", new_body)
new_body = new_body.replace("byte", "memory.getByte(this.address)")
new_body = re.sub(r"memory\.getByte\(([^\)]*)\) = (.*);", r"memory.setByte(\1, \2);", new_body)
print('''
export class {new_name} extends Instruction {{
size() {{
return 0; // TODO
}}
exec(cpu: CPU, memory: Memory) {{
{new_body}
}}
disassemble(memory: Memory) {{
return `TODO`;
}}
}}
'''.format(new_name=new_name, new_body=new_body))
with open('src/cpu.ts', 'r') as f:
fn_body = ''
fn_name = None
fn_regex = re.compile(r" (.*)\(")
for line in f.readlines():
if fn_name:
if line == ' }\n':
new_class(fn_name, fn_body)
fn_name = None
fn_body = ''
else:
fn_body += line
elif line.startswith(' op'):
in_fn = True
matches = fn_regex.match(line)
fn_name = matches.group(1)