annotate src/compiler.sml @ 1235:a7b773f1d053

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