-
Notifications
You must be signed in to change notification settings - Fork 3
/
bher
executable file
·171 lines (149 loc) · 7.17 KB
/
bher
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/python
import os, sys
from subprocess import Popen
from external import optfunc
from os.path import abspath, dirname
from datetime import datetime
def vprint(s, verbose):
if verbose:
print
print s
def call(cmd, verbose=False, allow_fail=False):
if verbose:
print cmd
p = Popen(cmd, shell=True)
p.communicate()
status = p.returncode
if status != 0 and not allow_fail:
print "command failed:\n%s" % cmd
exit()
else:
return status
def parse_params(params):
if params == "":
return [(None, None)]
else:
key, vals = params.split(":")
return [(key, val) for val in vals.split(",")]
compilers = {
"vicare" : {
#"header" : "scheme-compilers/header-vicare.sc",
#"trailer" : "scheme-compilers/trailer-vicare.sc",
"template" : "scheme-compilers/vicare-template.sc",
"extension" : ".ss",
"runnable" : True },
"biwa" : {
#"header" : "scheme-compilers/header-biwa.sc",
#"trailer" : "scheme-compilers/trailer-biwa.sc",
"template" : "scheme-compilers/biwa-template.sc",
"extension" : ".html",
"runnable" : False },
"scheme2js" : {
#"header" : "scheme-compilers/header-scheme2js.sc",
#"trailer" : "scheme-compilers/trailer-scheme2js.sc",
"template" : "scheme-compilers/scheme2js-template.sc",
"extension" : ".sc",
"runnable" : False },
}
@optfunc.main
@optfunc.arghelp('verbose', 'display all executed commands')
@optfunc.arghelp('debug', 'run all ikarus commands in debug mode')
@optfunc.arghelp('pretty', 'keep & pretty-print compiled file')
@optfunc.arghelp('ext', 'definitions to be included in header of compiled file')
@optfunc.arghelp('time', 'record the time it takes for the compiled file to run')
@optfunc.arghelp('set', 'run church file with multiple parameter settings')
@optfunc.arghelp('keep', 'do not delete compiled file')
@optfunc.arghelp('compiler', 'compiler to use (available: vicare, biwa, scheme2js)')
@optfunc.arghelp('only-compile', "only compile, don't run")
def main(file, verbose=False, debug=False, keep=False, pretty=False, time=False, norun=False, ext="", set="", compiler="vicare", only=False):
"""Usage: %prog <file> [options]
Examples:
bher foo.church -dp
Compile & run foo.church in debug mode, pretty-print compiled file
to foo.church.ss.
bher foo.church -v -e defs.ss
Compile & run foo.church including definitions from defs.ss, print
all commands used.
bher foo.church -t -s samples:10,100,1000
Compile & run foo.church three times, replacing %(samples)s in
foo.church with 10, 100, and 1000 respectively. Print out runtime
for each setting.
bher foo.church -c biwa
Compile foo.church to html/js.
"""
if pretty: keep = True
params = parse_params(set)
bher_path = abspath(dirname(sys.argv[0]))
settings = {
"bher_path" : bher_path,
"in_path" : abspath(file),
##"header_path" : abspath(os.path.join(bher_path, compilers[compiler]["header"])),
##"trailer_path" : abspath(os.path.join(bher_path, compilers[compiler]["trailer"])),
"template_path" : abspath(os.path.join(bher_path, compilers[compiler]["template"])),
"compiler" : abspath(os.path.join(bher_path, "church/church-compiler.ss")),
"debug" : debug and "--debug" or "",
"pretty" : pretty and "T" or "F",
"ext" : ext and abspath(ext) or ""
}
for (i, (param_key, param_value)) in enumerate(params):
if param_key:
vprint("* %s: %s" % (param_key, param_value), True)
settings["out_path"] = abspath(file) + "." + str(i) + ".ss"
else:
settings["out_path"] = abspath(file) + compilers[compiler]["extension"]
vprint("removing old compilation files (if existent)", verbose)
call("rm -f '%(out_path)s'" % settings, verbose, allow_fail=True)
call("rm -f '%(out_path)s.tmp'" % settings, verbose, allow_fail=True)
call("rm -f '%(in_path)s.tmp'" % settings, verbose, allow_fail=True)
vprint("processing input file", verbose)
if param_key:
code = open(file).read() % { param_key : param_value }
f = open(file + ".tmp", "w")
f.write(code)
f.close()
else:
call("cp '%(in_path)s' '%(in_path)s.tmp'" % settings, verbose)
vprint("compiling church->scheme", verbose)
call(("cd '%(bher_path)s' && vicare %(debug)s --r6rs-script '%(compiler)s' " +
"'%(in_path)s.tmp' '%(out_path)s.tmp' '%(ext)s' %(pretty)s") % settings, verbose)
settings["churchprogram"] = open("%(out_path)s.tmp" % settings).read()
call("rm -f '%(in_path)s.tmp'" % settings, verbose, allow_fail=True)
call("rm -f '%(out_path)s.tmp'" % settings, verbose, allow_fail=True)
## vprint("adding compiler-specific header & footer", verbose)
## call(("cat '%(header_path)s' '%(out_path)s.tmp' '%(trailer_path)s' " +
## "> '%(out_path)s'") % settings, verbose)
## call("rm -f '%(out_path)s.tmp'" % settings, verbose, allow_fail=True)
vprint("wrapping with compiler-specific template", verbose)
template = open(settings["template_path"]).read()
open(settings["out_path"], "w").write(template % settings)
if only:
exit()
if compilers[compiler]["runnable"]:
vprint("running generated scheme ", verbose)
pre = datetime.now()
call("vicare %(debug)s --r6rs-script '%(out_path)s'" % settings, verbose)
post = datetime.now()
if time:
delta = post-pre
seconds = delta.seconds + delta.microseconds/1000000.0
print("runtime: %fs" % seconds)
if not keep:
vprint("removing compiled file", verbose)
call("rm -f '%(out_path)s'" % settings, verbose)
if compiler=="scheme2js":
vprint("compiling scheme to js via scheme2js ", verbose)
settings["js_out_path"] = settings["in_path"] + ".js"
settings["js_mash_path"] = abspath(os.path.join(settings["bher_path"], "scheme-compilers/javascript/random/Mash.js"))
settings["js_random_path"] = abspath(os.path.join(settings["bher_path"], "scheme-compilers/javascript/random/MRG32k3a.js"))
settings["js_math_path"] = abspath(os.path.join(settings["bher_path"], "scheme-compilers/javascript/math-functions.js"))
settings["scheme2js_path"] = abspath(os.path.join(settings["bher_path"], "external/scheme2js/scheme2js.jar"))
settings["scheme2js_runtime_path"] = abspath(os.path.join(settings["bher_path"], "external/scheme2js/runtime.js"))
call("java -jar '%(scheme2js_path)s' '%(out_path)s' -o '%(js_out_path)s' -O1" % settings, verbose)
if not keep:
call("rm -f '%(out_path)s'" % settings, verbose)
vprint("constructing html file ", verbose)
scheme2js_template = open("scheme-compilers/template-scheme2js.html").read()
scheme2js_html = scheme2js_template % settings
f = open(settings["in_path"] + ".html", "w")
f.write(scheme2js_html)
f.close()