-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
executable file
·135 lines (112 loc) · 3.53 KB
/
preprocess.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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python
from api import *
TEMPLATE_DIR = "templates"
INO_DIR = "rpnled"
WEB_DIR = "web"
CONTROL_DIR = "control"
cpplines = (l for l in file(TEMPLATE_DIR + "/commands.cpp.templ").readlines())
hpplines = (l for l in file(TEMPLATE_DIR + "/commands.h.templ").readlines())
htmllines = (l for l in file(TEMPLATE_DIR + "/index.html.templ").readlines())
jslines = (l for l in file(TEMPLATE_DIR + "/runcmd.js.templ").readlines())
with open(INO_DIR + "/commands.h", "w") as hpp, \
open(INO_DIR + "/commands.cpp", "w") as cpp, \
open(CONTROL_DIR + "/commands.py", "w") as py, \
open(WEB_DIR + "/const.js", "w") as js, \
open(WEB_DIR + "/runcmd.js", "w") as jsrun, \
open(WEB_DIR + "/index.html", "w") as html:
for l in hpplines:
if "CONST" in l:
break
hpp.write(l)
for l in cpplines:
if "CONST" in l:
break
cpp.write(l)
for l in htmllines:
if "CONST" in l:
break
html.write(l)
for l in jslines:
if "CONST" in l:
break
jsrun.write(l)
val = 0
for op in operators:
val -= 1
if op is not None:
c = "C_" + op[0]
pop, push = op[2]
cmd = op[3]
cmd = cmd.replace("#3", "stack[stack_ptr-3]")
cmd = cmd.replace("#2", "stack[stack_ptr-2]")
cmd = cmd.replace("#1", "stack[stack_ptr-1]")
cmd = cmd.replace("#0", "stack[stack_ptr]")
case = " case " + c + ": "
case += push > pop and "PUSH(" or "POP("
case += repr(max(push, pop)) + "); "
case += cmd
if push != pop:
case += " stack_ptr -= %d;" % (pop - push)
case += " break;\n"
cpp.write( case )
jsrun.write( case )
hpp.write( "#define " + c + " " + repr(val) + "\n")
py.write( c + " = " + repr(val) + "\n")
js.write( "var " + c + " = " + repr(val) + ";\n")
#html.write( "<button id=go onclick=\"addcmd("+c+")\">" + c + "</button><br>\n")
html.write( c + "<br>\n")
else:
html.write( "<br>\n")
cpp.write("\n")
hpp.write("\n")
py.write("\n")
js.write("\n")
if not (val%29):
html.write("</td><td>")
'''
else:
while val % 10:
val -= 1
'''
for i, (c, s) in enumerate(other):
hpp.write( "#define " + c + " " + s + "\n")
js.write( "var " + c + " = " + s + ";\n")
for l in hpplines:
hpp.write(l)
for l in cpplines:
cpp.write(l)
for l in htmllines:
html.write(l)
for l in jslines:
jsrun.write(l)
import re
plines = file(CONTROL_DIR + "/patterns.py").readlines()
start = re.compile("^([a-zA-Z0-9_]+) = \[\s*$");
end = re.compile("^]")
name = None
patt = ""
allnames = []
with open(WEB_DIR + "/patterns.js", "w") as js:
for l in plines:
if name is not None:
result = end.match(l)
if result:
#print name + " : " + patt
js.write("var code_" + name + " = \"" + patt + "\";\n")
allnames.append(name)
name = None
patt = ""
else:
patt += l.rstrip() + "\\n"
else:
result = start.match(l)
if result:
name = result.group(1)
js.write("var code_patterns = {\n")
for name in allnames:
js.write(name + " : code_" + name + ",\n")
js.write("}")
import os
os.chdir(WEB_DIR)
os.system("uglifyjs const.js patterns.js runcmd.js lights.js -c -m > led.js")
os.system("gzip < led.js > led.js.gz")