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