annotate src/compiler.sml @ 1696:177547450bb9

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