summaryrefslogtreecommitdiff
path: root/tools/gencode.py
blob: 2d8af675f9e2ea386d8e41be740a737aa5a110dd (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
#!/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("tools/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])
	elif line.startswith("SHADED"):
		flush_code()
		event = int(line.split()[1]) + 100
	else:
		buf.append(line)

flush_code()

shaded = ["-1"] * 72
unshaded = ["-1"] * 72

pc = 0
print("const CODE = [")
for event in sorted(code.keys()):
	buf = code[event]
	if event > 100:
		print("\t// SHADED", event - 100)
		shaded[event  - 100] = str(pc)
	else:
		print("\t// EVENT", event)
		unshaded[event] = 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] + ' ],')
		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_endevent ],')
	pc = pc + 1
print("]")
print("const SHADED_START = [" + ",".join(shaded) + "]")
print("const UNSHADED_START = [" + ",".join(unshaded) + "]")