annotate src/compiler.sml @ 1328:c5799b1e4c58

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