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