annotate src/compiler.sml @ 1158:ed3e5329b60e

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