annotate src/compiler.sml @ 1768:a613cae954ca

Some standard library additions from Edward Z. Yang
author Adam Chlipala <adam@chlipala.net>
date Sat, 19 May 2012 11:32:12 -0400
parents 675ce534e3ec
children b5f5e8d439c7
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@1752 429 from = capitalize (OS.Path.file fname) ^ "/", to = "",
adam@1752 430 hyphenate = false}],
adam@1296 431 filterUrl = [],
adam@1296 432 filterMime = [],
adam@1465 433 filterRequest = [],
adam@1465 434 filterResponse = [],
adam@1296 435 protocol = NONE,
adam@1296 436 dbms = NONE,
adam@1296 437 sigFile = NONE,
adam@1296 438 safeGets = [],
adam@1332 439 onError = NONE,
adam@1332 440 minHeap = 0}
adam@1296 441 in
adam@1296 442 institutionalizeJob job;
adam@1296 443 {Job = job, Libs = []}
adam@1296 444 end
adam@1296 445 else
adam@1296 446 let
adam@1296 447 val pathmap = ref (!pathmap)
adam@1296 448 val bigLibs = ref []
adamc@767 449
adam@1296 450 fun pu filename =
adam@1296 451 let
adam@1338 452 val filename = OS.Path.mkAbsolute {path = filename, relativeTo = OS.FileSys.getDir ()}
adam@1338 453
adam@1296 454 val dir = OS.Path.dir filename
adam@1296 455 fun opener () = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@1151 456
adam@1296 457 val inf = opener ()
adamc@1151 458
adam@1296 459 fun hasSpaceLine () =
adam@1331 460 case inputCommentableLine inf of
adam@1603 461 Content s => s = "debug" orelse s = "profile"
adam@1603 462 orelse CharVector.exists (fn ch => ch = #" " orelse ch = #"\t") s orelse hasSpaceLine ()
adam@1605 463 | EndOfFile => false
adam@1605 464 | OnlyComment => hasSpaceLine ()
adamc@1151 465
adam@1296 466 val hasBlankLine = hasSpaceLine ()
adamc@1151 467
adam@1296 468 val inf = (TextIO.closeIn inf; opener ())
adamc@767 469
adam@1296 470 fun pathify fname =
adam@1296 471 if size fname > 0 andalso String.sub (fname, 0) = #"$" then
adam@1296 472 let
adam@1296 473 val fname' = Substring.extract (fname, 1, NONE)
adam@1296 474 val (befor, after) = Substring.splitl (fn ch => ch <> #"/") fname'
adam@1296 475 in
adam@1340 476 case M.find (!pathmap, Substring.string befor) of
adam@1340 477 NONE => fname
adam@1340 478 | SOME rep => rep ^ Substring.string after
adam@1296 479 end
adam@1296 480 else
adam@1296 481 fname
adam@1296 482
adam@1296 483 fun relify fname =
adamc@794 484 let
adam@1296 485 val fname = pathify fname
adamc@794 486 in
adam@1296 487 OS.Path.concat (dir, fname)
adam@1296 488 handle OS.Path.Path => fname
adamc@794 489 end
adamc@767 490
adam@1296 491 fun libify path =
adam@1296 492 (if Posix.FileSys.access (path ^ ".urp", []) then
adam@1296 493 path
adam@1296 494 else
adam@1296 495 path ^ "/lib")
adam@1296 496 handle SysErr => path
adamc@767 497
adam@1296 498 fun libify' path =
adam@1296 499 (if Posix.FileSys.access (relify path ^ ".urp", []) then
adam@1296 500 path
adam@1296 501 else
adam@1296 502 path ^ "/lib")
adam@1296 503 handle SysErr => path
adamc@767 504
adam@1296 505 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@794 506
adam@1296 507 fun relifyA fname =
adam@1296 508 OS.Path.mkAbsolute {path = pathify fname, relativeTo = absDir}
adamc@794 509
adam@1296 510 fun readSources acc =
adam@1331 511 case inputCommentableLine inf of
adam@1603 512 Content line =>
adam@1296 513 let
adam@1296 514 val acc = if CharVector.all Char.isSpace line then
adam@1296 515 acc
adam@1296 516 else
adam@1296 517 let
adam@1296 518 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adam@1296 519 (String.explode line))
adam@1296 520 val fname = relifyA fname
adam@1296 521 in
adam@1296 522 fname :: acc
adam@1296 523 end
adam@1296 524 in
adam@1296 525 readSources acc
adam@1296 526 end
adam@1606 527 | OnlyComment => readSources acc
adam@1606 528 | EndOfFile => rev acc
adamc@794 529
adam@1637 530 val prefix = ref (case Settings.getUrlPrefixFull () of "/" => NONE | s => SOME s)
adam@1296 531 val database = ref (Settings.getDbstring ())
adam@1296 532 val exe = ref (Settings.getExe ())
adam@1296 533 val sql = ref (Settings.getSql ())
adam@1296 534 val debug = ref (Settings.getDebug ())
adam@1296 535 val profile = ref false
adam@1296 536 val timeout = ref NONE
adam@1296 537 val ffi = ref []
adam@1296 538 val link = ref []
adam@1725 539 val linker = ref NONE
adam@1296 540 val headers = ref []
adam@1296 541 val scripts = ref []
adam@1296 542 val clientToServer = ref []
adam@1296 543 val effectful = ref []
adam@1296 544 val benignEffectful = ref []
adam@1296 545 val clientOnly = ref []
adam@1296 546 val serverOnly = ref []
adam@1296 547 val jsFuncs = ref []
adam@1296 548 val rewrites = ref []
adam@1296 549 val url = ref []
adam@1296 550 val mime = ref []
adam@1465 551 val request = ref []
adam@1465 552 val response = ref []
adam@1296 553 val libs = ref []
adam@1296 554 val protocol = ref NONE
adam@1296 555 val dbms = ref NONE
adam@1296 556 val sigFile = ref (Settings.getSigFile ())
adam@1296 557 val safeGets = ref []
adam@1296 558 val onError = ref NONE
adam@1332 559 val minHeap = ref 0
adamc@794 560
adam@1296 561 fun finish sources =
adam@1296 562 let
adam@1296 563 val job = {
adam@1296 564 prefix = Option.getOpt (!prefix, "/"),
adam@1296 565 database = !database,
adam@1296 566 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adam@1296 567 ext = SOME "exe"}),
adam@1296 568 sql = !sql,
adam@1296 569 debug = !debug,
adam@1296 570 profile = !profile,
adam@1296 571 timeout = Option.getOpt (!timeout, 60),
adam@1296 572 ffi = rev (!ffi),
adam@1296 573 link = rev (!link),
adam@1725 574 linker = !linker,
adam@1296 575 headers = rev (!headers),
adam@1296 576 scripts = rev (!scripts),
adam@1296 577 clientToServer = rev (!clientToServer),
adam@1296 578 effectful = rev (!effectful),
adam@1296 579 benignEffectful = rev (!benignEffectful),
adam@1296 580 clientOnly = rev (!clientOnly),
adam@1296 581 serverOnly = rev (!serverOnly),
adam@1296 582 jsFuncs = rev (!jsFuncs),
adam@1296 583 rewrites = rev (!rewrites),
adam@1296 584 filterUrl = rev (!url),
adam@1296 585 filterMime = rev (!mime),
adam@1465 586 filterRequest = rev (!request),
adam@1465 587 filterResponse = rev (!response),
adam@1296 588 sources = sources,
adam@1296 589 protocol = !protocol,
adam@1296 590 dbms = !dbms,
adam@1296 591 sigFile = !sigFile,
adam@1296 592 safeGets = rev (!safeGets),
adam@1332 593 onError = !onError,
adam@1332 594 minHeap = !minHeap
adam@1296 595 }
adamc@794 596
adam@1296 597 fun mergeO f (old, new) =
adam@1296 598 case (old, new) of
adam@1296 599 (NONE, _) => new
adam@1296 600 | (_, NONE) => old
adam@1296 601 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adamc@794 602
adam@1296 603 fun same desc = mergeO (fn (x : string, y) =>
adam@1296 604 (if x = y then
adam@1296 605 ()
adam@1296 606 else
adam@1296 607 ErrorMsg.error ("Multiple "
adam@1296 608 ^ desc ^ " values that don't agree");
adam@1296 609 x))
adamc@794 610
adam@1296 611 fun merge (old : job, new : job) = {
adam@1637 612 prefix = case #prefix old of
adam@1637 613 "/" => #prefix new
adam@1637 614 | pold => case #prefix new of
adam@1637 615 "/" => pold
adam@1637 616 | pnew => (if pold = pnew then
adam@1637 617 ()
adam@1637 618 else
adam@1637 619 ErrorMsg.error ("Multiple prefix values that don't agree: "
adam@1637 620 ^ pold ^ ", " ^ pnew);
adam@1637 621 pold),
adam@1296 622 database = mergeO (fn (old, _) => old) (#database old, #database new),
adam@1296 623 exe = #exe old,
adam@1296 624 sql = #sql old,
adam@1296 625 debug = #debug old orelse #debug new,
adam@1296 626 profile = #profile old orelse #profile new,
adam@1296 627 timeout = #timeout old,
adam@1296 628 ffi = #ffi old @ #ffi new,
adam@1296 629 link = #link old @ #link new,
adam@1725 630 linker = mergeO (fn (_, new) => new) (#linker old, #linker new),
adam@1296 631 headers = #headers old @ #headers new,
adam@1296 632 scripts = #scripts old @ #scripts new,
adam@1296 633 clientToServer = #clientToServer old @ #clientToServer new,
adam@1296 634 effectful = #effectful old @ #effectful new,
adam@1296 635 benignEffectful = #benignEffectful old @ #benignEffectful new,
adam@1296 636 clientOnly = #clientOnly old @ #clientOnly new,
adam@1296 637 serverOnly = #serverOnly old @ #serverOnly new,
adam@1296 638 jsFuncs = #jsFuncs old @ #jsFuncs new,
adam@1296 639 rewrites = #rewrites old @ #rewrites new,
adam@1296 640 filterUrl = #filterUrl old @ #filterUrl new,
adam@1296 641 filterMime = #filterMime old @ #filterMime new,
adam@1465 642 filterRequest = #filterRequest old @ #filterRequest new,
adam@1465 643 filterResponse = #filterResponse old @ #filterResponse new,
adam@1296 644 sources = #sources new
adam@1296 645 @ List.filter (fn s => List.all (fn s' => s' <> s) (#sources new))
adam@1296 646 (#sources old),
adam@1296 647 protocol = mergeO #2 (#protocol old, #protocol new),
adam@1296 648 dbms = mergeO #2 (#dbms old, #dbms new),
adam@1296 649 sigFile = mergeO #2 (#sigFile old, #sigFile new),
adam@1296 650 safeGets = #safeGets old @ #safeGets new,
adam@1332 651 onError = mergeO #2 (#onError old, #onError new),
adam@1332 652 minHeap = Int.max (#minHeap old, #minHeap new)
adam@1296 653 }
adamc@794 654 in
adam@1296 655 if accLibs then
adam@1296 656 foldr (fn (job', job) => merge (job, job')) job (!libs)
adam@1296 657 else
adam@1296 658 job
adamc@794 659 end
adamc@794 660
adam@1296 661 fun parsePkind s =
adam@1296 662 case s of
adam@1296 663 "all" => Settings.Any
adam@1296 664 | "url" => Settings.Url
adam@1296 665 | "table" => Settings.Table
adam@1296 666 | "sequence" => Settings.Sequence
adam@1296 667 | "view" => Settings.View
adam@1296 668 | "relation" => Settings.Relation
adam@1296 669 | "cookie" => Settings.Cookie
adam@1296 670 | "style" => Settings.Style
adam@1296 671 | _ => (ErrorMsg.error "Bad path kind spec";
adam@1296 672 Settings.Any)
adam@1296 673
adam@1296 674 fun parseFrom s =
adam@1296 675 if size s > 1 andalso String.sub (s, size s - 2) = #"/" andalso String.sub (s, size s - 1) = #"*" then
adam@1296 676 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 677 else
adam@1296 678 (Settings.Exact, s)
adam@1296 679
adam@1296 680 fun parseFkind s =
adam@1296 681 case s of
adam@1296 682 "url" => url
adam@1296 683 | "mime" => mime
adam@1465 684 | "requestHeader" => request
adam@1465 685 | "responseHeader" => response
adam@1296 686 | _ => (ErrorMsg.error "Bad filter kind";
adam@1296 687 url)
adam@1296 688
adam@1296 689 fun parsePattern s =
adam@1296 690 if size s > 0 andalso String.sub (s, size s - 1) = #"*" then
adam@1296 691 (Settings.Prefix, String.substring (s, 0, size s - 1))
adam@1296 692 else
adam@1296 693 (Settings.Exact, s)
adam@1296 694
adam@1296 695 fun read () =
adam@1331 696 case inputCommentableLine inf of
adam@1603 697 EndOfFile => finish []
adam@1603 698 | OnlyComment => read ()
adam@1603 699 | Content "" => finish (readSources [])
adam@1603 700 | Content line =>
adam@1296 701 let
adam@1296 702 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adam@1296 703 val cmd = Substring.string (trim cmd)
adam@1296 704 val arg = Substring.string (trim arg)
adam@1296 705
adam@1296 706 fun ffiS () =
adam@1296 707 case String.fields (fn ch => ch = #".") arg of
adam@1296 708 [m, x] => (m, x)
adam@1296 709 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adam@1296 710 ("", ""))
adam@1296 711
adam@1296 712 fun ffiM () =
adam@1296 713 case String.fields (fn ch => ch = #"=") arg of
adam@1296 714 [f, s] =>
adam@1433 715 let
adam@1433 716 val f = trimS f
adam@1433 717 val s = trimS s
adam@1433 718 in
adam@1433 719 case String.fields (fn ch => ch = #".") f of
adam@1433 720 [m, x] => ((m, x), s)
adam@1433 721 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1433 722 (("", ""), ""))
adam@1433 723 end
adam@1296 724 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adam@1296 725 (("", ""), ""))
adam@1296 726 in
adam@1296 727 case cmd of
adam@1336 728 "prefix" => prefix := SOME arg
adam@1296 729 | "database" =>
adam@1296 730 (case !database of
adam@1296 731 NONE => database := SOME arg
adam@1296 732 | SOME _ => ())
adam@1296 733 | "dbms" =>
adam@1296 734 (case !dbms of
adam@1296 735 NONE => dbms := SOME arg
adam@1296 736 | SOME _ => ())
adam@1296 737 | "sigfile" =>
adam@1296 738 (case !sigFile of
adam@1296 739 NONE => sigFile := SOME arg
adam@1296 740 | SOME _ => ())
adam@1296 741 | "exe" =>
adam@1296 742 (case !exe of
adam@1296 743 NONE => exe := SOME (relify arg)
adam@1296 744 | SOME _ => ())
adam@1296 745 | "sql" =>
adam@1296 746 (case !sql of
adam@1296 747 NONE => sql := SOME (relify arg)
adam@1296 748 | SOME _ => ())
adam@1296 749 | "debug" => debug := true
adam@1296 750 | "profile" => profile := true
adam@1296 751 | "timeout" =>
adam@1296 752 (case !timeout of
adam@1296 753 NONE => ()
adam@1296 754 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adam@1296 755 timeout := SOME (valOf (Int.fromString arg)))
adam@1296 756 | "ffi" => ffi := relify arg :: !ffi
adam@1296 757 | "link" => let
adam@1296 758 val arg = if size arg >= 1
adam@1296 759 andalso String.sub (arg, 0) = #"-" then
adam@1296 760 arg
adam@1296 761 else
adam@1296 762 relifyA arg
adam@1296 763 in
adam@1296 764 link := arg :: !link
adam@1296 765 end
adam@1725 766 | "linker" => linker := SOME arg
adam@1296 767 | "include" => headers := relifyA arg :: !headers
adam@1296 768 | "script" => scripts := arg :: !scripts
adam@1296 769 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adam@1296 770 | "safeGet" => safeGets := arg :: !safeGets
adam@1296 771 | "effectful" => effectful := ffiS () :: !effectful
adam@1296 772 | "benignEffectful" => benignEffectful := ffiS () :: !benignEffectful
adam@1296 773 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adam@1296 774 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adam@1296 775 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adam@1296 776 | "rewrite" =>
adam@1296 777 let
adam@1752 778 fun doit (pkind, from, to, hyph) =
adam@1296 779 let
adam@1296 780 val pkind = parsePkind pkind
adam@1296 781 val (kind, from) = parseFrom from
adam@1296 782 in
adam@1752 783 rewrites := {pkind = pkind, kind = kind, from = from, to = to, hyphenate = hyph} :: !rewrites
adam@1296 784 end
adam@1296 785 in
adam@1296 786 case String.tokens Char.isSpace arg of
adam@1752 787 [pkind, from, to, "[-]"] => doit (pkind, from, to, true)
adam@1752 788 | [pkind, from, "[-]"] => doit (pkind, from, "", true)
adam@1752 789 | [pkind, from, to] => doit (pkind, from, to, false)
adam@1752 790 | [pkind, from] => doit (pkind, from, "", false)
adam@1296 791 | _ => ErrorMsg.error "Bad 'rewrite' syntax"
adam@1296 792 end
adam@1296 793 | "allow" =>
adam@1296 794 (case String.tokens Char.isSpace arg of
adam@1296 795 [fkind, pattern] =>
adam@1296 796 let
adam@1296 797 val fkind = parseFkind fkind
adam@1296 798 val (kind, pattern) = parsePattern pattern
adam@1296 799 in
adam@1296 800 fkind := {action = Settings.Allow, kind = kind, pattern = pattern} :: !fkind
adam@1296 801 end
adam@1296 802 | _ => ErrorMsg.error "Bad 'allow' syntax")
adam@1296 803 | "deny" =>
adam@1296 804 (case String.tokens Char.isSpace arg of
adam@1296 805 [fkind, pattern] =>
adam@1296 806 let
adam@1296 807 val fkind = parseFkind fkind
adam@1296 808 val (kind, pattern) = parsePattern pattern
adam@1296 809 in
adam@1296 810 fkind := {action = Settings.Deny, kind = kind, pattern = pattern} :: !fkind
adam@1296 811 end
adam@1296 812 | _ => ErrorMsg.error "Bad 'deny' syntax")
adam@1296 813 | "library" => if accLibs then
adam@1296 814 libs := pu (libify (relify arg)) :: !libs
adam@1296 815 else
adam@1296 816 bigLibs := libify' arg :: !bigLibs
adam@1296 817 | "path" =>
adam@1296 818 (case String.fields (fn ch => ch = #"=") arg of
adam@1338 819 [n, v] => ((pathmap := M.insert (!pathmap, n, OS.Path.mkAbsolute {path = v, relativeTo = dir}))
adam@1338 820 handle OS.Path.Path => ErrorMsg.error "Invalid 'path' directory argument")
adam@1296 821 | _ => ErrorMsg.error "path argument not of the form name=value'")
adam@1296 822 | "onError" =>
adam@1296 823 (case String.fields (fn ch => ch = #".") arg of
adam@1296 824 m1 :: (fs as _ :: _) =>
adam@1296 825 onError := SOME (m1, List.take (fs, length fs - 1), List.last fs)
adam@1296 826 | _ => ErrorMsg.error "invalid 'onError' argument")
adam@1307 827 | "limit" =>
adam@1307 828 (case String.fields Char.isSpace arg of
adam@1307 829 [class, num] =>
adam@1307 830 (case Int.fromString num of
adam@1307 831 NONE => ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1307 832 | SOME n =>
adam@1307 833 if n < 0 then
adam@1307 834 ErrorMsg.error ("invalid limit number '" ^ num ^ "'")
adam@1307 835 else
adam@1307 836 Settings.addLimit (class, n))
adam@1307 837 | _ => ErrorMsg.error "invalid 'limit' arguments")
adam@1332 838 | "minHeap" =>
adam@1332 839 (case Int.fromString arg of
adam@1332 840 NONE => ErrorMsg.error ("invalid min heap '" ^ arg ^ "'")
adam@1332 841 | SOME n => minHeap := n)
adam@1393 842 | "alwaysInline" => Settings.addAlwaysInline arg
adam@1478 843 | "noXsrfProtection" => Settings.addNoXsrfProtection arg
adam@1629 844 | "timeFormat" => Settings.setTimeFormat arg
adam@1296 845
adam@1296 846 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adam@1296 847 read ()
adam@1296 848 end
adam@1296 849
adam@1296 850 val job = if hasBlankLine then
adam@1296 851 read ()
adam@1296 852 else
adam@1296 853 finish (readSources [])
adam@1296 854 in
adam@1296 855 TextIO.closeIn inf;
adam@1296 856 institutionalizeJob job;
adam@1296 857 job
adam@1296 858 end
adam@1296 859 in
adam@1296 860 {Job = pu fname, Libs = !bigLibs}
adam@1296 861 end
adamc@767 862
adamc@1082 863 fun p_job' {Job = j, Libs = _ : string list} = p_job j
adamc@1082 864
adamc@270 865 val parseUrp = {
adamc@1083 866 func = #Job o parseUrp' true,
adamc@270 867 print = p_job
adamc@270 868 }
adamc@270 869
adamc@1082 870 val parseUrp' = {
adamc@1083 871 func = parseUrp' false,
adamc@1082 872 print = p_job'
adamc@1082 873 }
adamc@1082 874
adamc@270 875 val toParseJob = transform parseUrp "parseJob"
adamc@1082 876 val toParseJob' = transform parseUrp' "parseJob'"
adamc@1082 877
adamc@1082 878 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@1082 879 func = fn input => case #func tr1 input of
adamc@1082 880 NONE => NONE
adamc@1082 881 | SOME v => #func tr2 v,
adamc@1082 882 print = #print tr2,
adamc@1082 883 time = fn (input, pmap) => let
adamc@1082 884 val (ro, pmap) = #time tr1 (input, pmap)
adamc@1082 885 in
adamc@1082 886 case ro of
adamc@1082 887 NONE => (NONE, pmap)
adamc@1082 888 | SOME v => #time tr2 (v, pmap)
adamc@1082 889 end
adamc@1082 890 }
adamc@270 891
adamc@1090 892 structure SM = BinaryMapFn(struct
adamc@1090 893 type ord_key = string
adamc@1090 894 val compare = String.compare
adamc@1090 895 end)
adamc@1090 896
adamc@1090 897 val moduleRoots = ref ([] : (string * string) list)
adamc@1090 898 fun addModuleRoot (k, v) = moduleRoots := (k, v) :: !moduleRoots
adamc@1090 899
adam@1743 900 structure SK = struct
adam@1743 901 type ord_key = string
adam@1743 902 val compare = String.compare
adam@1743 903 end
adam@1743 904
adam@1743 905 structure SS = BinarySetFn(SK)
adam@1743 906 structure SM = BinaryMapFn(SK)
adamc@1090 907
adamc@201 908 val parse = {
adam@1294 909 func = fn {database, sources = fnames, ffi, onError, ...} : job =>
adamc@201 910 let
adamc@1090 911 val mrs = !moduleRoots
adamc@1090 912
adamc@834 913 val anyErrors = ref false
adamc@834 914 fun checkErrors () = anyErrors := (!anyErrors orelse ErrorMsg.anyErrors ())
adam@1343 915 fun nameOf fname =
adam@1343 916 let
adam@1343 917 val fname = OS.Path.file fname
adam@1343 918 val fst = if size fname = 0 then #"!" else String.sub (fname, 0)
adam@1343 919 in
adam@1343 920 if not (Char.isAlpha fst) then
adam@1343 921 ErrorMsg.error ("Filename doesn't start with letter: " ^ fname)
adam@1343 922 else if CharVector.exists (fn ch => not (Char.isAlphaNum ch) andalso ch <> #"_") fname then
adam@1343 923 ErrorMsg.error ("Filename contains a character that isn't alphanumeric or underscore: " ^ fname)
adam@1343 924 else
adam@1343 925 ();
adam@1343 926 capitalize fname
adam@1343 927 end
adamc@109 928
adamc@764 929 fun parseFfi fname =
adamc@764 930 let
adamc@764 931 val mname = nameOf fname
adamc@764 932 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 933
adamc@764 934 val loc = {file = urs,
adamc@764 935 first = ErrorMsg.dummyPos,
adamc@764 936 last = ErrorMsg.dummyPos}
adamc@764 937
adamc@764 938 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 939 in
adamc@834 940 checkErrors ();
adam@1733 941 (Source.DFfiStr (mname, sgn, if !Elaborate.incremental then SOME (OS.FileSys.modTime urs) else NONE), loc)
adamc@764 942 end
adamc@764 943
adamc@1090 944 val defed = ref SS.empty
adamc@1092 945 val fulls = ref SS.empty
adamc@1090 946
adamc@201 947 fun parseOne fname =
adamc@201 948 let
adamc@201 949 val mname = nameOf fname
adamc@244 950 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 951 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 952
adamc@201 953 val sgnO =
adamc@244 954 if Posix.FileSys.access (urs, []) then
adamc@244 955 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 956 {file = urs,
adamc@201 957 first = ErrorMsg.dummyPos,
adamc@201 958 last = ErrorMsg.dummyPos})
adamc@834 959 before checkErrors ()
adamc@201 960 else
adamc@201 961 NONE
adamc@56 962
adamc@244 963 val loc = {file = ur,
adamc@201 964 first = ErrorMsg.dummyPos,
adamc@201 965 last = ErrorMsg.dummyPos}
adamc@56 966
adam@1738 967 val urt = OS.FileSys.modTime ur
adam@1738 968 val urst = (OS.FileSys.modTime urs) handle _ => urt
adam@1738 969
adamc@244 970 val ds = #func parseUr ur
adam@1738 971 val d = (Source.DStr (mname, sgnO, if !Elaborate.incremental then SOME (if Time.> (urt, urst) then urt else urst) else NONE,
adam@1733 972 (Source.StrConst ds, loc)), loc)
adamc@1090 973
adamc@1126 974 val fname = OS.Path.mkCanonical fname
adamc@1090 975 val d = case List.find (fn (root, name) =>
adamc@1090 976 String.isPrefix (root ^ "/") fname) mrs of
adamc@1090 977 NONE => d
adamc@1090 978 | SOME (root, name) =>
adamc@1090 979 let
adamc@1090 980 val fname = String.extract (fname, size root + 1, NONE)
adamc@1090 981 val pieces = name :: String.tokens (fn ch => ch = #"/") fname
adamc@1090 982 val pieces = List.filter (fn s => size s > 0
adamc@1090 983 andalso Char.isAlpha (String.sub (s, 0)))
adamc@1090 984 pieces
adamc@1090 985 val pieces = map capitalize pieces
adamc@1092 986 val full = String.concatWith "." pieces
adamc@1090 987
adamc@1090 988 fun makeD prefix pieces =
adamc@1090 989 case pieces of
adamc@1090 990 [] => (ErrorMsg.error "Empty module path";
adamc@1090 991 (Source.DStyle "Boo", loc))
adamc@1090 992 | [_] => d
adamc@1090 993 | piece :: pieces =>
adamc@1090 994 let
adamc@1146 995 val this = case prefix of
adamc@1146 996 "" => piece
adamc@1146 997 | _ => prefix ^ "." ^ piece
adamc@1090 998 val old = SS.member (!defed, this)
adamc@1146 999
adamc@1146 1000 fun notThere (ch, s) =
adamc@1146 1001 Substring.isEmpty (#2 (Substring.splitl
adamc@1146 1002 (fn ch' => ch' <> ch) s))
adamc@1146 1003
adamc@1146 1004 fun simOpen () =
adamc@1146 1005 SS.foldl (fn (full, ds) =>
adamc@1146 1006 if String.isPrefix (this ^ ".") full
adamc@1146 1007 andalso notThere (#".",
adamc@1146 1008 Substring.extract (full,
adamc@1146 1009 size
adamc@1146 1010 this + 1,
adamc@1146 1011 NONE)) then
adamc@1146 1012 let
adamc@1146 1013 val parts = String.tokens
adamc@1146 1014 (fn ch => ch = #".") full
adamc@1146 1015
adamc@1146 1016 val part = List.last parts
adamc@1146 1017
adamc@1146 1018 val imp = if length parts >= 2 then
adamc@1146 1019 (Source.StrProj
adamc@1146 1020 ((Source.StrVar
adamc@1146 1021 (List.nth (parts,
adamc@1146 1022 length
adamc@1146 1023 parts
adamc@1146 1024 - 2)),
adamc@1146 1025 loc),
adamc@1146 1026 part), loc)
adamc@1146 1027 else
adamc@1146 1028 (Source.StrVar part, loc)
adamc@1146 1029 in
adam@1732 1030 (Source.DStr (part, NONE, NONE, imp),
adamc@1146 1031 loc) :: ds
adamc@1146 1032 end
adamc@1146 1033 else
adamc@1146 1034 ds) [] (!fulls)
adamc@1090 1035 in
adamc@1090 1036 defed := SS.add (!defed, this);
adam@1732 1037 (Source.DStr (piece, NONE, NONE,
adamc@1090 1038 (Source.StrConst (if old then
adamc@1146 1039 simOpen ()
adamc@1146 1040 @ [makeD this pieces]
adamc@1090 1041 else
adamc@1146 1042 [makeD this pieces]), loc)),
adamc@1090 1043 loc)
adamc@1090 1044 end
adamc@1090 1045 in
adamc@1092 1046 if SS.member (!fulls, full) then
adamc@1092 1047 ErrorMsg.error ("Rooted module " ^ full ^ " has multiple versions.")
adamc@1092 1048 else
adamc@1092 1049 ();
adamc@1146 1050
adamc@1090 1051 makeD "" pieces
adamc@1146 1052 before ignore (foldl (fn (new, path) =>
adamc@1146 1053 let
adamc@1146 1054 val new' = case path of
adamc@1146 1055 "" => new
adamc@1146 1056 | _ => path ^ "." ^ new
adamc@1146 1057 in
adamc@1146 1058 fulls := SS.add (!fulls, new');
adamc@1146 1059 new'
adamc@1146 1060 end) "" pieces)
adamc@1090 1061 end
adamc@201 1062 in
adamc@834 1063 checkErrors ();
adamc@1090 1064 d
adamc@201 1065 end
adamc@56 1066
adamc@764 1067 val dsFfi = map parseFfi ffi
adamc@201 1068 val ds = map parseOne fnames
adamc@1090 1069 val loc = ErrorMsg.dummySpan
adamc@201 1070 in
adamc@834 1071 if !anyErrors then
adamc@834 1072 ErrorMsg.error "Parse failure"
adamc@834 1073 else
adamc@834 1074 ();
adamc@834 1075
adamc@201 1076 let
adamc@1126 1077 val final = List.last fnames
adamc@1126 1078 val final = case List.find (fn (root, name) =>
adamc@1126 1079 String.isPrefix (root ^ "/") final) mrs of
adamc@1126 1080 NONE => (Source.StrVar (nameOf final), loc)
adamc@1126 1081 | SOME (root, name) =>
adamc@1126 1082 let
adamc@1126 1083 val m = (Source.StrVar name, loc)
adamc@1126 1084 val final = String.extract (final, size root + 1, NONE)
adamc@1264 1085 val fields = String.fields (fn ch => ch = #"/") final
adamc@1264 1086 val fields = List.filter (fn s => size s = 0
adamc@1264 1087 orelse not (Char.isDigit (String.sub (s, 0))))
adamc@1264 1088 fields
adamc@1126 1089 in
adamc@1126 1090 foldl (fn (x, m) => (Source.StrProj (m, capitalize x), loc))
adamc@1264 1091 m fields
adamc@1126 1092 end
adamc@271 1093
adamc@764 1094 val ds = dsFfi @ ds
adamc@1126 1095 @ [(Source.DExport final, loc)]
adamc@1090 1096
adamc@1090 1097 val ds = case database of
adamc@1090 1098 NONE => ds
adamc@1090 1099 | SOME s => (Source.DDatabase s, loc) :: ds
adam@1294 1100
adam@1294 1101 val ds = case onError of
adam@1294 1102 NONE => ds
adam@1294 1103 | SOME v => ds @ [(Source.DOnError v, loc)]
adamc@201 1104 in
adam@1738 1105 ignore (List.foldl (fn (d, used) =>
adam@1738 1106 case #1 d of
adam@1738 1107 Source.DStr (x, _, _, _) =>
adam@1743 1108 (case SM.find (used, x) of
adam@1743 1109 SOME loc =>
adam@1743 1110 (ErrorMsg.error ("Duplicate top-level module name " ^ x);
adam@1743 1111 Print.prefaces "Files" [("Previous", Print.PD.string (ErrorMsg.spanToString loc)),
adam@1743 1112 ("Current", Print.PD.string (ErrorMsg.spanToString (#2 d)))];
adam@1743 1113 used)
adam@1743 1114 | NONE =>
adam@1743 1115 SM.insert (used, x, #2 d))
adam@1743 1116 | _ => used) SM.empty ds);
adamc@1090 1117 ds
adamc@201 1118 end handle Empty => ds
adamc@201 1119 end,
adamc@201 1120 print = SourcePrint.p_file
adamc@201 1121 }
adamc@56 1122
adamc@270 1123 val toParse = transform parse "parse" o toParseJob
adamc@38 1124
adamc@201 1125 val elaborate = {
adamc@201 1126 func = fn file => let
ezyang@1739 1127 val basisF = Settings.libFile "basis.urs"
ezyang@1739 1128 val topF = Settings.libFile "top.urs"
ezyang@1739 1129 val topF' = Settings.libFile "top.ur"
adam@1732 1130
adam@1732 1131 val basis = #func parseUrs basisF
adam@1732 1132 val topSgn = #func parseUrs topF
adam@1732 1133 val topStr = #func parseUr topF'
adam@1732 1134
adam@1732 1135 val tm1 = OS.FileSys.modTime topF
adam@1732 1136 val tm2 = OS.FileSys.modTime topF'
adamc@201 1137 in
adam@1732 1138 Elaborate.elabFile basis (OS.FileSys.modTime basisF)
adam@1732 1139 topStr topSgn (if Time.< (tm1, tm2) then tm2 else tm1)
adam@1732 1140 ElabEnv.empty file
adamc@201 1141 end,
adamc@201 1142 print = ElabPrint.p_file ElabEnv.empty
adamc@201 1143 }
adamc@5 1144
adamc@270 1145 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 1146
adamc@448 1147 val unnest = {
adamc@448 1148 func = Unnest.unnest,
adamc@448 1149 print = ElabPrint.p_file ElabEnv.empty
adamc@448 1150 }
adamc@448 1151
adamc@448 1152 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 1153
adamc@313 1154 val termination = {
adamc@313 1155 func = (fn file => (Termination.check file; file)),
adamc@313 1156 print = ElabPrint.p_file ElabEnv.empty
adamc@313 1157 }
adamc@313 1158
adamc@448 1159 val toTermination = transform termination "termination" o toUnnest
adamc@313 1160
adamc@201 1161 val explify = {
adamc@201 1162 func = Explify.explify,
adamc@201 1163 print = ExplPrint.p_file ExplEnv.empty
adamc@201 1164 }
adamc@201 1165
adamc@625 1166 val toExplify = transform explify "explify" o toUnnest
adamc@201 1167
adamc@201 1168 val corify = {
adamc@201 1169 func = Corify.corify,
adamc@201 1170 print = CorePrint.p_file CoreEnv.empty
adamc@201 1171 }
adamc@201 1172
adamc@270 1173 val toCorify = transform corify "corify" o toExplify
adamc@201 1174
adamc@482 1175 (*val reduce_local = {
adamc@482 1176 func = ReduceLocal.reduce,
adamc@482 1177 print = CorePrint.p_file CoreEnv.empty
adamc@482 1178 }
adamc@482 1179
adamc@482 1180 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 1181
adamc@443 1182 val especialize = {
adamc@443 1183 func = ESpecialize.specialize,
adamc@443 1184 print = CorePrint.p_file CoreEnv.empty
adamc@443 1185 }
adamc@443 1186
adamc@454 1187 val core_untangle = {
adamc@454 1188 func = CoreUntangle.untangle,
adamc@454 1189 print = CorePrint.p_file CoreEnv.empty
adamc@454 1190 }
adamc@454 1191
adamc@794 1192 val toCore_untangle = transform core_untangle "core_untangle" o toCorify
adamc@454 1193
adamc@202 1194 val shake = {
adamc@202 1195 func = Shake.shake,
adamc@202 1196 print = CorePrint.p_file CoreEnv.empty
adamc@202 1197 }
adamc@39 1198
adamc@454 1199 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 1200
adam@1362 1201 val toEspecialize1' = transform especialize "especialize1'" o toShake1
adam@1362 1202 val toShake1' = transform shake "shake1'" o toEspecialize1'
adam@1362 1203
adamc@607 1204 val rpcify = {
adamc@607 1205 func = Rpcify.frob,
adamc@607 1206 print = CorePrint.p_file CoreEnv.empty
adamc@607 1207 }
adamc@607 1208
adam@1362 1209 val toRpcify = transform rpcify "rpcify" o toShake1'
adamc@607 1210
adamc@642 1211 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 1212 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@1181 1213
adamc@1186 1214 val toEspecialize1 = transform especialize "especialize1" o toShake2
adamc@1181 1215
adamc@1062 1216 val toCore_untangle3 = transform core_untangle "core_untangle3" o toEspecialize1
adamc@1062 1217 val toShake3 = transform shake "shake3" o toCore_untangle3
adamc@642 1218
adamc@202 1219 val tag = {
adamc@202 1220 func = Tag.tag,
adamc@202 1221 print = CorePrint.p_file CoreEnv.empty
adamc@202 1222 }
adamc@193 1223
adamc@1062 1224 val toTag = transform tag "tag" o toShake3
adamc@20 1225
adamc@202 1226 val reduce = {
adamc@202 1227 func = Reduce.reduce,
adamc@202 1228 print = CorePrint.p_file CoreEnv.empty
adamc@202 1229 }
adamc@25 1230
adamc@692 1231 val toReduce = transform reduce "reduce" o toTag
adamc@23 1232
adamc@1186 1233 val toShakey = transform shake "shakey" o toReduce
adamc@1186 1234
adamc@1186 1235 val unpoly = {
adamc@1186 1236 func = Unpoly.unpoly,
adamc@1186 1237 print = CorePrint.p_file CoreEnv.empty
adamc@1186 1238 }
adamc@1186 1239
adamc@1186 1240 val toUnpoly = transform unpoly "unpoly" o toShakey
adamc@315 1241
adamc@202 1242 val specialize = {
adamc@202 1243 func = Specialize.specialize,
adamc@202 1244 print = CorePrint.p_file CoreEnv.empty
adamc@202 1245 }
adamc@132 1246
adamc@1186 1247 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 1248
adamc@1062 1249 val toShake4 = transform shake "shake4" o toSpecialize
adamc@133 1250
adamc@1062 1251 val toEspecialize2 = transform especialize "especialize2" o toShake4
adamc@1272 1252 val toShake4' = transform shake "shake4'" o toEspecialize2
adamc@1272 1253 val toUnpoly2 = transform unpoly "unpoly2" o toShake4'
adamc@1276 1254 val toSpecialize2 = transform specialize "specialize2" o toUnpoly2
adamc@1276 1255 val toShake4'' = transform shake "shake4'" o toSpecialize2
adamc@1272 1256 val toEspecialize3 = transform especialize "especialize3" o toShake4''
adamc@794 1257
adamc@1272 1258 val toReduce2 = transform reduce "reduce2" o toEspecialize3
adamc@898 1259
adamc@1062 1260 val toShake5 = transform shake "shake5" o toReduce2
adamc@794 1261
adamc@692 1262 val marshalcheck = {
adamc@692 1263 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 1264 print = CorePrint.p_file CoreEnv.empty
adamc@692 1265 }
adamc@692 1266
adamc@1062 1267 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake5
adamc@692 1268
adamc@732 1269 val effectize = {
adamc@732 1270 func = Effective.effectize,
adamc@732 1271 print = CorePrint.p_file CoreEnv.empty
adamc@732 1272 }
adamc@732 1273
adamc@732 1274 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 1275
adamc@1170 1276 val css = {
adamc@1170 1277 func = Css.summarize,
adamc@1170 1278 print = fn _ => Print.box []
adamc@1170 1279 }
adamc@1170 1280
adamc@1170 1281 val toCss = transform css "css" o toShake5
adamc@1170 1282
adamc@202 1283 val monoize = {
adamc@202 1284 func = Monoize.monoize CoreEnv.empty,
adamc@202 1285 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1286 }
adamc@134 1287
adamc@732 1288 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 1289
adamc@202 1290 val mono_opt = {
adamc@910 1291 func = MonoOpt.optimize,
adamc@202 1292 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1293 }
adamc@29 1294
adamc@270 1295 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 1296
adamc@202 1297 val untangle = {
adamc@202 1298 func = Untangle.untangle,
adamc@202 1299 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1300 }
adamc@1 1301
adamc@270 1302 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 1303
adamc@202 1304 val mono_reduce = {
adamc@202 1305 func = MonoReduce.reduce,
adamc@202 1306 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1307 }
adamc@16 1308
adamc@270 1309 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 1310
adamc@202 1311 val mono_shake = {
adamc@202 1312 func = MonoShake.shake,
adamc@202 1313 print = MonoPrint.p_file MonoEnv.empty
adamc@202 1314 }
adamc@110 1315
adamc@270 1316 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 1317
adamc@572 1318 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 1319
adamc@1200 1320 val iflow = {
adamc@1235 1321 func = (fn file => (if !doIflow then Iflow.check file else (); file)),
adamc@1200 1322 print = MonoPrint.p_file MonoEnv.empty
adamc@1200 1323 }
adamc@1200 1324
adamc@1200 1325 val toIflow = transform iflow "iflow" o toMono_opt2
adamc@1200 1326
adamc@567 1327 val jscomp = {
adamc@567 1328 func = JsComp.process,
adamc@567 1329 print = MonoPrint.p_file MonoEnv.empty
adamc@567 1330 }
adamc@567 1331
adamc@1200 1332 val toJscomp = transform jscomp "jscomp" o toIflow
adamc@567 1333
adamc@910 1334 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 1335
adamc@506 1336 val fuse = {
adamc@506 1337 func = Fuse.fuse,
adamc@506 1338 print = MonoPrint.p_file MonoEnv.empty
adamc@506 1339 }
adamc@506 1340
adamc@572 1341 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 1342
adamc@506 1343 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 1344
adamc@601 1345 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 1346 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@916 1347 val toMono_opt4 = transform mono_opt "mono_opt4" o toMono_shake2
adamc@1017 1348 val toMono_reduce3 = transform mono_reduce "mono_reduce3" o toMono_opt4
adamc@1185 1349 val toFuse2 = transform fuse "fuse2" o toMono_reduce3
adamc@916 1350 val toUntangle3 = transform untangle "untangle3" o toFuse2
adamc@916 1351 val toMono_shake3 = transform mono_shake "mono_shake3" o toUntangle3
adamc@506 1352
adamc@377 1353 val pathcheck = {
adamc@377 1354 func = (fn file => (PathCheck.check file; file)),
adamc@377 1355 print = MonoPrint.p_file MonoEnv.empty
adamc@377 1356 }
adamc@377 1357
adamc@916 1358 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake3
adamc@377 1359
adam@1595 1360 val sidecheck = {
adam@1595 1361 func = SideCheck.check,
adam@1595 1362 print = MonoPrint.p_file MonoEnv.empty
adam@1595 1363 }
adam@1595 1364
adam@1595 1365 val toSidecheck = transform sidecheck "sidecheck" o toPathcheck
adam@1595 1366
adamc@202 1367 val cjrize = {
adamc@202 1368 func = Cjrize.cjrize,
adamc@202 1369 print = CjrPrint.p_file CjrEnv.empty
adamc@202 1370 }
adamc@23 1371
adam@1595 1372 val toCjrize = transform cjrize "cjrize" o toSidecheck
adamc@29 1373
adamc@643 1374 val scriptcheck = {
adamc@643 1375 func = ScriptCheck.classify,
adamc@643 1376 print = CjrPrint.p_file CjrEnv.empty
adamc@643 1377 }
adamc@643 1378
adamc@643 1379 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 1380
adamc@282 1381 val prepare = {
adamc@282 1382 func = Prepare.prepare,
adamc@282 1383 print = CjrPrint.p_file CjrEnv.empty
adamc@282 1384 }
adamc@282 1385
adamc@643 1386 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 1387
adamc@879 1388 val checknest = {
adamc@879 1389 func = fn f => if #supportsNestedPrepared (Settings.currentDbms ()) then f else Checknest.annotate f,
adamc@879 1390 print = CjrPrint.p_file CjrEnv.empty
adamc@879 1391 }
adamc@879 1392
adamc@879 1393 val toChecknest = transform checknest "checknest" o toPrepare
adamc@879 1394
adamc@274 1395 val sqlify = {
adamc@274 1396 func = Cjrize.cjrize,
adamc@274 1397 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 1398 }
adamc@274 1399
adamc@274 1400 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 1401
adam@1473 1402 val escapeFilename = String.translate (fn #" " => "\\ " | #"\"" => "\\\"" | #"'" => "\\'" | ch => str ch)
adam@1473 1403
adam@1540 1404 val beforeC = ref (fn () => ())
adam@1540 1405
adam@1725 1406 fun compileC {cname, oname, ename, libs, profile, debug, linker, link = link'} =
adamc@183 1407 let
adamc@855 1408 val proto = Settings.currentProtocol ()
adamc@1095 1409
adam@1368 1410 val lib = if Settings.getStaticLinking () then
ezyang@1739 1411 " " ^ !Settings.configLib ^ "/" ^ #linkStatic proto ^ " " ^ !Settings.configLib ^ "/liburweb.a"
adam@1368 1412 else
ezyang@1739 1413 "-L" ^ !Settings.configLib ^ " " ^ #linkDynamic proto ^ " -lurweb"
adamc@378 1414
adam@1558 1415 val opt = if debug then
adam@1558 1416 ""
adam@1558 1417 else
adam@1558 1418 " -O3"
adam@1558 1419
adam@1558 1420 val compile = Config.ccompiler ^ " " ^ Config.ccArgs ^ " " ^ Config.pthreadCflags ^ " -Wimplicit -Werror -Wno-unused-value"
ezyang@1739 1421 ^ opt ^ " -I " ^ !Settings.configInclude
adamc@1096 1422 ^ " " ^ #compile proto
adam@1473 1423 ^ " -c " ^ escapeFilename cname ^ " -o " ^ escapeFilename oname
adamc@1096 1424
adam@1725 1425 val linker = Option.getOpt (linker, Config.ccompiler ^ " -Werror" ^ opt ^ " " ^ Config.ccArgs ^ " " ^ Config.pthreadCflags ^ " " ^ Config.pthreadLibs)
adam@1725 1426
adam@1725 1427 val link = linker
adam@1659 1428 ^ " " ^ lib ^ " " ^ escapeFilename oname ^ " " ^ libs ^ " -lm " ^ Config.openssl ^ " -o " ^ escapeFilename ename
adamc@502 1429
adamc@502 1430 val (compile, link) =
adamc@502 1431 if profile then
adamc@502 1432 (compile ^ " -pg", link ^ " -pg")
adamc@502 1433 else
adamc@502 1434 (compile, link)
adamc@742 1435
adamc@742 1436 val (compile, link) =
adamc@742 1437 if debug then
adamc@742 1438 (compile ^ " -g", link ^ " -g")
adamc@742 1439 else
adamc@742 1440 (compile, link)
adamc@764 1441
adamc@764 1442 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adam@1394 1443
adam@1394 1444 fun system s =
adam@1394 1445 (if debug then
adam@1394 1446 print (s ^ "\n")
adam@1394 1447 else
adam@1394 1448 ();
adam@1394 1449 OS.Process.isSuccess (OS.Process.system s))
adamc@183 1450 in
adam@1540 1451 !beforeC ();
adam@1394 1452 system compile andalso system link
adamc@183 1453 end
adamc@183 1454
adamc@202 1455 fun compile job =
adamc@879 1456 case run toChecknest job of
adamc@1045 1457 NONE => false
adamc@29 1458 | SOME file =>
adamc@202 1459 let
adamc@274 1460 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 1461
adamc@274 1462 val (cname, oname, cleanup) =
adamc@274 1463 if #debug job then
adamc@457 1464 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 1465 else
adamc@274 1466 let
adamc@274 1467 val dir = OS.FileSys.tmpName ()
adamc@403 1468 val () = if OS.FileSys.access (dir, []) then
adamc@403 1469 OS.FileSys.remove dir
adamc@403 1470 else
adamc@403 1471 ()
adamc@457 1472 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 1473 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 1474 in
adamc@274 1475 OS.FileSys.mkDir dir;
adamc@274 1476 (cname, oname,
adam@1700 1477 fn () => (if OS.Process.isSuccess (OS.Process.system ("rm -rf " ^ dir)) then
adam@1700 1478 ()
adam@1700 1479 else
adam@1700 1480 raise Fail ("Unable to delete temporary directory " ^ dir)))
adamc@274 1481 end
adamc@274 1482 val ename = #exe job
adamc@202 1483 in
adamc@274 1484 let
adamc@274 1485 val outf = TextIO.openOut cname
adamc@274 1486 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 1487
adamc@432 1488 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 1489 val libs =
adamc@432 1490 if hasDb then
adamc@866 1491 #link (Settings.currentDbms ())
adamc@432 1492 else
adamc@432 1493 ""
adamc@274 1494 in
adamc@274 1495 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 1496 TextIO.output1 (outf, #"\n");
adamc@274 1497 TextIO.closeOut outf;
adamc@102 1498
adam@1381 1499 if ErrorMsg.anyErrors () then
adam@1381 1500 false
adam@1381 1501 else
adam@1381 1502 (case #sql job of
adam@1381 1503 NONE => ()
adam@1381 1504 | SOME sql =>
adam@1381 1505 let
adam@1381 1506 val outf = TextIO.openOut sql
adam@1381 1507 val s = TextIOPP.openOut {dst = outf, wid = 80}
adam@1381 1508 in
adam@1381 1509 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adam@1381 1510 TextIO.closeOut outf
adam@1381 1511 end;
adamc@274 1512
adam@1381 1513 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adam@1725 1514 profile = #profile job, debug = #debug job, linker = #linker job, link = #link job}
adam@1381 1515
adam@1381 1516 before cleanup ())
adamc@274 1517 end
adamc@274 1518 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 1519 end
adamc@29 1520
adamc@1045 1521 fun compiler job =
adamc@1045 1522 if compile job then
adamc@1045 1523 ()
adamc@1045 1524 else
adamc@1045 1525 OS.Process.exit OS.Process.failure
adamc@1045 1526
adamc@1266 1527 fun moduleOf fname =
adamc@1266 1528 let
adamc@1266 1529 val mrs = !moduleRoots
adamc@1266 1530 val fname = OS.Path.mkCanonical fname
adamc@1266 1531 in
adamc@1266 1532 case List.find (fn (root, _) => String.isPrefix (root ^ "/") fname) mrs of
adamc@1266 1533 NONE => capitalize (OS.Path.base (OS.Path.file fname))
adamc@1266 1534 | SOME (root, name) =>
adamc@1266 1535 let
adamc@1266 1536 val fname = OS.Path.base fname
adamc@1266 1537 val fname = String.extract (fname, size root + 1, NONE)
adamc@1266 1538 val fs = String.fields (fn ch => ch = #"/") fname
adamc@1266 1539 val fs = List.filter (CharVector.exists (fn ch => not (Char.isDigit ch))) fs
adamc@1266 1540 val fs = map capitalize fs
adamc@1266 1541 in
adamc@1266 1542 String.concatWith "." (name :: fs)
adamc@1266 1543 end
adamc@1266 1544 end
adamc@1266 1545
adamc@1 1546 end