annotate src/compiler.sml @ 1170:52c6ac6a59f1

Basic analysis of tag and CSS class usage
author Adam Chlipala <adamc@hcoop.net>
date Sat, 27 Feb 2010 14:57:57 -0500
parents 8679ba87cf3c
children 7a2a7a8f9cab
rev   line source
adamc@1132 1 (* Copyright (c) 2008-2010, 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@768 53 jsFuncs : (Settings.ffi * string) list,
adamc@769 54 rewrites : Settings.rewrite list,
adamc@769 55 filterUrl : Settings.rule list,
adamc@866 56 filterMime : Settings.rule list,
adamc@866 57 protocol : string option,
adamc@1164 58 dbms : string option,
adamc@1164 59 sigFile : string option
adamc@270 60 }
adamc@201 61
adamc@201 62 type ('src, 'dst) phase = {
adamc@201 63 func : 'src -> 'dst,
adamc@201 64 print : 'dst -> Print.PD.pp_desc
adamc@201 65 }
adamc@201 66
adamc@201 67 type pmap = (string * Time.time) list
adamc@201 68
adamc@201 69 type ('src, 'dst) transform = {
adamc@201 70 func : 'src -> 'dst option,
adamc@201 71 print : 'dst -> Print.PD.pp_desc,
adamc@201 72 time : 'src * pmap -> 'dst option * pmap
adamc@201 73 }
adamc@201 74
adamc@1079 75 val debug = ref false
adamc@1079 76
adamc@201 77 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 78 func = fn input => let
adamc@1079 79 val () = if !debug then
adamc@1079 80 print ("Starting " ^ name ^ "....\n")
adamc@1079 81 else
adamc@1079 82 ()
adamc@201 83 val v = #func ph input
adamc@201 84 in
adamc@1079 85 if !debug then
adamc@1079 86 print ("Finished " ^ name ^ ".\n")
adamc@1079 87 else
adamc@1079 88 ();
adamc@201 89 if ErrorMsg.anyErrors () then
adamc@201 90 NONE
adamc@201 91 else
adamc@201 92 SOME v
adamc@201 93 end,
adamc@201 94 print = #print ph,
adamc@201 95 time = fn (input, pmap) => let
adamc@201 96 val befor = Time.now ()
adamc@201 97 val v = #func ph input
adamc@201 98 val elapsed = Time.- (Time.now (), befor)
adamc@201 99 in
adamc@201 100 (if ErrorMsg.anyErrors () then
adamc@201 101 NONE
adamc@201 102 else
adamc@201 103 SOME v,
adamc@201 104 (name, elapsed) :: pmap)
adamc@201 105 end
adamc@201 106 }
adamc@201 107
adamc@346 108 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 109 ignore (#func tr x))
adamc@346 110
adamc@280 111 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 112 #func tr x)
adamc@201 113
adamc@201 114 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 115 (ErrorMsg.resetErrors ();
adamc@280 116 case #func tr input of
adamc@280 117 NONE => print "Failure\n"
adamc@280 118 | SOME v =>
adamc@280 119 (print "Success\n";
adamc@280 120 Print.print (#print tr v);
adamc@280 121 print "\n"))
adamc@201 122
adamc@201 123 fun time (tr : ('src, 'dst) transform) input =
adamc@55 124 let
adamc@201 125 val (_, pmap) = #time tr (input, [])
adamc@201 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 end
adamc@55 132
adamc@201 133 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 134 let
adamc@201 135 val (ro, pmap) = #time tr (input, [])
adamc@55 136 in
adamc@201 137 app (fn (name, time) =>
adamc@201 138 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 139 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 140 print "\n";
adamc@201 141 case ro of
adamc@201 142 NONE => print "Failure\n"
adamc@201 143 | SOME v =>
adamc@201 144 (print "Success\n";
adamc@201 145 Print.print (#print tr v);
adamc@201 146 print "\n")
adamc@55 147 end
adamc@55 148
adamc@244 149 val parseUrs =
adamc@201 150 {func = fn filename => let
adamc@201 151 val fname = OS.FileSys.tmpName ()
adamc@201 152 val outf = TextIO.openOut fname
adamc@201 153 val () = TextIO.output (outf, "sig\n")
adamc@201 154 val inf = TextIO.openIn filename
adamc@201 155 fun loop () =
adamc@201 156 case TextIO.inputLine inf of
adamc@201 157 NONE => ()
adamc@201 158 | SOME line => (TextIO.output (outf, line);
adamc@201 159 loop ())
adamc@201 160 val () = loop ()
adamc@201 161 val () = TextIO.closeIn inf
adamc@201 162 val () = TextIO.closeOut outf
adamc@201 163
adamc@201 164 val () = (ErrorMsg.resetErrors ();
adamc@201 165 ErrorMsg.resetPositioning filename;
adamc@201 166 Lex.UserDeclarations.initialize ())
adamc@201 167 val file = TextIO.openIn fname
adamc@201 168 fun get _ = TextIO.input file
adamc@201 169 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 170 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 171 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 172 in
adamc@201 173 TextIO.closeIn file;
adamc@201 174 case absyn of
adamc@201 175 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 176 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 177 first = {line = 0,
adamc@201 178 char = 0},
adamc@201 179 last = {line = 0,
adamc@201 180 char = 0}} "Not a signature";
adamc@201 181 [])
adamc@201 182 end
adamc@201 183 handle LrParser.ParseError => [],
adamc@201 184 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 185
adamc@1 186 (* The main parsing routine *)
adamc@244 187 val parseUr = {
adamc@201 188 func = fn filename =>
adamc@201 189 let
adamc@201 190 val () = (ErrorMsg.resetErrors ();
adamc@201 191 ErrorMsg.resetPositioning filename;
adamc@201 192 Lex.UserDeclarations.initialize ())
adamc@201 193 val file = TextIO.openIn filename
adamc@201 194 fun get _ = TextIO.input file
adamc@201 195 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 196 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 197 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 198 in
adamc@201 199 TextIO.closeIn file;
adamc@201 200 case absyn of
adamc@201 201 [(Source.DSgn ("?", _), _)] =>
adamc@201 202 (ErrorMsg.errorAt {file = filename,
adamc@201 203 first = {line = 0,
adamc@201 204 char = 0},
adamc@201 205 last = {line = 0,
adamc@201 206 char = 0}} "File starts with 'sig'";
adamc@201 207 [])
adamc@201 208 | _ => absyn
adamc@201 209 end
adamc@201 210 handle LrParser.ParseError => [],
adamc@201 211 print = SourcePrint.p_file}
adamc@56 212
adamc@768 213 fun p_job ({prefix, database, exe, sql, sources, debug, profile,
adamc@768 214 timeout, ffi, link, headers, scripts,
adamc@768 215 clientToServer, effectful, clientOnly, serverOnly, jsFuncs, ...} : job) =
adamc@270 216 let
adamc@270 217 open Print.PD
adamc@270 218 open Print
adamc@765 219
adamc@765 220 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
adamc@765 221 box [string name, space, string m, string ".", string s, newline])
adamc@270 222 in
adamc@274 223 box [if debug then
adamc@274 224 box [string "DEBUG", newline]
adamc@274 225 else
adamc@274 226 box [],
adamc@502 227 if profile then
adamc@502 228 box [string "PROFILE", newline]
adamc@502 229 else
adamc@502 230 box [],
adamc@274 231 case database of
adamc@270 232 NONE => string "No database."
adamc@270 233 | SOME db => string ("Database: " ^ db),
adamc@270 234 newline,
adamc@274 235 string "Exe: ",
adamc@274 236 string exe,
adamc@274 237 newline,
adamc@274 238 case sql of
adamc@274 239 NONE => string "No SQL file."
adamc@274 240 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 241 newline,
adamc@673 242 string "Timeout: ",
adamc@673 243 string (Int.toString timeout),
adamc@673 244 newline,
adamc@764 245 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
adamc@764 246 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
adamc@766 247 p_list_sep (box []) (fn s => box [string "Script", space, string s, newline]) scripts,
adamc@764 248 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
adamc@765 249 p_ffi "ClientToServer" clientToServer,
adamc@765 250 p_ffi "Effectful" effectful,
adamc@765 251 p_ffi "ClientOnly" clientOnly,
adamc@765 252 p_ffi "ServerOnly" serverOnly,
adamc@765 253 p_list_sep (box []) (fn ((m, s), s') =>
adamc@765 254 box [string "JsFunc", space, string m, string ".", string s,
adamc@765 255 space, string "=", space, string s', newline]) jsFuncs,
adamc@270 256 string "Sources:",
adamc@270 257 p_list string sources,
adamc@270 258 newline]
adamc@270 259 end
adamc@270 260
adamc@270 261 fun trim s =
adamc@270 262 let
adamc@270 263 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 264 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 265 in
adamc@270 266 s
adamc@270 267 end
adamc@270 268
adamc@794 269 structure M = BinaryMapFn(struct
adamc@794 270 type ord_key = string
adamc@794 271 val compare = String.compare
adamc@794 272 end)
adamc@794 273
adamc@1089 274 val pathmap = ref (M.insert (M.empty, "", Config.libUr))
adamc@1089 275
adamc@1089 276 fun addPath (k, v) = pathmap := M.insert (!pathmap, k, v)
adamc@1089 277
adamc@1082 278 fun parseUrp' accLibs fname =
adamc@767 279 let
adamc@1089 280 val pathmap = ref (!pathmap)
adamc@1082 281 val bigLibs = ref []
adamc@767 282
adamc@794 283 fun pu filename =
adamc@794 284 let
adamc@794 285 val dir = OS.Path.dir filename
adamc@1151 286 fun opener () = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@1151 287
adamc@1151 288 val inf = opener ()
adamc@1151 289
adamc@1157 290 fun hasSpaceLine () =
adamc@1151 291 case TextIO.inputLine inf of
adamc@1151 292 NONE => false
adamc@1158 293 | SOME s => CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
adamc@1151 294
adamc@1157 295 val hasBlankLine = hasSpaceLine ()
adamc@1151 296
adamc@1151 297 val inf = (TextIO.closeIn inf; opener ())
adamc@767 298
adamc@794 299 fun pathify fname =
adamc@794 300 if size fname > 0 andalso String.sub (fname, 0) = #"$" then
adamc@794 301 let
adamc@794 302 val fname' = Substring.extract (fname, 1, NONE)
adamc@794 303 val (befor, after) = Substring.splitl (fn ch => ch <> #"/") fname'
adamc@794 304 in
adamc@794 305 if Substring.isEmpty after then
adamc@794 306 fname
adamc@794 307 else
adamc@794 308 case M.find (!pathmap, Substring.string befor) of
adamc@794 309 NONE => fname
adamc@794 310 | SOME rep => rep ^ Substring.string after
adamc@794 311 end
adamc@794 312 else
adamc@794 313 fname
adamc@767 314
adamc@794 315 fun relify fname =
adamc@794 316 let
adamc@794 317 val fname = pathify fname
adamc@794 318 in
adamc@794 319 OS.Path.concat (dir, fname)
adamc@794 320 handle OS.Path.Path => fname
adamc@794 321 end
adamc@767 322
adamc@1089 323 fun libify path =
adamc@1089 324 (if Posix.FileSys.access (path ^ ".urp", []) then
adamc@1089 325 path
adamc@1089 326 else
adamc@1089 327 path ^ "/lib")
adamc@1089 328 handle SysErr => path
adamc@1089 329
adamc@1089 330 fun libify' path =
adamc@1089 331 (if Posix.FileSys.access (relify path ^ ".urp", []) then
adamc@1089 332 path
adamc@1089 333 else
adamc@1089 334 path ^ "/lib")
adamc@1089 335 handle SysErr => path
adamc@1089 336
adamc@794 337 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@767 338
adamc@794 339 fun relifyA fname =
adamc@794 340 OS.Path.mkAbsolute {path = pathify fname, relativeTo = absDir}
adamc@767 341
adamc@794 342 fun readSources acc =
adamc@794 343 case TextIO.inputLine inf of
adamc@794 344 NONE => rev acc
adamc@794 345 | SOME line =>
adamc@794 346 let
adamc@794 347 val acc = if CharVector.all Char.isSpace line then
adamc@794 348 acc
adamc@794 349 else
adamc@794 350 let
adamc@794 351 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adamc@794 352 (String.explode line))
adamc@1126 353 val fname = relifyA fname
adamc@794 354 in
adamc@794 355 fname :: acc
adamc@794 356 end
adamc@794 357 in
adamc@794 358 readSources acc
adamc@794 359 end
adamc@767 360
adamc@794 361 val prefix = ref NONE
adamc@891 362 val database = ref (Settings.getDbstring ())
adamc@891 363 val exe = ref (Settings.getExe ())
adamc@891 364 val sql = ref (Settings.getSql ())
adamc@857 365 val debug = ref (Settings.getDebug ())
adamc@794 366 val profile = ref false
adamc@794 367 val timeout = ref NONE
adamc@794 368 val ffi = ref []
adamc@794 369 val link = ref []
adamc@794 370 val headers = ref []
adamc@794 371 val scripts = ref []
adamc@794 372 val clientToServer = ref []
adamc@794 373 val effectful = ref []
adamc@794 374 val clientOnly = ref []
adamc@794 375 val serverOnly = ref []
adamc@794 376 val jsFuncs = ref []
adamc@794 377 val rewrites = ref []
adamc@794 378 val url = ref []
adamc@794 379 val mime = ref []
adamc@794 380 val libs = ref []
adamc@866 381 val protocol = ref NONE
adamc@866 382 val dbms = ref NONE
adamc@1164 383 val sigFile = ref (Settings.getSigFile ())
adamc@767 384
adamc@794 385 fun finish sources =
adamc@794 386 let
adamc@794 387 val job = {
adamc@794 388 prefix = Option.getOpt (!prefix, "/"),
adamc@794 389 database = !database,
adamc@794 390 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adamc@794 391 ext = SOME "exe"}),
adamc@794 392 sql = !sql,
adamc@794 393 debug = !debug,
adamc@794 394 profile = !profile,
adamc@794 395 timeout = Option.getOpt (!timeout, 60),
adamc@794 396 ffi = rev (!ffi),
adamc@794 397 link = rev (!link),
adamc@794 398 headers = rev (!headers),
adamc@794 399 scripts = rev (!scripts),
adamc@794 400 clientToServer = rev (!clientToServer),
adamc@794 401 effectful = rev (!effectful),
adamc@794 402 clientOnly = rev (!clientOnly),
adamc@794 403 serverOnly = rev (!serverOnly),
adamc@794 404 jsFuncs = rev (!jsFuncs),
adamc@794 405 rewrites = rev (!rewrites),
adamc@794 406 filterUrl = rev (!url),
adamc@794 407 filterMime = rev (!mime),
adamc@866 408 sources = sources,
adamc@866 409 protocol = !protocol,
adamc@1164 410 dbms = !dbms,
adamc@1164 411 sigFile = !sigFile
adamc@794 412 }
adamc@767 413
adamc@794 414 fun mergeO f (old, new) =
adamc@794 415 case (old, new) of
adamc@794 416 (NONE, _) => new
adamc@794 417 | (_, NONE) => old
adamc@794 418 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adamc@794 419
adamc@794 420 fun same desc = mergeO (fn (x : string, y) =>
adamc@794 421 (if x = y then
adamc@794 422 ()
adamc@794 423 else
adamc@794 424 ErrorMsg.error ("Multiple "
adamc@794 425 ^ desc ^ " values that don't agree");
adamc@794 426 x))
adamc@794 427
adamc@794 428 fun merge (old : job, new : job) = {
adamc@794 429 prefix = #prefix old,
adamc@891 430 database = mergeO (fn (old, _) => old) (#database old, #database new),
adamc@794 431 exe = #exe old,
adamc@794 432 sql = #sql old,
adamc@794 433 debug = #debug old orelse #debug new,
adamc@794 434 profile = #profile old orelse #profile new,
adamc@794 435 timeout = #timeout old,
adamc@794 436 ffi = #ffi old @ #ffi new,
adamc@794 437 link = #link old @ #link new,
adamc@794 438 headers = #headers old @ #headers new,
adamc@794 439 scripts = #scripts old @ #scripts new,
adamc@794 440 clientToServer = #clientToServer old @ #clientToServer new,
adamc@794 441 effectful = #effectful old @ #effectful new,
adamc@794 442 clientOnly = #clientOnly old @ #clientOnly new,
adamc@794 443 serverOnly = #serverOnly old @ #serverOnly new,
adamc@794 444 jsFuncs = #jsFuncs old @ #jsFuncs new,
adamc@794 445 rewrites = #rewrites old @ #rewrites new,
adamc@794 446 filterUrl = #filterUrl old @ #filterUrl new,
adamc@794 447 filterMime = #filterMime old @ #filterMime new,
adamc@1092 448 sources = #sources new
adamc@1092 449 @ List.filter (fn s => List.all (fn s' => s' <> s) (#sources new))
adamc@1092 450 (#sources old),
adamc@866 451 protocol = mergeO #2 (#protocol old, #protocol new),
adamc@1164 452 dbms = mergeO #2 (#dbms old, #dbms new),
adamc@1164 453 sigFile = mergeO #2 (#sigFile old, #sigFile new)
adamc@794 454 }
adamc@794 455 in
adamc@1082 456 if accLibs then
adamc@1082 457 foldr (fn (job', job) => merge (job, job')) job (!libs)
adamc@1082 458 else
adamc@1082 459 job
adamc@794 460 end
adamc@794 461
adamc@794 462 fun parsePkind s =
adamc@794 463 case s of
adamc@794 464 "all" => Settings.Any
adamc@794 465 | "url" => Settings.Url
adamc@794 466 | "table" => Settings.Table
adamc@794 467 | "sequence" => Settings.Sequence
adamc@794 468 | "view" => Settings.View
adamc@794 469 | "relation" => Settings.Relation
adamc@794 470 | "cookie" => Settings.Cookie
adamc@794 471 | "style" => Settings.Style
adamc@794 472 | _ => (ErrorMsg.error "Bad path kind spec";
adamc@794 473 Settings.Any)
adamc@794 474
adamc@794 475 fun parseFrom s =
adamc@794 476 if size s > 1 andalso String.sub (s, size s - 2) = #"/" andalso String.sub (s, size s - 1) = #"*" then
adamc@794 477 (Settings.Prefix, String.substring (s, 0, size s - 1))
adamc@794 478 else
adamc@794 479 (Settings.Exact, s)
adamc@794 480
adamc@794 481 fun parseFkind s =
adamc@794 482 case s of
adamc@794 483 "url" => url
adamc@794 484 | "mime" => mime
adamc@794 485 | _ => (ErrorMsg.error "Bad filter kind";
adamc@794 486 url)
adamc@794 487
adamc@794 488 fun parsePattern s =
adamc@794 489 if size s > 0 andalso String.sub (s, size s - 1) = #"*" then
adamc@794 490 (Settings.Prefix, String.substring (s, 0, size s - 1))
adamc@794 491 else
adamc@794 492 (Settings.Exact, s)
adamc@794 493
adamc@794 494 fun read () =
adamc@794 495 case TextIO.inputLine inf of
adamc@794 496 NONE => finish []
adamc@794 497 | SOME "\n" => finish (readSources [])
adamc@794 498 | SOME line =>
adamc@794 499 let
adamc@794 500 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@794 501 val cmd = Substring.string (trim cmd)
adamc@794 502 val arg = Substring.string (trim arg)
adamc@794 503
adamc@794 504 fun ffiS () =
adamc@794 505 case String.fields (fn ch => ch = #".") arg of
adamc@794 506 [m, x] => (m, x)
adamc@794 507 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adamc@794 508 ("", ""))
adamc@794 509
adamc@794 510 fun ffiM () =
adamc@794 511 case String.fields (fn ch => ch = #"=") arg of
adamc@794 512 [f, s] =>
adamc@794 513 (case String.fields (fn ch => ch = #".") f of
adamc@794 514 [m, x] => ((m, x), s)
adamc@794 515 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@794 516 (("", ""), "")))
adamc@794 517 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@794 518 (("", ""), ""))
adamc@794 519 in
adamc@794 520 case cmd of
adamc@794 521 "prefix" =>
adamc@794 522 (case !prefix of
adamc@794 523 NONE => ()
adamc@794 524 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
adamc@794 525 prefix := SOME arg)
adamc@794 526 | "database" =>
adamc@794 527 (case !database of
adamc@891 528 NONE => database := SOME arg
adamc@891 529 | SOME _ => ())
adamc@1164 530 | "dbms" =>
adamc@1164 531 (case !dbms of
adamc@1164 532 NONE => dbms := SOME arg
adamc@1164 533 | SOME _ => ())
adamc@1164 534 | "sigfile" =>
adamc@1164 535 (case !sigFile of
adamc@1164 536 NONE => sigFile := SOME arg
adamc@1164 537 | SOME _ => ())
adamc@794 538 | "exe" =>
adamc@794 539 (case !exe of
adamc@891 540 NONE => exe := SOME (relify arg)
adamc@891 541 | SOME _ => ())
adamc@794 542 | "sql" =>
adamc@794 543 (case !sql of
adamc@891 544 NONE => sql := SOME (relify arg)
adamc@891 545 | SOME _ => ())
adamc@794 546 | "debug" => debug := true
adamc@794 547 | "profile" => profile := true
adamc@794 548 | "timeout" =>
adamc@794 549 (case !timeout of
adamc@794 550 NONE => ()
adamc@794 551 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adamc@794 552 timeout := SOME (valOf (Int.fromString arg)))
adamc@794 553 | "ffi" => ffi := relify arg :: !ffi
adamc@1061 554 | "link" => let
adamc@1096 555 val arg = if size arg >= 1
adamc@1096 556 andalso String.sub (arg, 0) = #"-" then
adamc@1061 557 arg
adamc@1061 558 else
adamc@1061 559 relifyA arg
adamc@1061 560 in
adamc@1061 561 link := arg :: !link
adamc@1061 562 end
adamc@794 563 | "include" => headers := relifyA arg :: !headers
adamc@794 564 | "script" => scripts := arg :: !scripts
adamc@794 565 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adamc@794 566 | "effectful" => effectful := ffiS () :: !effectful
adamc@794 567 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adamc@794 568 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adamc@794 569 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adamc@794 570 | "rewrite" =>
adamc@794 571 let
adamc@794 572 fun doit (pkind, from, to) =
adamc@794 573 let
adamc@794 574 val pkind = parsePkind pkind
adamc@794 575 val (kind, from) = parseFrom from
adamc@794 576 in
adamc@794 577 rewrites := {pkind = pkind, kind = kind, from = from, to = to} :: !rewrites
adamc@794 578 end
adamc@794 579 in
adamc@794 580 case String.tokens Char.isSpace arg of
adamc@794 581 [pkind, from, to] => doit (pkind, from, to)
adamc@794 582 | [pkind, from] => doit (pkind, from, "")
adamc@794 583 | _ => ErrorMsg.error "Bad 'rewrite' syntax"
adamc@794 584 end
adamc@794 585 | "allow" =>
adamc@794 586 (case String.tokens Char.isSpace arg of
adamc@794 587 [fkind, pattern] =>
adamc@794 588 let
adamc@794 589 val fkind = parseFkind fkind
adamc@794 590 val (kind, pattern) = parsePattern pattern
adamc@794 591 in
adamc@794 592 fkind := {action = Settings.Allow, kind = kind, pattern = pattern} :: !fkind
adamc@794 593 end
adamc@794 594 | _ => ErrorMsg.error "Bad 'allow' syntax")
adamc@794 595 | "deny" =>
adamc@794 596 (case String.tokens Char.isSpace arg of
adamc@794 597 [fkind, pattern] =>
adamc@794 598 let
adamc@794 599 val fkind = parseFkind fkind
adamc@794 600 val (kind, pattern) = parsePattern pattern
adamc@794 601 in
adamc@794 602 fkind := {action = Settings.Deny, kind = kind, pattern = pattern} :: !fkind
adamc@794 603 end
adamc@794 604 | _ => ErrorMsg.error "Bad 'deny' syntax")
adamc@1082 605 | "library" => if accLibs then
adamc@1089 606 libs := pu (libify (relify arg)) :: !libs
adamc@1082 607 else
adamc@1089 608 bigLibs := libify' arg :: !bigLibs
adamc@794 609 | "path" =>
adamc@794 610 (case String.fields (fn ch => ch = #"=") arg of
adamc@794 611 [n, v] => pathmap := M.insert (!pathmap, n, v)
adamc@794 612 | _ => ErrorMsg.error "path argument not of the form name=value'")
adamc@794 613 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@794 614 read ()
adamc@794 615 end
adamc@794 616
adamc@1151 617 val job = if hasBlankLine then
adamc@1151 618 read ()
adamc@1151 619 else
adamc@1151 620 finish (readSources [])
adamc@767 621 in
adamc@794 622 TextIO.closeIn inf;
adamc@794 623 Settings.setUrlPrefix (#prefix job);
adamc@794 624 Settings.setTimeout (#timeout job);
adamc@794 625 Settings.setHeaders (#headers job);
adamc@794 626 Settings.setScripts (#scripts job);
adamc@794 627 Settings.setClientToServer (#clientToServer job);
adamc@794 628 Settings.setEffectful (#effectful job);
adamc@794 629 Settings.setClientOnly (#clientOnly job);
adamc@794 630 Settings.setServerOnly (#serverOnly job);
adamc@794 631 Settings.setJsFuncs (#jsFuncs job);
adamc@794 632 Settings.setRewriteRules (#rewrites job);
adamc@794 633 Settings.setUrlRules (#filterUrl job);
adamc@794 634 Settings.setMimeRules (#filterMime job);
adamc@866 635 Option.app Settings.setProtocol (#protocol job);
adamc@866 636 Option.app Settings.setDbms (#dbms job);
adamc@794 637 job
adamc@767 638 end
adamc@767 639 in
adamc@1082 640 {Job = pu fname, Libs = !bigLibs}
adamc@767 641 end
adamc@767 642
adamc@1082 643 fun p_job' {Job = j, Libs = _ : string list} = p_job j
adamc@1082 644
adamc@270 645 val parseUrp = {
adamc@1083 646 func = #Job o parseUrp' true,
adamc@270 647 print = p_job
adamc@270 648 }
adamc@270 649
adamc@1082 650 val parseUrp' = {
adamc@1083 651 func = parseUrp' false,
adamc@1082 652 print = p_job'
adamc@1082 653 }
adamc@1082 654
adamc@270 655 val toParseJob = transform parseUrp "parseJob"
adamc@1082 656 val toParseJob' = transform parseUrp' "parseJob'"
adamc@1082 657
adamc@1082 658 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@1082 659 func = fn input => case #func tr1 input of
adamc@1082 660 NONE => NONE
adamc@1082 661 | SOME v => #func tr2 v,
adamc@1082 662 print = #print tr2,
adamc@1082 663 time = fn (input, pmap) => let
adamc@1082 664 val (ro, pmap) = #time tr1 (input, pmap)
adamc@1082 665 in
adamc@1082 666 case ro of
adamc@1082 667 NONE => (NONE, pmap)
adamc@1082 668 | SOME v => #time tr2 (v, pmap)
adamc@1082 669 end
adamc@1082 670 }
adamc@270 671
adamc@56 672 fun capitalize "" = ""
adamc@56 673 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 674
adamc@1090 675 structure SM = BinaryMapFn(struct
adamc@1090 676 type ord_key = string
adamc@1090 677 val compare = String.compare
adamc@1090 678 end)
adamc@1090 679
adamc@1090 680 val moduleRoots = ref ([] : (string * string) list)
adamc@1090 681 fun addModuleRoot (k, v) = moduleRoots := (k, v) :: !moduleRoots
adamc@1090 682
adamc@1090 683 structure SS = BinarySetFn(struct
adamc@1090 684 type ord_key = string
adamc@1090 685 val compare = String.compare
adamc@1090 686 end)
adamc@1090 687
adamc@201 688 val parse = {
adamc@764 689 func = fn {database, sources = fnames, ffi, ...} : job =>
adamc@201 690 let
adamc@1090 691 val mrs = !moduleRoots
adamc@1090 692
adamc@834 693 val anyErrors = ref false
adamc@834 694 fun checkErrors () = anyErrors := (!anyErrors orelse ErrorMsg.anyErrors ())
adamc@201 695 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 696
adamc@764 697 fun parseFfi fname =
adamc@764 698 let
adamc@764 699 val mname = nameOf fname
adamc@764 700 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 701
adamc@764 702 val loc = {file = urs,
adamc@764 703 first = ErrorMsg.dummyPos,
adamc@764 704 last = ErrorMsg.dummyPos}
adamc@764 705
adamc@764 706 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 707 in
adamc@834 708 checkErrors ();
adamc@764 709 (Source.DFfiStr (mname, sgn), loc)
adamc@764 710 end
adamc@764 711
adamc@1090 712 val defed = ref SS.empty
adamc@1092 713 val fulls = ref SS.empty
adamc@1090 714
adamc@201 715 fun parseOne fname =
adamc@201 716 let
adamc@201 717 val mname = nameOf fname
adamc@244 718 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 719 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 720
adamc@201 721 val sgnO =
adamc@244 722 if Posix.FileSys.access (urs, []) then
adamc@244 723 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 724 {file = urs,
adamc@201 725 first = ErrorMsg.dummyPos,
adamc@201 726 last = ErrorMsg.dummyPos})
adamc@834 727 before checkErrors ()
adamc@201 728 else
adamc@201 729 NONE
adamc@56 730
adamc@244 731 val loc = {file = ur,
adamc@201 732 first = ErrorMsg.dummyPos,
adamc@201 733 last = ErrorMsg.dummyPos}
adamc@56 734
adamc@244 735 val ds = #func parseUr ur
adamc@1090 736 val d = (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@1090 737
adamc@1126 738 val fname = OS.Path.mkCanonical fname
adamc@1090 739 val d = case List.find (fn (root, name) =>
adamc@1090 740 String.isPrefix (root ^ "/") fname) mrs of
adamc@1090 741 NONE => d
adamc@1090 742 | SOME (root, name) =>
adamc@1090 743 let
adamc@1090 744 val fname = String.extract (fname, size root + 1, NONE)
adamc@1090 745 val pieces = name :: String.tokens (fn ch => ch = #"/") fname
adamc@1090 746 val pieces = List.filter (fn s => size s > 0
adamc@1090 747 andalso Char.isAlpha (String.sub (s, 0)))
adamc@1090 748 pieces
adamc@1090 749 val pieces = map capitalize pieces
adamc@1092 750 val full = String.concatWith "." pieces
adamc@1090 751
adamc@1090 752 fun makeD prefix pieces =
adamc@1090 753 case pieces of
adamc@1090 754 [] => (ErrorMsg.error "Empty module path";
adamc@1090 755 (Source.DStyle "Boo", loc))
adamc@1090 756 | [_] => d
adamc@1090 757 | piece :: pieces =>
adamc@1090 758 let
adamc@1146 759 val this = case prefix of
adamc@1146 760 "" => piece
adamc@1146 761 | _ => prefix ^ "." ^ piece
adamc@1090 762 val old = SS.member (!defed, this)
adamc@1146 763
adamc@1146 764 fun notThere (ch, s) =
adamc@1146 765 Substring.isEmpty (#2 (Substring.splitl
adamc@1146 766 (fn ch' => ch' <> ch) s))
adamc@1146 767
adamc@1146 768 fun simOpen () =
adamc@1146 769 SS.foldl (fn (full, ds) =>
adamc@1146 770 if String.isPrefix (this ^ ".") full
adamc@1146 771 andalso notThere (#".",
adamc@1146 772 Substring.extract (full,
adamc@1146 773 size
adamc@1146 774 this + 1,
adamc@1146 775 NONE)) then
adamc@1146 776 let
adamc@1146 777 val parts = String.tokens
adamc@1146 778 (fn ch => ch = #".") full
adamc@1146 779
adamc@1146 780 val part = List.last parts
adamc@1146 781
adamc@1146 782 val imp = if length parts >= 2 then
adamc@1146 783 (Source.StrProj
adamc@1146 784 ((Source.StrVar
adamc@1146 785 (List.nth (parts,
adamc@1146 786 length
adamc@1146 787 parts
adamc@1146 788 - 2)),
adamc@1146 789 loc),
adamc@1146 790 part), loc)
adamc@1146 791 else
adamc@1146 792 (Source.StrVar part, loc)
adamc@1146 793 in
adamc@1146 794 (Source.DStr (part, NONE, imp),
adamc@1146 795 loc) :: ds
adamc@1146 796 end
adamc@1146 797 else
adamc@1146 798 ds) [] (!fulls)
adamc@1090 799 in
adamc@1090 800 defed := SS.add (!defed, this);
adamc@1090 801 (Source.DStr (piece, NONE,
adamc@1090 802 (Source.StrConst (if old then
adamc@1146 803 simOpen ()
adamc@1146 804 @ [makeD this pieces]
adamc@1090 805 else
adamc@1146 806 [makeD this pieces]), loc)),
adamc@1090 807 loc)
adamc@1090 808 end
adamc@1090 809 in
adamc@1092 810 if SS.member (!fulls, full) then
adamc@1092 811 ErrorMsg.error ("Rooted module " ^ full ^ " has multiple versions.")
adamc@1092 812 else
adamc@1092 813 ();
adamc@1146 814
adamc@1090 815 makeD "" pieces
adamc@1146 816 before ignore (foldl (fn (new, path) =>
adamc@1146 817 let
adamc@1146 818 val new' = case path of
adamc@1146 819 "" => new
adamc@1146 820 | _ => path ^ "." ^ new
adamc@1146 821 in
adamc@1146 822 fulls := SS.add (!fulls, new');
adamc@1146 823 new'
adamc@1146 824 end) "" pieces)
adamc@1090 825 end
adamc@201 826 in
adamc@834 827 checkErrors ();
adamc@1090 828 d
adamc@201 829 end
adamc@56 830
adamc@764 831 val dsFfi = map parseFfi ffi
adamc@201 832 val ds = map parseOne fnames
adamc@1090 833 val loc = ErrorMsg.dummySpan
adamc@201 834 in
adamc@834 835 if !anyErrors then
adamc@834 836 ErrorMsg.error "Parse failure"
adamc@834 837 else
adamc@834 838 ();
adamc@834 839
adamc@201 840 let
adamc@1126 841 val final = List.last fnames
adamc@1126 842 val final = case List.find (fn (root, name) =>
adamc@1126 843 String.isPrefix (root ^ "/") final) mrs of
adamc@1126 844 NONE => (Source.StrVar (nameOf final), loc)
adamc@1126 845 | SOME (root, name) =>
adamc@1126 846 let
adamc@1126 847 val m = (Source.StrVar name, loc)
adamc@1126 848 val final = String.extract (final, size root + 1, NONE)
adamc@1126 849 in
adamc@1126 850 foldl (fn (x, m) => (Source.StrProj (m, capitalize x), loc))
adamc@1126 851 m (String.fields (fn ch => ch = #"/") final)
adamc@1126 852 end
adamc@271 853
adamc@764 854 val ds = dsFfi @ ds
adamc@1126 855 @ [(Source.DExport final, loc)]
adamc@1090 856
adamc@1090 857 val ds = case database of
adamc@1090 858 NONE => ds
adamc@1090 859 | SOME s => (Source.DDatabase s, loc) :: ds
adamc@201 860 in
adamc@1090 861 ds
adamc@201 862 end handle Empty => ds
adamc@201 863 end,
adamc@201 864 print = SourcePrint.p_file
adamc@201 865 }
adamc@56 866
adamc@270 867 val toParse = transform parse "parse" o toParseJob
adamc@38 868
adamc@378 869 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
adamc@378 870 file = s}
adamc@378 871 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@378 872 file = s}
adamc@378 873
adamc@201 874 val elaborate = {
adamc@201 875 func = fn file => let
adamc@378 876 val basis = #func parseUrs (libFile "basis.urs")
adamc@378 877 val topSgn = #func parseUrs (libFile "top.urs")
adamc@378 878 val topStr = #func parseUr (libFile "top.ur")
adamc@201 879 in
adamc@325 880 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
adamc@201 881 end,
adamc@201 882 print = ElabPrint.p_file ElabEnv.empty
adamc@201 883 }
adamc@5 884
adamc@270 885 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 886
adamc@448 887 val unnest = {
adamc@448 888 func = Unnest.unnest,
adamc@448 889 print = ElabPrint.p_file ElabEnv.empty
adamc@448 890 }
adamc@448 891
adamc@448 892 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 893
adamc@313 894 val termination = {
adamc@313 895 func = (fn file => (Termination.check file; file)),
adamc@313 896 print = ElabPrint.p_file ElabEnv.empty
adamc@313 897 }
adamc@313 898
adamc@448 899 val toTermination = transform termination "termination" o toUnnest
adamc@313 900
adamc@201 901 val explify = {
adamc@201 902 func = Explify.explify,
adamc@201 903 print = ExplPrint.p_file ExplEnv.empty
adamc@201 904 }
adamc@201 905
adamc@625 906 val toExplify = transform explify "explify" o toUnnest
adamc@201 907
adamc@201 908 val corify = {
adamc@201 909 func = Corify.corify,
adamc@201 910 print = CorePrint.p_file CoreEnv.empty
adamc@201 911 }
adamc@201 912
adamc@270 913 val toCorify = transform corify "corify" o toExplify
adamc@201 914
adamc@482 915 (*val reduce_local = {
adamc@482 916 func = ReduceLocal.reduce,
adamc@482 917 print = CorePrint.p_file CoreEnv.empty
adamc@482 918 }
adamc@482 919
adamc@482 920 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 921
adamc@443 922 val especialize = {
adamc@443 923 func = ESpecialize.specialize,
adamc@443 924 print = CorePrint.p_file CoreEnv.empty
adamc@443 925 }
adamc@443 926
adamc@454 927 val core_untangle = {
adamc@454 928 func = CoreUntangle.untangle,
adamc@454 929 print = CorePrint.p_file CoreEnv.empty
adamc@454 930 }
adamc@454 931
adamc@794 932 val toCore_untangle = transform core_untangle "core_untangle" o toCorify
adamc@454 933
adamc@202 934 val shake = {
adamc@202 935 func = Shake.shake,
adamc@202 936 print = CorePrint.p_file CoreEnv.empty
adamc@202 937 }
adamc@39 938
adamc@454 939 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 940
adamc@607 941 val rpcify = {
adamc@607 942 func = Rpcify.frob,
adamc@607 943 print = CorePrint.p_file CoreEnv.empty
adamc@607 944 }
adamc@607 945
adamc@607 946 val toRpcify = transform rpcify "rpcify" o toShake1
adamc@607 947
adamc@642 948 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 949 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@1062 950 val toEspecialize1 = transform especialize "especialize1" o toShake2
adamc@1062 951 val toCore_untangle3 = transform core_untangle "core_untangle3" o toEspecialize1
adamc@1062 952 val toShake3 = transform shake "shake3" o toCore_untangle3
adamc@642 953
adamc@202 954 val tag = {
adamc@202 955 func = Tag.tag,
adamc@202 956 print = CorePrint.p_file CoreEnv.empty
adamc@202 957 }
adamc@193 958
adamc@1062 959 val toTag = transform tag "tag" o toShake3
adamc@20 960
adamc@202 961 val reduce = {
adamc@202 962 func = Reduce.reduce,
adamc@202 963 print = CorePrint.p_file CoreEnv.empty
adamc@202 964 }
adamc@25 965
adamc@692 966 val toReduce = transform reduce "reduce" o toTag
adamc@23 967
adamc@315 968 val unpoly = {
adamc@315 969 func = Unpoly.unpoly,
adamc@315 970 print = CorePrint.p_file CoreEnv.empty
adamc@315 971 }
adamc@315 972
adamc@315 973 val toUnpoly = transform unpoly "unpoly" o toReduce
adamc@315 974
adamc@202 975 val specialize = {
adamc@202 976 func = Specialize.specialize,
adamc@202 977 print = CorePrint.p_file CoreEnv.empty
adamc@202 978 }
adamc@132 979
adamc@315 980 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 981
adamc@1062 982 val toShake4 = transform shake "shake4" o toSpecialize
adamc@133 983
adamc@1062 984 val toEspecialize2 = transform especialize "especialize2" o toShake4
adamc@794 985
adamc@1062 986 val toReduce2 = transform reduce "reduce2" o toEspecialize2
adamc@898 987
adamc@1062 988 val toShake5 = transform shake "shake5" o toReduce2
adamc@794 989
adamc@692 990 val marshalcheck = {
adamc@692 991 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 992 print = CorePrint.p_file CoreEnv.empty
adamc@692 993 }
adamc@692 994
adamc@1062 995 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake5
adamc@692 996
adamc@732 997 val effectize = {
adamc@732 998 func = Effective.effectize,
adamc@732 999 print = CorePrint.p_file CoreEnv.empty
adamc@732 1000 }
adamc@732 1001
adamc@732 1002 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 1003
adamc@1170 1004 val css = {
adamc@1170 1005 func = Css.summarize,
adamc@1170 1006 print = fn _ => Print.box []
adamc@1170 1007 }
adamc@1170 1008
adamc@1170 1009 val toCss = transform css "css" o toShake5
adamc@1170 1010
adamc@202 1011 val monoize = {
adamc@202 1012 func = Monoize.monoize CoreEnv.empty,
adamc@202 1013 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1014 }
adamc@134 1015
adamc@732 1016 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 1017
adamc@202 1018 val mono_opt = {
adamc@910 1019 func = MonoOpt.optimize,
adamc@202 1020 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1021 }
adamc@29 1022
adamc@270 1023 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 1024
adamc@202 1025 val untangle = {
adamc@202 1026 func = Untangle.untangle,
adamc@202 1027 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1028 }
adamc@1 1029
adamc@270 1030 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 1031
adamc@202 1032 val mono_reduce = {
adamc@202 1033 func = MonoReduce.reduce,
adamc@202 1034 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1035 }
adamc@16 1036
adamc@270 1037 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 1038
adamc@202 1039 val mono_shake = {
adamc@202 1040 func = MonoShake.shake,
adamc@202 1041 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1042 }
adamc@110 1043
adamc@270 1044 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 1045
adamc@572 1046 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 1047
adamc@567 1048 val jscomp = {
adamc@567 1049 func = JsComp.process,
adamc@567 1050 print = MonoPrint.p_file MonoEnv.empty
adamc@567 1051 }
adamc@567 1052
adamc@572 1053 val toJscomp = transform jscomp "jscomp" o toMono_opt2
adamc@567 1054
adamc@910 1055 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 1056
adamc@506 1057 val fuse = {
adamc@506 1058 func = Fuse.fuse,
adamc@506 1059 print = MonoPrint.p_file MonoEnv.empty
adamc@506 1060 }
adamc@506 1061
adamc@572 1062 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 1063
adamc@506 1064 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 1065
adamc@601 1066 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 1067 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@916 1068 val toMono_opt4 = transform mono_opt "mono_opt4" o toMono_shake2
adamc@1017 1069 val toMono_reduce3 = transform mono_reduce "mono_reduce3" o toMono_opt4
adamc@1017 1070 val toFuse2 = transform fuse "shake2" o toMono_reduce3
adamc@916 1071 val toUntangle3 = transform untangle "untangle3" o toFuse2
adamc@916 1072 val toMono_shake3 = transform mono_shake "mono_shake3" o toUntangle3
adamc@506 1073
adamc@377 1074 val pathcheck = {
adamc@377 1075 func = (fn file => (PathCheck.check file; file)),
adamc@377 1076 print = MonoPrint.p_file MonoEnv.empty
adamc@377 1077 }
adamc@377 1078
adamc@916 1079 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake3
adamc@377 1080
adamc@202 1081 val cjrize = {
adamc@202 1082 func = Cjrize.cjrize,
adamc@202 1083 print = CjrPrint.p_file CjrEnv.empty
adamc@202 1084 }
adamc@23 1085
adamc@377 1086 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 1087
adamc@643 1088 val scriptcheck = {
adamc@643 1089 func = ScriptCheck.classify,
adamc@643 1090 print = CjrPrint.p_file CjrEnv.empty
adamc@643 1091 }
adamc@643 1092
adamc@643 1093 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 1094
adamc@282 1095 val prepare = {
adamc@282 1096 func = Prepare.prepare,
adamc@282 1097 print = CjrPrint.p_file CjrEnv.empty
adamc@282 1098 }
adamc@282 1099
adamc@643 1100 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 1101
adamc@879 1102 val checknest = {
adamc@879 1103 func = fn f => if #supportsNestedPrepared (Settings.currentDbms ()) then f else Checknest.annotate f,
adamc@879 1104 print = CjrPrint.p_file CjrEnv.empty
adamc@879 1105 }
adamc@879 1106
adamc@879 1107 val toChecknest = transform checknest "checknest" o toPrepare
adamc@879 1108
adamc@274 1109 val sqlify = {
adamc@274 1110 func = Cjrize.cjrize,
adamc@274 1111 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 1112 }
adamc@274 1113
adamc@274 1114 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 1115
adamc@764 1116 fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
adamc@183 1117 let
adamc@855 1118 val proto = Settings.currentProtocol ()
adamc@1095 1119
adamc@1095 1120 val lib = if Settings.getStaticLinking () then
adamc@1132 1121 #linkStatic proto ^ " " ^ Config.lib ^ "/../liburweb.a"
adamc@1095 1122 else
adamc@1106 1123 "-L" ^ Config.lib ^ "/.. -lurweb " ^ #linkDynamic proto
adamc@378 1124
adamc@1094 1125 val compile = "gcc " ^ Config.gccArgs ^ " -Wimplicit -Werror -O3 -fno-inline -I " ^ Config.includ
adamc@1096 1126 ^ " " ^ #compile proto
adamc@832 1127 ^ " -c " ^ cname ^ " -o " ^ oname
adamc@1096 1128
adamc@1095 1129 val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ Config.gccArgs ^ " " ^ libs ^ " " ^ lib ^ " " ^ oname
adamc@1095 1130 ^ " -o " ^ ename
adamc@502 1131
adamc@502 1132 val (compile, link) =
adamc@502 1133 if profile then
adamc@502 1134 (compile ^ " -pg", link ^ " -pg")
adamc@502 1135 else
adamc@502 1136 (compile, link)
adamc@742 1137
adamc@742 1138 val (compile, link) =
adamc@742 1139 if debug then
adamc@742 1140 (compile ^ " -g", link ^ " -g")
adamc@742 1141 else
adamc@742 1142 (compile, link)
adamc@764 1143
adamc@764 1144 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adamc@183 1145 in
adamc@1045 1146 OS.Process.isSuccess (OS.Process.system compile)
adamc@1045 1147 andalso OS.Process.isSuccess (OS.Process.system link)
adamc@183 1148 end
adamc@183 1149
adamc@202 1150 fun compile job =
adamc@879 1151 case run toChecknest job of
adamc@1045 1152 NONE => false
adamc@29 1153 | SOME file =>
adamc@202 1154 let
adamc@274 1155 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 1156
adamc@274 1157 val (cname, oname, cleanup) =
adamc@274 1158 if #debug job then
adamc@457 1159 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 1160 else
adamc@274 1161 let
adamc@274 1162 val dir = OS.FileSys.tmpName ()
adamc@403 1163 val () = if OS.FileSys.access (dir, []) then
adamc@403 1164 OS.FileSys.remove dir
adamc@403 1165 else
adamc@403 1166 ()
adamc@457 1167 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 1168 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 1169 in
adamc@274 1170 OS.FileSys.mkDir dir;
adamc@274 1171 (cname, oname,
adamc@274 1172 fn () => (OS.FileSys.remove cname;
adamc@274 1173 OS.FileSys.remove oname;
adamc@473 1174 OS.FileSys.rmDir dir)
adamc@473 1175 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 1176 end
adamc@274 1177 val ename = #exe job
adamc@202 1178 in
adamc@274 1179 let
adamc@274 1180 val outf = TextIO.openOut cname
adamc@274 1181 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 1182
adamc@432 1183 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 1184 val libs =
adamc@432 1185 if hasDb then
adamc@866 1186 #link (Settings.currentDbms ())
adamc@432 1187 else
adamc@432 1188 ""
adamc@274 1189 in
adamc@274 1190 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 1191 TextIO.output1 (outf, #"\n");
adamc@274 1192 TextIO.closeOut outf;
adamc@102 1193
adamc@274 1194 case #sql job of
adamc@274 1195 NONE => ()
adamc@274 1196 | SOME sql =>
adamc@274 1197 let
adamc@274 1198 val outf = TextIO.openOut sql
adamc@274 1199 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 1200 in
adamc@274 1201 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 1202 TextIO.closeOut outf
adamc@274 1203 end;
adamc@274 1204
adamc@742 1205 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adamc@1045 1206 profile = #profile job, debug = #debug job, link = #link job}
adamc@274 1207
adamc@1045 1208 before cleanup ()
adamc@274 1209 end
adamc@274 1210 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 1211 end
adamc@29 1212
adamc@1045 1213 fun compiler job =
adamc@1045 1214 if compile job then
adamc@1045 1215 ()
adamc@1045 1216 else
adamc@1045 1217 OS.Process.exit OS.Process.failure
adamc@1045 1218
adamc@1 1219 end