annotate src/compiler.sml @ 1745:518e0b23c4ef

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