forked from udem-dlteam/ribbit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsc
executable file
·212 lines (183 loc) · 5.95 KB
/
rsc
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
# This shell script is a convenient tool for executing the Ribbit
# AOT compiler with command line arguments to specify the target
# language, library, output file, etc.
#
# Here is a sample use:
#
# $ echo '(display "hello!\n")' > h.scm
# $ ./rsc -t py -l max h.scm
# $ python3 h.scm.py
# hello!
#
# The various command line options are explained in the README file.
#
# The script is particular useful for bootstrapping the compiler with
# itself. For example:
#
# $ ./rsc -t js -l max rsc.scm # compile rsc.scm to rsc.scm.js
# $ ./rsc -t py -l max -c "node rsc.scm.js" h.scm # use bootstrapped compiler
# $ python3 h.scm.py
# hello!
ROOTDIR="`dirname $0`"
TARGET="rvm"
INPUT=""
OUTPUT=""
LIBRARY=""
MINIFY=""
VERBOSITY=""
COMPILER=""
SOURCES=""
LASTSOURCE=""
usage()
{
echo "usage: $0 [-t target] [-c compiler] [-o output] [-l lib] [-m] [file.scm ...]"
echo "where"
echo " target defaults to rvm and is otherwise one of js, py, sh, c, ..."
echo " compiler defaults to \$RSC_COMPILER which defaults to"
echo " \"\$RSC_SCHEME_INTERPRETER $ROOTDIR/rsc.scm\" where"
echo " \$RSC_SCHEME_INTERPRETER defaults to \"gsi\" (the Gambit interpreter)"
echo " output defaults to last file.scm suffixed with .target"
echo " lib defaults to max-tc (library with type checking)"
echo " if there is no file.scm or it is -, the source code is read from stdin"
echo "examples:"
echo " $0 -t js prog.scm # generates prog.scm.js using Gambit interpreter"
echo " RSC_SCHEME_INTERPRETER=guile $0 -t js prog.scm # same using Guile"
echo " $0 -t js -l max rsc.scm # generates rsc.scm.js"
echo " $0 -t py -l max -c \"node rsc.scm.js\" rsc.scm # generates rsc.scm.py using nodejs running rsc.scm.js"
echo " RSC_COMPILER=\"node rsc.scm.js\" $0 -t py -l max rsc.scm # same with environment variable"
}
while [ "$#" != "0" ]; do
case "$1" in
-t|--target) # target
shift
TARGET="$1"
;;
-i|--input) # input
shift
INPUT="$1"
;;
-o|--output) # output
shift
OUTPUT="$1"
;;
-l|--library) # library
shift
LIBRARY="$1"
;;
-m|--minify) # minify
MINIFY="minify"
;;
-v|--v) # verbosity
VERBOSITY="v$VERBOSITY"
;;
-vv|--vv) # verbosity
VERBOSITY="vv$VERBOSITY"
;;
-vvv|--vvv) # verbosity
VERBOSITY="vvv$VERBOSITY"
;;
-c|--compiler) # compiler
shift
COMPILER="$1"
;;
-h|--help) # compiler
usage
exit 1
;;
-) # stdin
LASTSOURCE="-"
SOURCES="-"
;;
-*) # other options
echo "*** unknown option: $1"
exit 1
;;
*)
LASTSOURCE="$1"
SOURCES="$SOURCES $LASTSOURCE"
if [ ! -e "$LASTSOURCE" ]; then
echo "*** file does not exist: $LASTSOURCE"
exit 1
fi
;;
esac
shift
done
if [ "$LASTSOURCE" = "" ]; then
LASTSOURCE="-"
SOURCES="-"
fi
if [ "$LASTSOURCE" = "-" ]; then
OUTPUTBASE="./rsc-output"
else
OUTPUTBASE="`dirname $LASTSOURCE`/`basename $LASTSOURCE .scm`"
fi
RVM="$OUTPUTBASE.rvm"
if [ "$RSC_SCHEME_INTERPRETER" = "" ]; then
RSC_SCHEME_INTERPRETER="gsi"
fi
if [ "$COMPILER" = "" ]; then
if [ "$RSC_COMPILER" = "" ]; then
COMPILER="$RSC_SCHEME_INTERPRETER $ROOTDIR/rsc.scm"
else
COMPILER="$RSC_COMPILER"
fi
fi
if [ "$LIBRARY" = "" ]; then
LIBRARY="$ROOTDIR/lib/max-tc.scm"
else
LIBRARY="$ROOTDIR/lib/$LIBRARY.scm"
fi
if [ "$OUTPUT" = "" ]; then
if [ "$LASTSOURCE" = "-" ]; then
OUTPUTFILE="./rsc-output.$TARGET"
else
OUTPUTFILE="$LASTSOURCE.$TARGET"
fi
else
OUTPUTFILE="$OUTPUT"
fi
if cat "$LIBRARY" $SOURCES | sed -e 's/^#!/;;#!/g' | $COMPILER > "$RVM.temp" ; then
if [ "$INPUT" = "" ]; then
mv "$RVM.temp" "$RVM"
else
if [ ! -e "$INPUT" ]; then
echo "*** file does not exist: $INPUT"
exit 1
fi
cat "$INPUT" "$RVM.temp" > "$RVM"
rm "$RVM.temp"
fi
if [ "$TARGET" = "rvm" ]; then
if [ "$RVM" != "$OUTPUTFILE" ]; then
mv "$RVM" "$OUTPUTFILE"
fi
else
SAMPLE="41 59 39 117 63 62 118 68 63 62 118 82 68 63 62 118 82 65 63 62 118 82 65 63 62 118 82 58 63 62 118 82 61 33 40 58 108 107 109 33 39 58 108 107 118 54 121"
if grep "$SAMPLE" "$ROOTDIR/host/$TARGET/rvm.$TARGET" > "$OUTPUTFILE.temp" ; then
set 0 `od -v -A n -t u1 "$RVM"`
shift
sed -e "s/$SAMPLE/$*/g" "$ROOTDIR/host/$TARGET/rvm.$TARGET" > "$OUTPUTFILE.temp"
else
SAMPLE=");'u?>vD?>vRD?>vRA?>vRA?>vR:?>vR=!(:lkm!':lkv6y"
"$RSC_SCHEME_INTERPRETER" -e "(define B\"$SAMPLE\")(define D\"$ROOTDIR/host/$TARGET/rvm.$TARGET\")(define(A b c)(let((o(string-length b))(h(string-length c)))(let q((j 0)(d 0)(k(list)))(if(<(+ d h)o)(let x((i 0))(if(< i h)(if(char=?(string-ref b(+ d i))(string-ref c i))(x(+ i 1))(q j(+ d 1)k))(q(+ d h)(+ d h)(cons(substring b j d)k))))(reverse(cons(substring b j o)k))))))(define(z m c)(if(pair? m)(let((n(reverse m))(c(string->list c)))(let f((a(cdr n))(t(string->list(car n))))(if(pair? a)(f(cdr a)(append(string->list(car a))(append c t)))(list->string t))))(string)))(define C'((9 116)(10 110)(13 114)(34 34)(39 39)(92 92)))(define(v b)(let f((a(reverse(string->list b)))(e(list)))(if(pair? a)(f(cdr a)(let*((g(char->integer(car a)))(s(assoc g C)))(if s(cons 92(cons(cadr s)e))(cons g e))))(list->string(map integer->char e)))))(define(r u)(let f((e(list)))(let((g(read-char u)))(if(char? g)(f(cons g e))(list->string(reverse e))))))(define(w y)(call-with-input-file y r))(define p(r(current-input-port)))(define l(A(w D)B))(display(if(null?(cdr l))(string-append(car l)p)(z l(v p))))" < "$RVM" > "$OUTPUTFILE.temp"
fi
if [ "$MINIFY" = "" ]; then
mv "$OUTPUTFILE.temp" "$OUTPUTFILE"
else
cat "$OUTPUTFILE.temp" | "$ROOTDIR/host/$TARGET/minify" > "$OUTPUTFILE"
rm "$OUTPUTFILE.temp"
fi
rm "$RVM"
fi
if [ "$OUTPUT" = "" ]; then
cat "$OUTPUTFILE"
rm "$OUTPUTFILE"
fi
else
echo "*** compilation error:"
cat "$RVM.temp" # print errors
rm "$RVM.temp"
exit 1
fi