annotate src/compiler.sml @ 1294:b4480a56cab7

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