summaryrefslogtreecommitdiff
path: root/tools/gencode.py
blob: b8309885008d9b9c2613b871faa61676503f8aac (plain)
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
#!/bin/env python3

code = {}
buf = []
event = 0

def flush_code():
	global buf
	if event > 0:
		code[event] = buf
		buf = []

event = 0
for line in open("events.txt").readlines():
	line = line.strip()
	if line == "EOF":
		break
	elif line == "":
		continue
	elif line.startswith("#"):
		continue
	elif line.startswith("EVENT"):
		flush_code()
		event = (int(line.split()[1]) << 1)
	elif line.startswith("SHADED"):
		flush_code()
		event = (int(line.split()[1]) << 1) + 1
	else:
		buf.append(line)

flush_code()

code_index = ["-1"] * (72 * 2)

pc = 0
print("const CODE = [")
for event in range(2,146):
	if event & 1 == 1:
		print("\t// SHADED", event >> 1)
	else:
		print("\t// EVENT", event >> 1)
	if not event in code:
		print("\t// TODO")
		continue
	code_index[event-2] = str(pc)
	for line in code[event]:
		if line.startswith('space'):
			line = line.split(' ', 2)
			print('\t[ vm_space, ' + line[1] + ', (s)=>' + line[2] + ' ],')
		elif line.startswith('each space'):
			line = line.split(' ', 2)
			print('\t[ vm_space, 0, (s)=>' + line[2] + ' ],')
		elif line.startswith('piece'):
			line = line.split(' ', 2)
			print('\t[ vm_piece, ' + line[1] + ', (p,s)=>' + line[2] + ' ],')
		elif line.startswith('each piece'):
			line = line.split(' ', 2)
			print('\t[ vm_piece, 0, (p,s)=>' + line[2] + ' ],')
		elif line.startswith('if'):
			line = line.split(' ', 1)
			print('\t[ vm_if, ()=>' + line[1] + ' ],')
		elif line.startswith('prompt'):
			line = line.split(' ', 1)
			print('\t[ vm_prompt, "' + line[1] + '" ],')
		elif line.startswith('log'):
			line = line.split(' ', 1)
			print('\t[ vm_log, "' + line[1] + '" ],')
		else:
			line = line.split(' ')
			cmd = line[0]
			args = [ ("()=>" + x if x[0] == '(' else x) for x in line[1:] ]
			if len(args) > 0:
				print('\t[ vm_' + cmd + ', ' + ', '.join(args) + ' ],')
			else:
				print('\t[ vm_' + cmd + ' ],')
		pc = pc + 1
	print('\t[ vm_return ],')
	pc = pc + 1
print("]")
print("const CODE_INDEX = [ " + ", ".join(code_index) + " ]")