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