annotate src/compiler.sml @ 1171:7a2a7a8f9cab

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