adamc@764
|
1 (* Copyright (c) 2008-2009, Adam Chlipala
|
adamc@1
|
2 * All rights reserved.
|
adamc@1
|
3 *
|
adamc@1
|
4 * Redistribution and use in source and binary forms, with or without
|
adamc@1
|
5 * modification, are permitted provided that the following conditions are met:
|
adamc@1
|
6 *
|
adamc@1
|
7 * - Redistributions of source code must retain the above copyright notice,
|
adamc@1
|
8 * this list of conditions and the following disclaimer.
|
adamc@1
|
9 * - Redistributions in binary form must reproduce the above copyright notice,
|
adamc@1
|
10 * this list of conditions and the following disclaimer in the documentation
|
adamc@1
|
11 * and/or other materials provided with the distribution.
|
adamc@1
|
12 * - The names of contributors may not be used to endorse or promote products
|
adamc@1
|
13 * derived from this software without specific prior written permission.
|
adamc@1
|
14 *
|
adamc@1
|
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
adamc@1
|
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
adamc@1
|
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
adamc@1
|
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
adamc@1
|
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
adamc@1
|
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
adamc@1
|
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
adamc@1
|
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
adamc@1
|
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
adamc@1
|
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
adamc@1
|
25 * POSSIBILITY OF SUCH DAMAGE.
|
adamc@1
|
26 *)
|
adamc@1
|
27
|
adamc@1
|
28 structure Compiler :> COMPILER = struct
|
adamc@1
|
29
|
adamc@244
|
30 structure UrwebLrVals = UrwebLrValsFn(structure Token = LrParser.Token)
|
adamc@244
|
31 structure Lex = UrwebLexFn(structure Tokens = UrwebLrVals.Tokens)
|
adamc@244
|
32 structure UrwebP = Join(structure ParserData = UrwebLrVals.ParserData
|
adamc@1
|
33 structure Lex = Lex
|
adamc@1
|
34 structure LrParser = LrParser)
|
adamc@1
|
35
|
adamc@270
|
36 type job = {
|
adamc@385
|
37 prefix : string,
|
adamc@270
|
38 database : string option,
|
adamc@274
|
39 sources : string list,
|
adamc@274
|
40 exe : string,
|
adamc@274
|
41 sql : string option,
|
adamc@502
|
42 debug : bool,
|
adamc@673
|
43 profile : bool,
|
adamc@764
|
44 timeout : int,
|
adamc@764
|
45 ffi : string list,
|
adamc@764
|
46 link : string list,
|
adamc@765
|
47 headers : string list,
|
adamc@766
|
48 scripts : string list,
|
adamc@765
|
49 clientToServer : Settings.ffi list,
|
adamc@765
|
50 effectful : Settings.ffi list,
|
adamc@765
|
51 clientOnly : Settings.ffi list,
|
adamc@765
|
52 serverOnly : Settings.ffi list,
|
adamc@765
|
53 jsFuncs : (Settings.ffi * string) list
|
adamc@270
|
54 }
|
adamc@201
|
55
|
adamc@201
|
56 type ('src, 'dst) phase = {
|
adamc@201
|
57 func : 'src -> 'dst,
|
adamc@201
|
58 print : 'dst -> Print.PD.pp_desc
|
adamc@201
|
59 }
|
adamc@201
|
60
|
adamc@201
|
61 type pmap = (string * Time.time) list
|
adamc@201
|
62
|
adamc@201
|
63 type ('src, 'dst) transform = {
|
adamc@201
|
64 func : 'src -> 'dst option,
|
adamc@201
|
65 print : 'dst -> Print.PD.pp_desc,
|
adamc@201
|
66 time : 'src * pmap -> 'dst option * pmap
|
adamc@201
|
67 }
|
adamc@201
|
68
|
adamc@201
|
69 fun transform (ph : ('src, 'dst) phase) name = {
|
adamc@201
|
70 func = fn input => let
|
adamc@201
|
71 val v = #func ph input
|
adamc@201
|
72 in
|
adamc@201
|
73 if ErrorMsg.anyErrors () then
|
adamc@201
|
74 NONE
|
adamc@201
|
75 else
|
adamc@201
|
76 SOME v
|
adamc@201
|
77 end,
|
adamc@201
|
78 print = #print ph,
|
adamc@201
|
79 time = fn (input, pmap) => let
|
adamc@201
|
80 val befor = Time.now ()
|
adamc@201
|
81 val v = #func ph input
|
adamc@201
|
82 val elapsed = Time.- (Time.now (), befor)
|
adamc@201
|
83 in
|
adamc@201
|
84 (if ErrorMsg.anyErrors () then
|
adamc@201
|
85 NONE
|
adamc@201
|
86 else
|
adamc@201
|
87 SOME v,
|
adamc@201
|
88 (name, elapsed) :: pmap)
|
adamc@201
|
89 end
|
adamc@201
|
90 }
|
adamc@201
|
91
|
adamc@270
|
92 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
|
adamc@201
|
93 func = fn input => case #func tr1 input of
|
adamc@201
|
94 NONE => NONE
|
adamc@201
|
95 | SOME v => #func tr2 v,
|
adamc@201
|
96 print = #print tr2,
|
adamc@201
|
97 time = fn (input, pmap) => let
|
adamc@201
|
98 val (ro, pmap) = #time tr1 (input, pmap)
|
adamc@201
|
99 in
|
adamc@201
|
100 case ro of
|
adamc@201
|
101 NONE => (NONE, pmap)
|
adamc@201
|
102 | SOME v => #time tr2 (v, pmap)
|
adamc@201
|
103 end
|
adamc@201
|
104 }
|
adamc@201
|
105
|
adamc@346
|
106 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
|
adamc@346
|
107 ignore (#func tr x))
|
adamc@346
|
108
|
adamc@280
|
109 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
|
adamc@280
|
110 #func tr x)
|
adamc@201
|
111
|
adamc@201
|
112 fun runPrint (tr : ('src, 'dst) transform) input =
|
adamc@280
|
113 (ErrorMsg.resetErrors ();
|
adamc@280
|
114 case #func tr input of
|
adamc@280
|
115 NONE => print "Failure\n"
|
adamc@280
|
116 | SOME v =>
|
adamc@280
|
117 (print "Success\n";
|
adamc@280
|
118 Print.print (#print tr v);
|
adamc@280
|
119 print "\n"))
|
adamc@201
|
120
|
adamc@201
|
121 fun time (tr : ('src, 'dst) transform) input =
|
adamc@55
|
122 let
|
adamc@201
|
123 val (_, pmap) = #time tr (input, [])
|
adamc@201
|
124 in
|
adamc@201
|
125 app (fn (name, time) =>
|
adamc@201
|
126 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
|
adamc@201
|
127 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
|
adamc@201
|
128 print "\n"
|
adamc@201
|
129 end
|
adamc@55
|
130
|
adamc@201
|
131 fun timePrint (tr : ('src, 'dst) transform) input =
|
adamc@201
|
132 let
|
adamc@201
|
133 val (ro, pmap) = #time tr (input, [])
|
adamc@55
|
134 in
|
adamc@201
|
135 app (fn (name, time) =>
|
adamc@201
|
136 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
|
adamc@201
|
137 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
|
adamc@201
|
138 print "\n";
|
adamc@201
|
139 case ro of
|
adamc@201
|
140 NONE => print "Failure\n"
|
adamc@201
|
141 | SOME v =>
|
adamc@201
|
142 (print "Success\n";
|
adamc@201
|
143 Print.print (#print tr v);
|
adamc@201
|
144 print "\n")
|
adamc@55
|
145 end
|
adamc@55
|
146
|
adamc@244
|
147 val parseUrs =
|
adamc@201
|
148 {func = fn filename => let
|
adamc@201
|
149 val fname = OS.FileSys.tmpName ()
|
adamc@201
|
150 val outf = TextIO.openOut fname
|
adamc@201
|
151 val () = TextIO.output (outf, "sig\n")
|
adamc@201
|
152 val inf = TextIO.openIn filename
|
adamc@201
|
153 fun loop () =
|
adamc@201
|
154 case TextIO.inputLine inf of
|
adamc@201
|
155 NONE => ()
|
adamc@201
|
156 | SOME line => (TextIO.output (outf, line);
|
adamc@201
|
157 loop ())
|
adamc@201
|
158 val () = loop ()
|
adamc@201
|
159 val () = TextIO.closeIn inf
|
adamc@201
|
160 val () = TextIO.closeOut outf
|
adamc@201
|
161
|
adamc@201
|
162 val () = (ErrorMsg.resetErrors ();
|
adamc@201
|
163 ErrorMsg.resetPositioning filename;
|
adamc@201
|
164 Lex.UserDeclarations.initialize ())
|
adamc@201
|
165 val file = TextIO.openIn fname
|
adamc@201
|
166 fun get _ = TextIO.input file
|
adamc@201
|
167 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
|
adamc@201
|
168 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
|
adamc@244
|
169 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
|
adamc@201
|
170 in
|
adamc@201
|
171 TextIO.closeIn file;
|
adamc@201
|
172 case absyn of
|
adamc@201
|
173 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
|
adamc@201
|
174 | _ => (ErrorMsg.errorAt {file = filename,
|
adamc@201
|
175 first = {line = 0,
|
adamc@201
|
176 char = 0},
|
adamc@201
|
177 last = {line = 0,
|
adamc@201
|
178 char = 0}} "Not a signature";
|
adamc@201
|
179 [])
|
adamc@201
|
180 end
|
adamc@201
|
181 handle LrParser.ParseError => [],
|
adamc@201
|
182 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
|
adamc@55
|
183
|
adamc@1
|
184 (* The main parsing routine *)
|
adamc@244
|
185 val parseUr = {
|
adamc@201
|
186 func = fn filename =>
|
adamc@201
|
187 let
|
adamc@201
|
188 val () = (ErrorMsg.resetErrors ();
|
adamc@201
|
189 ErrorMsg.resetPositioning filename;
|
adamc@201
|
190 Lex.UserDeclarations.initialize ())
|
adamc@201
|
191 val file = TextIO.openIn filename
|
adamc@201
|
192 fun get _ = TextIO.input file
|
adamc@201
|
193 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
|
adamc@201
|
194 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
|
adamc@244
|
195 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
|
adamc@201
|
196 in
|
adamc@201
|
197 TextIO.closeIn file;
|
adamc@201
|
198 case absyn of
|
adamc@201
|
199 [(Source.DSgn ("?", _), _)] =>
|
adamc@201
|
200 (ErrorMsg.errorAt {file = filename,
|
adamc@201
|
201 first = {line = 0,
|
adamc@201
|
202 char = 0},
|
adamc@201
|
203 last = {line = 0,
|
adamc@201
|
204 char = 0}} "File starts with 'sig'";
|
adamc@201
|
205 [])
|
adamc@201
|
206 | _ => absyn
|
adamc@201
|
207 end
|
adamc@201
|
208 handle LrParser.ParseError => [],
|
adamc@201
|
209 print = SourcePrint.p_file}
|
adamc@56
|
210
|
adamc@765
|
211 fun p_job {prefix, database, exe, sql, sources, debug, profile,
|
adamc@766
|
212 timeout, ffi, link, headers, scripts,
|
adamc@765
|
213 clientToServer, effectful, clientOnly, serverOnly, jsFuncs} =
|
adamc@270
|
214 let
|
adamc@270
|
215 open Print.PD
|
adamc@270
|
216 open Print
|
adamc@765
|
217
|
adamc@765
|
218 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
|
adamc@765
|
219 box [string name, space, string m, string ".", string s, newline])
|
adamc@270
|
220 in
|
adamc@274
|
221 box [if debug then
|
adamc@274
|
222 box [string "DEBUG", newline]
|
adamc@274
|
223 else
|
adamc@274
|
224 box [],
|
adamc@502
|
225 if profile then
|
adamc@502
|
226 box [string "PROFILE", newline]
|
adamc@502
|
227 else
|
adamc@502
|
228 box [],
|
adamc@274
|
229 case database of
|
adamc@270
|
230 NONE => string "No database."
|
adamc@270
|
231 | SOME db => string ("Database: " ^ db),
|
adamc@270
|
232 newline,
|
adamc@274
|
233 string "Exe: ",
|
adamc@274
|
234 string exe,
|
adamc@274
|
235 newline,
|
adamc@274
|
236 case sql of
|
adamc@274
|
237 NONE => string "No SQL file."
|
adamc@274
|
238 | SOME sql => string ("SQL fle: " ^ sql),
|
adamc@673
|
239 newline,
|
adamc@673
|
240 string "Timeout: ",
|
adamc@673
|
241 string (Int.toString timeout),
|
adamc@673
|
242 newline,
|
adamc@764
|
243 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
|
adamc@764
|
244 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
|
adamc@766
|
245 p_list_sep (box []) (fn s => box [string "Script", space, string s, newline]) scripts,
|
adamc@764
|
246 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
|
adamc@765
|
247 p_ffi "ClientToServer" clientToServer,
|
adamc@765
|
248 p_ffi "Effectful" effectful,
|
adamc@765
|
249 p_ffi "ClientOnly" clientOnly,
|
adamc@765
|
250 p_ffi "ServerOnly" serverOnly,
|
adamc@765
|
251 p_list_sep (box []) (fn ((m, s), s') =>
|
adamc@765
|
252 box [string "JsFunc", space, string m, string ".", string s,
|
adamc@765
|
253 space, string "=", space, string s', newline]) jsFuncs,
|
adamc@270
|
254 string "Sources:",
|
adamc@270
|
255 p_list string sources,
|
adamc@270
|
256 newline]
|
adamc@270
|
257 end
|
adamc@270
|
258
|
adamc@270
|
259 fun trim s =
|
adamc@270
|
260 let
|
adamc@270
|
261 val (_, s) = Substring.splitl Char.isSpace s
|
adamc@270
|
262 val (s, _) = Substring.splitr Char.isSpace s
|
adamc@270
|
263 in
|
adamc@270
|
264 s
|
adamc@270
|
265 end
|
adamc@270
|
266
|
adamc@270
|
267 val parseUrp = {
|
adamc@270
|
268 func = fn filename =>
|
adamc@270
|
269 let
|
adamc@270
|
270 val dir = OS.Path.dir filename
|
adamc@270
|
271 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
|
adamc@270
|
272
|
adamc@274
|
273 fun relify fname =
|
adamc@274
|
274 OS.Path.concat (dir, fname)
|
adamc@274
|
275 handle OS.Path.Path => fname
|
adamc@274
|
276
|
adamc@764
|
277 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
|
adamc@764
|
278
|
adamc@764
|
279 fun relifyA fname = OS.Path.mkAbsolute {path = fname, relativeTo = absDir}
|
adamc@764
|
280
|
adamc@270
|
281 fun readSources acc =
|
adamc@270
|
282 case TextIO.inputLine inf of
|
adamc@270
|
283 NONE => rev acc
|
adamc@270
|
284 | SOME line =>
|
adamc@270
|
285 let
|
adamc@270
|
286 val acc = if CharVector.all Char.isSpace line then
|
adamc@270
|
287 acc
|
adamc@270
|
288 else
|
adamc@270
|
289 let
|
adamc@270
|
290 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
|
adamc@270
|
291 (String.explode line))
|
adamc@274
|
292 val fname = relify fname
|
adamc@270
|
293 in
|
adamc@270
|
294 fname :: acc
|
adamc@270
|
295 end
|
adamc@270
|
296 in
|
adamc@270
|
297 readSources acc
|
adamc@270
|
298 end
|
adamc@270
|
299
|
adamc@764
|
300 val prefix = ref NONE
|
adamc@764
|
301 val database = ref NONE
|
adamc@764
|
302 val exe = ref NONE
|
adamc@764
|
303 val sql = ref NONE
|
adamc@764
|
304 val debug = ref false
|
adamc@764
|
305 val profile = ref false
|
adamc@764
|
306 val timeout = ref NONE
|
adamc@764
|
307 val ffi = ref []
|
adamc@764
|
308 val link = ref []
|
adamc@764
|
309 val headers = ref []
|
adamc@766
|
310 val scripts = ref []
|
adamc@765
|
311 val clientToServer = ref []
|
adamc@765
|
312 val effectful = ref []
|
adamc@765
|
313 val clientOnly = ref []
|
adamc@765
|
314 val serverOnly = ref []
|
adamc@765
|
315 val jsFuncs = ref []
|
adamc@764
|
316
|
adamc@764
|
317 fun finish sources =
|
adamc@764
|
318 {prefix = Option.getOpt (!prefix, "/"),
|
adamc@764
|
319 database = !database,
|
adamc@764
|
320 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
|
adamc@764
|
321 ext = SOME "exe"}),
|
adamc@764
|
322 sql = !sql,
|
adamc@764
|
323 debug = !debug,
|
adamc@764
|
324 profile = !profile,
|
adamc@764
|
325 timeout = Option.getOpt (!timeout, 60),
|
adamc@765
|
326 ffi = rev (!ffi),
|
adamc@765
|
327 link = rev (!link),
|
adamc@765
|
328 headers = rev (!headers),
|
adamc@766
|
329 scripts = rev (!scripts),
|
adamc@765
|
330 clientToServer = rev (!clientToServer),
|
adamc@765
|
331 effectful = rev (!effectful),
|
adamc@765
|
332 clientOnly = rev (!clientOnly),
|
adamc@765
|
333 serverOnly = rev (!serverOnly),
|
adamc@765
|
334 jsFuncs = rev (!jsFuncs),
|
adamc@274
|
335 sources = sources}
|
adamc@274
|
336
|
adamc@764
|
337 fun read () =
|
adamc@270
|
338 case TextIO.inputLine inf of
|
adamc@764
|
339 NONE => finish []
|
adamc@764
|
340 | SOME "\n" => finish (readSources [])
|
adamc@270
|
341 | SOME line =>
|
adamc@270
|
342 let
|
adamc@270
|
343 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
|
adamc@270
|
344 val cmd = Substring.string (trim cmd)
|
adamc@270
|
345 val arg = Substring.string (trim arg)
|
adamc@765
|
346
|
adamc@765
|
347 fun ffiS () =
|
adamc@765
|
348 case String.fields (fn ch => ch = #".") arg of
|
adamc@765
|
349 [m, x] => (m, x)
|
adamc@765
|
350 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
|
adamc@765
|
351 ("", ""))
|
adamc@765
|
352
|
adamc@765
|
353 fun ffiM () =
|
adamc@765
|
354 case String.fields (fn ch => ch = #"=") arg of
|
adamc@765
|
355 [f, s] =>
|
adamc@765
|
356 (case String.fields (fn ch => ch = #".") f of
|
adamc@765
|
357 [m, x] => ((m, x), s)
|
adamc@765
|
358 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
|
adamc@765
|
359 (("", ""), "")))
|
adamc@765
|
360 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
|
adamc@765
|
361 (("", ""), ""))
|
adamc@270
|
362 in
|
adamc@270
|
363 case cmd of
|
adamc@385
|
364 "prefix" =>
|
adamc@764
|
365 (case !prefix of
|
adamc@385
|
366 NONE => ()
|
adamc@385
|
367 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
|
adamc@764
|
368 prefix := SOME arg)
|
adamc@385
|
369 | "database" =>
|
adamc@764
|
370 (case !database of
|
adamc@270
|
371 NONE => ()
|
adamc@270
|
372 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
|
adamc@764
|
373 database := SOME arg)
|
adamc@274
|
374 | "exe" =>
|
adamc@764
|
375 (case !exe of
|
adamc@274
|
376 NONE => ()
|
adamc@274
|
377 | SOME _ => ErrorMsg.error "Duplicate 'exe' directive";
|
adamc@764
|
378 exe := SOME (relify arg))
|
adamc@274
|
379 | "sql" =>
|
adamc@764
|
380 (case !sql of
|
adamc@274
|
381 NONE => ()
|
adamc@274
|
382 | SOME _ => ErrorMsg.error "Duplicate 'sql' directive";
|
adamc@764
|
383 sql := SOME (relify arg))
|
adamc@764
|
384 | "debug" => debug := true
|
adamc@764
|
385 | "profile" => profile := true
|
adamc@673
|
386 | "timeout" =>
|
adamc@764
|
387 (case !timeout of
|
adamc@673
|
388 NONE => ()
|
adamc@673
|
389 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
|
adamc@764
|
390 timeout := SOME (valOf (Int.fromString arg)))
|
adamc@764
|
391 | "ffi" => ffi := relify arg :: !ffi
|
adamc@764
|
392 | "link" => link := relifyA arg :: !link
|
adamc@764
|
393 | "include" => headers := relifyA arg :: !headers
|
adamc@766
|
394 | "script" => scripts := arg :: !scripts
|
adamc@765
|
395 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
|
adamc@765
|
396 | "effectful" => effectful := ffiS () :: !effectful
|
adamc@765
|
397 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
|
adamc@765
|
398 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
|
adamc@765
|
399 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
|
adamc@764
|
400 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
|
adamc@764
|
401 read ()
|
adamc@270
|
402 end
|
adamc@385
|
403
|
adamc@764
|
404 val job = read ()
|
adamc@270
|
405 in
|
adamc@385
|
406 TextIO.closeIn inf;
|
adamc@764
|
407 Settings.setUrlPrefix (#prefix job);
|
adamc@764
|
408 Settings.setTimeout (#timeout job);
|
adamc@764
|
409 Settings.setHeaders (#headers job);
|
adamc@766
|
410 Settings.setScripts (#scripts job);
|
adamc@765
|
411 Settings.setClientToServer (#clientToServer job);
|
adamc@765
|
412 Settings.setEffectful (#effectful job);
|
adamc@765
|
413 Settings.setClientOnly (#clientOnly job);
|
adamc@765
|
414 Settings.setServerOnly (#serverOnly job);
|
adamc@765
|
415 Settings.setJsFuncs (#jsFuncs job);
|
adamc@385
|
416 job
|
adamc@270
|
417 end,
|
adamc@270
|
418 print = p_job
|
adamc@270
|
419 }
|
adamc@270
|
420
|
adamc@270
|
421 val toParseJob = transform parseUrp "parseJob"
|
adamc@270
|
422
|
adamc@56
|
423 fun capitalize "" = ""
|
adamc@56
|
424 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
|
adamc@56
|
425
|
adamc@201
|
426 val parse = {
|
adamc@764
|
427 func = fn {database, sources = fnames, ffi, ...} : job =>
|
adamc@201
|
428 let
|
adamc@201
|
429 fun nameOf fname = capitalize (OS.Path.file fname)
|
adamc@109
|
430
|
adamc@764
|
431 fun parseFfi fname =
|
adamc@764
|
432 let
|
adamc@764
|
433 val mname = nameOf fname
|
adamc@764
|
434 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
|
adamc@764
|
435
|
adamc@764
|
436 val loc = {file = urs,
|
adamc@764
|
437 first = ErrorMsg.dummyPos,
|
adamc@764
|
438 last = ErrorMsg.dummyPos}
|
adamc@764
|
439
|
adamc@764
|
440 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
|
adamc@764
|
441 in
|
adamc@764
|
442 (Source.DFfiStr (mname, sgn), loc)
|
adamc@764
|
443 end
|
adamc@764
|
444
|
adamc@201
|
445 fun parseOne fname =
|
adamc@201
|
446 let
|
adamc@201
|
447 val mname = nameOf fname
|
adamc@244
|
448 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
|
adamc@244
|
449 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
|
adamc@56
|
450
|
adamc@201
|
451 val sgnO =
|
adamc@244
|
452 if Posix.FileSys.access (urs, []) then
|
adamc@244
|
453 SOME (Source.SgnConst (#func parseUrs urs),
|
adamc@244
|
454 {file = urs,
|
adamc@201
|
455 first = ErrorMsg.dummyPos,
|
adamc@201
|
456 last = ErrorMsg.dummyPos})
|
adamc@201
|
457 else
|
adamc@201
|
458 NONE
|
adamc@56
|
459
|
adamc@244
|
460 val loc = {file = ur,
|
adamc@201
|
461 first = ErrorMsg.dummyPos,
|
adamc@201
|
462 last = ErrorMsg.dummyPos}
|
adamc@56
|
463
|
adamc@244
|
464 val ds = #func parseUr ur
|
adamc@201
|
465 in
|
adamc@201
|
466 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
|
adamc@201
|
467 end
|
adamc@56
|
468
|
adamc@764
|
469 val dsFfi = map parseFfi ffi
|
adamc@201
|
470 val ds = map parseOne fnames
|
adamc@201
|
471 in
|
adamc@201
|
472 let
|
adamc@201
|
473 val final = nameOf (List.last fnames)
|
adamc@271
|
474
|
adamc@764
|
475 val ds = dsFfi @ ds
|
adamc@764
|
476 @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
|
adamc@201
|
477 in
|
adamc@271
|
478 case database of
|
adamc@271
|
479 NONE => ds
|
adamc@271
|
480 | SOME s => (Source.DDatabase s, ErrorMsg.dummySpan) :: ds
|
adamc@201
|
481 end handle Empty => ds
|
adamc@201
|
482 end,
|
adamc@201
|
483 print = SourcePrint.p_file
|
adamc@201
|
484 }
|
adamc@56
|
485
|
adamc@270
|
486 val toParse = transform parse "parse" o toParseJob
|
adamc@38
|
487
|
adamc@378
|
488 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
|
adamc@378
|
489 file = s}
|
adamc@378
|
490 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
|
adamc@378
|
491 file = s}
|
adamc@378
|
492
|
adamc@201
|
493 val elaborate = {
|
adamc@201
|
494 func = fn file => let
|
adamc@378
|
495 val basis = #func parseUrs (libFile "basis.urs")
|
adamc@378
|
496 val topSgn = #func parseUrs (libFile "top.urs")
|
adamc@378
|
497 val topStr = #func parseUr (libFile "top.ur")
|
adamc@201
|
498 in
|
adamc@325
|
499 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
|
adamc@201
|
500 end,
|
adamc@201
|
501 print = ElabPrint.p_file ElabEnv.empty
|
adamc@201
|
502 }
|
adamc@5
|
503
|
adamc@270
|
504 val toElaborate = transform elaborate "elaborate" o toParse
|
adamc@201
|
505
|
adamc@448
|
506 val unnest = {
|
adamc@448
|
507 func = Unnest.unnest,
|
adamc@448
|
508 print = ElabPrint.p_file ElabEnv.empty
|
adamc@448
|
509 }
|
adamc@448
|
510
|
adamc@448
|
511 val toUnnest = transform unnest "unnest" o toElaborate
|
adamc@448
|
512
|
adamc@313
|
513 val termination = {
|
adamc@313
|
514 func = (fn file => (Termination.check file; file)),
|
adamc@313
|
515 print = ElabPrint.p_file ElabEnv.empty
|
adamc@313
|
516 }
|
adamc@313
|
517
|
adamc@448
|
518 val toTermination = transform termination "termination" o toUnnest
|
adamc@313
|
519
|
adamc@201
|
520 val explify = {
|
adamc@201
|
521 func = Explify.explify,
|
adamc@201
|
522 print = ExplPrint.p_file ExplEnv.empty
|
adamc@201
|
523 }
|
adamc@201
|
524
|
adamc@625
|
525 val toExplify = transform explify "explify" o toUnnest
|
adamc@201
|
526
|
adamc@201
|
527 val corify = {
|
adamc@201
|
528 func = Corify.corify,
|
adamc@201
|
529 print = CorePrint.p_file CoreEnv.empty
|
adamc@201
|
530 }
|
adamc@201
|
531
|
adamc@270
|
532 val toCorify = transform corify "corify" o toExplify
|
adamc@201
|
533
|
adamc@482
|
534 (*val reduce_local = {
|
adamc@482
|
535 func = ReduceLocal.reduce,
|
adamc@482
|
536 print = CorePrint.p_file CoreEnv.empty
|
adamc@482
|
537 }
|
adamc@482
|
538
|
adamc@482
|
539 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
|
adamc@482
|
540
|
adamc@443
|
541 val especialize = {
|
adamc@443
|
542 func = ESpecialize.specialize,
|
adamc@443
|
543 print = CorePrint.p_file CoreEnv.empty
|
adamc@443
|
544 }
|
adamc@443
|
545
|
adamc@443
|
546 val toEspecialize = transform especialize "especialize" o toCorify
|
adamc@443
|
547
|
adamc@454
|
548 val core_untangle = {
|
adamc@454
|
549 func = CoreUntangle.untangle,
|
adamc@454
|
550 print = CorePrint.p_file CoreEnv.empty
|
adamc@454
|
551 }
|
adamc@454
|
552
|
adamc@454
|
553 val toCore_untangle = transform core_untangle "core_untangle" o toEspecialize
|
adamc@454
|
554
|
adamc@202
|
555 val shake = {
|
adamc@202
|
556 func = Shake.shake,
|
adamc@202
|
557 print = CorePrint.p_file CoreEnv.empty
|
adamc@202
|
558 }
|
adamc@39
|
559
|
adamc@454
|
560 val toShake1 = transform shake "shake1" o toCore_untangle
|
adamc@110
|
561
|
adamc@607
|
562 val rpcify = {
|
adamc@607
|
563 func = Rpcify.frob,
|
adamc@607
|
564 print = CorePrint.p_file CoreEnv.empty
|
adamc@607
|
565 }
|
adamc@607
|
566
|
adamc@607
|
567 val toRpcify = transform rpcify "rpcify" o toShake1
|
adamc@607
|
568
|
adamc@642
|
569 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
|
adamc@642
|
570 val toShake2 = transform shake "shake2" o toCore_untangle2
|
adamc@642
|
571
|
adamc@202
|
572 val tag = {
|
adamc@202
|
573 func = Tag.tag,
|
adamc@202
|
574 print = CorePrint.p_file CoreEnv.empty
|
adamc@202
|
575 }
|
adamc@193
|
576
|
adamc@642
|
577 val toTag = transform tag "tag" o toCore_untangle2
|
adamc@20
|
578
|
adamc@202
|
579 val reduce = {
|
adamc@202
|
580 func = Reduce.reduce,
|
adamc@202
|
581 print = CorePrint.p_file CoreEnv.empty
|
adamc@202
|
582 }
|
adamc@25
|
583
|
adamc@692
|
584 val toReduce = transform reduce "reduce" o toTag
|
adamc@23
|
585
|
adamc@315
|
586 val unpoly = {
|
adamc@315
|
587 func = Unpoly.unpoly,
|
adamc@315
|
588 print = CorePrint.p_file CoreEnv.empty
|
adamc@315
|
589 }
|
adamc@315
|
590
|
adamc@315
|
591 val toUnpoly = transform unpoly "unpoly" o toReduce
|
adamc@315
|
592
|
adamc@202
|
593 val specialize = {
|
adamc@202
|
594 func = Specialize.specialize,
|
adamc@202
|
595 print = CorePrint.p_file CoreEnv.empty
|
adamc@202
|
596 }
|
adamc@132
|
597
|
adamc@315
|
598 val toSpecialize = transform specialize "specialize" o toUnpoly
|
adamc@131
|
599
|
adamc@642
|
600 val toShake3 = transform shake "shake3" o toSpecialize
|
adamc@133
|
601
|
adamc@692
|
602 val marshalcheck = {
|
adamc@692
|
603 func = (fn file => (MarshalCheck.check file; file)),
|
adamc@692
|
604 print = CorePrint.p_file CoreEnv.empty
|
adamc@692
|
605 }
|
adamc@692
|
606
|
adamc@692
|
607 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake3
|
adamc@692
|
608
|
adamc@732
|
609 val effectize = {
|
adamc@732
|
610 func = Effective.effectize,
|
adamc@732
|
611 print = CorePrint.p_file CoreEnv.empty
|
adamc@732
|
612 }
|
adamc@732
|
613
|
adamc@732
|
614 val toEffectize = transform effectize "effectize" o toMarshalcheck
|
adamc@732
|
615
|
adamc@202
|
616 val monoize = {
|
adamc@202
|
617 func = Monoize.monoize CoreEnv.empty,
|
adamc@202
|
618 print = MonoPrint.p_file MonoEnv.empty
|
adamc@202
|
619 }
|
adamc@134
|
620
|
adamc@732
|
621 val toMonoize = transform monoize "monoize" o toEffectize
|
adamc@96
|
622
|
adamc@202
|
623 val mono_opt = {
|
adamc@202
|
624 func = MonoOpt.optimize,
|
adamc@202
|
625 print = MonoPrint.p_file MonoEnv.empty
|
adamc@202
|
626 }
|
adamc@29
|
627
|
adamc@270
|
628 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
|
adamc@5
|
629
|
adamc@202
|
630 val untangle = {
|
adamc@202
|
631 func = Untangle.untangle,
|
adamc@202
|
632 print = MonoPrint.p_file MonoEnv.empty
|
adamc@202
|
633 }
|
adamc@1
|
634
|
adamc@270
|
635 val toUntangle = transform untangle "untangle" o toMono_opt1
|
adamc@38
|
636
|
adamc@202
|
637 val mono_reduce = {
|
adamc@202
|
638 func = MonoReduce.reduce,
|
adamc@202
|
639 print = MonoPrint.p_file MonoEnv.empty
|
adamc@202
|
640 }
|
adamc@16
|
641
|
adamc@270
|
642 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
|
adamc@39
|
643
|
adamc@202
|
644 val mono_shake = {
|
adamc@202
|
645 func = MonoShake.shake,
|
adamc@202
|
646 print = MonoPrint.p_file MonoEnv.empty
|
adamc@202
|
647 }
|
adamc@110
|
648
|
adamc@270
|
649 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
|
adamc@193
|
650
|
adamc@572
|
651 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
|
adamc@572
|
652
|
adamc@567
|
653 val jscomp = {
|
adamc@567
|
654 func = JsComp.process,
|
adamc@567
|
655 print = MonoPrint.p_file MonoEnv.empty
|
adamc@567
|
656 }
|
adamc@567
|
657
|
adamc@572
|
658 val toJscomp = transform jscomp "jscomp" o toMono_opt2
|
adamc@567
|
659
|
adamc@572
|
660 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
|
adamc@20
|
661
|
adamc@506
|
662 val fuse = {
|
adamc@506
|
663 func = Fuse.fuse,
|
adamc@506
|
664 print = MonoPrint.p_file MonoEnv.empty
|
adamc@506
|
665 }
|
adamc@506
|
666
|
adamc@572
|
667 val toFuse = transform fuse "fuse" o toMono_opt3
|
adamc@506
|
668
|
adamc@506
|
669 val toUntangle2 = transform untangle "untangle2" o toFuse
|
adamc@506
|
670
|
adamc@601
|
671 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
|
adamc@601
|
672 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
|
adamc@506
|
673
|
adamc@377
|
674 val pathcheck = {
|
adamc@377
|
675 func = (fn file => (PathCheck.check file; file)),
|
adamc@377
|
676 print = MonoPrint.p_file MonoEnv.empty
|
adamc@377
|
677 }
|
adamc@377
|
678
|
adamc@506
|
679 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake2
|
adamc@377
|
680
|
adamc@202
|
681 val cjrize = {
|
adamc@202
|
682 func = Cjrize.cjrize,
|
adamc@202
|
683 print = CjrPrint.p_file CjrEnv.empty
|
adamc@202
|
684 }
|
adamc@23
|
685
|
adamc@377
|
686 val toCjrize = transform cjrize "cjrize" o toPathcheck
|
adamc@29
|
687
|
adamc@643
|
688 val scriptcheck = {
|
adamc@643
|
689 func = ScriptCheck.classify,
|
adamc@643
|
690 print = CjrPrint.p_file CjrEnv.empty
|
adamc@643
|
691 }
|
adamc@643
|
692
|
adamc@643
|
693 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
|
adamc@643
|
694
|
adamc@282
|
695 val prepare = {
|
adamc@282
|
696 func = Prepare.prepare,
|
adamc@282
|
697 print = CjrPrint.p_file CjrEnv.empty
|
adamc@282
|
698 }
|
adamc@282
|
699
|
adamc@643
|
700 val toPrepare = transform prepare "prepare" o toScriptcheck
|
adamc@282
|
701
|
adamc@274
|
702 val sqlify = {
|
adamc@274
|
703 func = Cjrize.cjrize,
|
adamc@274
|
704 print = CjrPrint.p_sql CjrEnv.empty
|
adamc@274
|
705 }
|
adamc@274
|
706
|
adamc@274
|
707 val toSqlify = transform sqlify "sqlify" o toMono_opt2
|
adamc@274
|
708
|
adamc@764
|
709 fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
|
adamc@183
|
710 let
|
adamc@378
|
711 val urweb_o = clibFile "urweb.o"
|
adamc@378
|
712 val driver_o = clibFile "driver.o"
|
adamc@378
|
713
|
adamc@435
|
714 val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
|
adamc@734
|
715 val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
|
adamc@502
|
716
|
adamc@502
|
717 val (compile, link) =
|
adamc@502
|
718 if profile then
|
adamc@502
|
719 (compile ^ " -pg", link ^ " -pg")
|
adamc@502
|
720 else
|
adamc@502
|
721 (compile, link)
|
adamc@742
|
722
|
adamc@742
|
723 val (compile, link) =
|
adamc@742
|
724 if debug then
|
adamc@742
|
725 (compile ^ " -g", link ^ " -g")
|
adamc@742
|
726 else
|
adamc@742
|
727 (compile, link)
|
adamc@764
|
728
|
adamc@764
|
729 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
|
adamc@183
|
730 in
|
adamc@183
|
731 if not (OS.Process.isSuccess (OS.Process.system compile)) then
|
adamc@183
|
732 print "C compilation failed\n"
|
adamc@183
|
733 else if not (OS.Process.isSuccess (OS.Process.system link)) then
|
adamc@186
|
734 print "C linking failed\n"
|
adamc@183
|
735 else
|
adamc@673
|
736 ()
|
adamc@183
|
737 end
|
adamc@183
|
738
|
adamc@202
|
739 fun compile job =
|
adamc@282
|
740 case run toPrepare job of
|
adamc@244
|
741 NONE => print "Ur compilation failed\n"
|
adamc@29
|
742 | SOME file =>
|
adamc@202
|
743 let
|
adamc@274
|
744 val job = valOf (run (transform parseUrp "parseUrp") job)
|
adamc@102
|
745
|
adamc@274
|
746 val (cname, oname, cleanup) =
|
adamc@274
|
747 if #debug job then
|
adamc@457
|
748 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
|
adamc@274
|
749 else
|
adamc@274
|
750 let
|
adamc@274
|
751 val dir = OS.FileSys.tmpName ()
|
adamc@403
|
752 val () = if OS.FileSys.access (dir, []) then
|
adamc@403
|
753 OS.FileSys.remove dir
|
adamc@403
|
754 else
|
adamc@403
|
755 ()
|
adamc@457
|
756 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
|
adamc@457
|
757 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
|
adamc@274
|
758 in
|
adamc@274
|
759 OS.FileSys.mkDir dir;
|
adamc@274
|
760 (cname, oname,
|
adamc@274
|
761 fn () => (OS.FileSys.remove cname;
|
adamc@274
|
762 OS.FileSys.remove oname;
|
adamc@473
|
763 OS.FileSys.rmDir dir)
|
adamc@473
|
764 handle OS.SysErr _ => OS.FileSys.rmDir dir)
|
adamc@274
|
765 end
|
adamc@274
|
766 val ename = #exe job
|
adamc@202
|
767 in
|
adamc@274
|
768 let
|
adamc@274
|
769 val outf = TextIO.openOut cname
|
adamc@274
|
770 val s = TextIOPP.openOut {dst = outf, wid = 80}
|
adamc@432
|
771
|
adamc@432
|
772 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
|
adamc@432
|
773 val libs =
|
adamc@432
|
774 if hasDb then
|
adamc@432
|
775 "-lpq"
|
adamc@432
|
776 else
|
adamc@432
|
777 ""
|
adamc@274
|
778 in
|
adamc@274
|
779 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
|
adamc@415
|
780 TextIO.output1 (outf, #"\n");
|
adamc@274
|
781 TextIO.closeOut outf;
|
adamc@102
|
782
|
adamc@274
|
783 case #sql job of
|
adamc@274
|
784 NONE => ()
|
adamc@274
|
785 | SOME sql =>
|
adamc@274
|
786 let
|
adamc@274
|
787 val outf = TextIO.openOut sql
|
adamc@274
|
788 val s = TextIOPP.openOut {dst = outf, wid = 80}
|
adamc@274
|
789 in
|
adamc@274
|
790 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
|
adamc@274
|
791 TextIO.closeOut outf
|
adamc@274
|
792 end;
|
adamc@274
|
793
|
adamc@742
|
794 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
|
adamc@764
|
795 profile = #profile job, debug = #debug job, link = #link job};
|
adamc@274
|
796
|
adamc@274
|
797 cleanup ()
|
adamc@274
|
798 end
|
adamc@274
|
799 handle ex => (((cleanup ()) handle _ => ()); raise ex)
|
adamc@202
|
800 end
|
adamc@29
|
801
|
adamc@1
|
802 end
|