annotate src/compiler.sml @ 767:d27ed5ddeb52

Add 'library' directive
author Adam Chlipala <adamc@hcoop.net>
date Sat, 02 May 2009 12:50:52 -0400
parents df09c95085f8
children 3b7e46790fa7
rev   line source
adamc@764 1 (* Copyright (c) 2008-2009, 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@765 53 jsFuncs : (Settings.ffi * string) list
adamc@270 54 }
adamc@201 55
adamc@201 56 type ('src, 'dst) phase = {
adamc@201 57 func : 'src -> 'dst,
adamc@201 58 print : 'dst -> Print.PD.pp_desc
adamc@201 59 }
adamc@201 60
adamc@201 61 type pmap = (string * Time.time) list
adamc@201 62
adamc@201 63 type ('src, 'dst) transform = {
adamc@201 64 func : 'src -> 'dst option,
adamc@201 65 print : 'dst -> Print.PD.pp_desc,
adamc@201 66 time : 'src * pmap -> 'dst option * pmap
adamc@201 67 }
adamc@201 68
adamc@201 69 fun transform (ph : ('src, 'dst) phase) name = {
adamc@201 70 func = fn input => let
adamc@201 71 val v = #func ph input
adamc@201 72 in
adamc@201 73 if ErrorMsg.anyErrors () then
adamc@201 74 NONE
adamc@201 75 else
adamc@201 76 SOME v
adamc@201 77 end,
adamc@201 78 print = #print ph,
adamc@201 79 time = fn (input, pmap) => let
adamc@201 80 val befor = Time.now ()
adamc@201 81 val v = #func ph input
adamc@201 82 val elapsed = Time.- (Time.now (), befor)
adamc@201 83 in
adamc@201 84 (if ErrorMsg.anyErrors () then
adamc@201 85 NONE
adamc@201 86 else
adamc@201 87 SOME v,
adamc@201 88 (name, elapsed) :: pmap)
adamc@201 89 end
adamc@201 90 }
adamc@201 91
adamc@270 92 fun op o (tr2 : ('b, 'c) transform, tr1 : ('a, 'b) transform) = {
adamc@201 93 func = fn input => case #func tr1 input of
adamc@201 94 NONE => NONE
adamc@201 95 | SOME v => #func tr2 v,
adamc@201 96 print = #print tr2,
adamc@201 97 time = fn (input, pmap) => let
adamc@201 98 val (ro, pmap) = #time tr1 (input, pmap)
adamc@201 99 in
adamc@201 100 case ro of
adamc@201 101 NONE => (NONE, pmap)
adamc@201 102 | SOME v => #time tr2 (v, pmap)
adamc@201 103 end
adamc@201 104 }
adamc@201 105
adamc@346 106 fun check (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@346 107 ignore (#func tr x))
adamc@346 108
adamc@280 109 fun run (tr : ('src, 'dst) transform) x = (ErrorMsg.resetErrors ();
adamc@280 110 #func tr x)
adamc@201 111
adamc@201 112 fun runPrint (tr : ('src, 'dst) transform) input =
adamc@280 113 (ErrorMsg.resetErrors ();
adamc@280 114 case #func tr input of
adamc@280 115 NONE => print "Failure\n"
adamc@280 116 | SOME v =>
adamc@280 117 (print "Success\n";
adamc@280 118 Print.print (#print tr v);
adamc@280 119 print "\n"))
adamc@201 120
adamc@201 121 fun time (tr : ('src, 'dst) transform) input =
adamc@55 122 let
adamc@201 123 val (_, pmap) = #time tr (input, [])
adamc@201 124 in
adamc@201 125 app (fn (name, time) =>
adamc@201 126 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 127 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 128 print "\n"
adamc@201 129 end
adamc@55 130
adamc@201 131 fun timePrint (tr : ('src, 'dst) transform) input =
adamc@201 132 let
adamc@201 133 val (ro, pmap) = #time tr (input, [])
adamc@55 134 in
adamc@201 135 app (fn (name, time) =>
adamc@201 136 print (name ^ ": " ^ LargeReal.toString (Time.toReal time) ^ "\n")) (rev pmap);
adamc@201 137 print ("TOTAL: " ^ LargeReal.toString (Time.toReal (foldl Time.+ Time.zeroTime (map #2 pmap))) ^ "\n");
adamc@201 138 print "\n";
adamc@201 139 case ro of
adamc@201 140 NONE => print "Failure\n"
adamc@201 141 | SOME v =>
adamc@201 142 (print "Success\n";
adamc@201 143 Print.print (#print tr v);
adamc@201 144 print "\n")
adamc@55 145 end
adamc@55 146
adamc@244 147 val parseUrs =
adamc@201 148 {func = fn filename => let
adamc@201 149 val fname = OS.FileSys.tmpName ()
adamc@201 150 val outf = TextIO.openOut fname
adamc@201 151 val () = TextIO.output (outf, "sig\n")
adamc@201 152 val inf = TextIO.openIn filename
adamc@201 153 fun loop () =
adamc@201 154 case TextIO.inputLine inf of
adamc@201 155 NONE => ()
adamc@201 156 | SOME line => (TextIO.output (outf, line);
adamc@201 157 loop ())
adamc@201 158 val () = loop ()
adamc@201 159 val () = TextIO.closeIn inf
adamc@201 160 val () = TextIO.closeOut outf
adamc@201 161
adamc@201 162 val () = (ErrorMsg.resetErrors ();
adamc@201 163 ErrorMsg.resetPositioning filename;
adamc@201 164 Lex.UserDeclarations.initialize ())
adamc@201 165 val file = TextIO.openIn fname
adamc@201 166 fun get _ = TextIO.input file
adamc@201 167 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 168 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 169 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 170 in
adamc@201 171 TextIO.closeIn file;
adamc@201 172 case absyn of
adamc@201 173 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => sgis
adamc@201 174 | _ => (ErrorMsg.errorAt {file = filename,
adamc@201 175 first = {line = 0,
adamc@201 176 char = 0},
adamc@201 177 last = {line = 0,
adamc@201 178 char = 0}} "Not a signature";
adamc@201 179 [])
adamc@201 180 end
adamc@201 181 handle LrParser.ParseError => [],
adamc@201 182 print = Print.p_list_sep Print.PD.newline SourcePrint.p_sgn_item}
adamc@55 183
adamc@1 184 (* The main parsing routine *)
adamc@244 185 val parseUr = {
adamc@201 186 func = fn filename =>
adamc@201 187 let
adamc@201 188 val () = (ErrorMsg.resetErrors ();
adamc@201 189 ErrorMsg.resetPositioning filename;
adamc@201 190 Lex.UserDeclarations.initialize ())
adamc@201 191 val file = TextIO.openIn filename
adamc@201 192 fun get _ = TextIO.input file
adamc@201 193 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@201 194 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@244 195 val (absyn, _) = UrwebP.parse (30, lexer, parseerror, ())
adamc@201 196 in
adamc@201 197 TextIO.closeIn file;
adamc@201 198 case absyn of
adamc@201 199 [(Source.DSgn ("?", _), _)] =>
adamc@201 200 (ErrorMsg.errorAt {file = filename,
adamc@201 201 first = {line = 0,
adamc@201 202 char = 0},
adamc@201 203 last = {line = 0,
adamc@201 204 char = 0}} "File starts with 'sig'";
adamc@201 205 [])
adamc@201 206 | _ => absyn
adamc@201 207 end
adamc@201 208 handle LrParser.ParseError => [],
adamc@201 209 print = SourcePrint.p_file}
adamc@56 210
adamc@765 211 fun p_job {prefix, database, exe, sql, sources, debug, profile,
adamc@766 212 timeout, ffi, link, headers, scripts,
adamc@765 213 clientToServer, effectful, clientOnly, serverOnly, jsFuncs} =
adamc@270 214 let
adamc@270 215 open Print.PD
adamc@270 216 open Print
adamc@765 217
adamc@765 218 fun p_ffi name = p_list_sep (box []) (fn (m, s) =>
adamc@765 219 box [string name, space, string m, string ".", string s, newline])
adamc@270 220 in
adamc@274 221 box [if debug then
adamc@274 222 box [string "DEBUG", newline]
adamc@274 223 else
adamc@274 224 box [],
adamc@502 225 if profile then
adamc@502 226 box [string "PROFILE", newline]
adamc@502 227 else
adamc@502 228 box [],
adamc@274 229 case database of
adamc@270 230 NONE => string "No database."
adamc@270 231 | SOME db => string ("Database: " ^ db),
adamc@270 232 newline,
adamc@274 233 string "Exe: ",
adamc@274 234 string exe,
adamc@274 235 newline,
adamc@274 236 case sql of
adamc@274 237 NONE => string "No SQL file."
adamc@274 238 | SOME sql => string ("SQL fle: " ^ sql),
adamc@673 239 newline,
adamc@673 240 string "Timeout: ",
adamc@673 241 string (Int.toString timeout),
adamc@673 242 newline,
adamc@764 243 p_list_sep (box []) (fn s => box [string "Ffi", space, string s, newline]) ffi,
adamc@764 244 p_list_sep (box []) (fn s => box [string "Header", space, string s, newline]) headers,
adamc@766 245 p_list_sep (box []) (fn s => box [string "Script", space, string s, newline]) scripts,
adamc@764 246 p_list_sep (box []) (fn s => box [string "Link", space, string s, newline]) link,
adamc@765 247 p_ffi "ClientToServer" clientToServer,
adamc@765 248 p_ffi "Effectful" effectful,
adamc@765 249 p_ffi "ClientOnly" clientOnly,
adamc@765 250 p_ffi "ServerOnly" serverOnly,
adamc@765 251 p_list_sep (box []) (fn ((m, s), s') =>
adamc@765 252 box [string "JsFunc", space, string m, string ".", string s,
adamc@765 253 space, string "=", space, string s', newline]) jsFuncs,
adamc@270 254 string "Sources:",
adamc@270 255 p_list string sources,
adamc@270 256 newline]
adamc@270 257 end
adamc@270 258
adamc@270 259 fun trim s =
adamc@270 260 let
adamc@270 261 val (_, s) = Substring.splitl Char.isSpace s
adamc@270 262 val (s, _) = Substring.splitr Char.isSpace s
adamc@270 263 in
adamc@270 264 s
adamc@270 265 end
adamc@270 266
adamc@767 267 fun parseUrp' filename =
adamc@767 268 let
adamc@767 269 val dir = OS.Path.dir filename
adamc@767 270 val inf = TextIO.openIn (OS.Path.joinBaseExt {base = filename, ext = SOME "urp"})
adamc@767 271
adamc@767 272 fun relify fname =
adamc@767 273 OS.Path.concat (dir, fname)
adamc@767 274 handle OS.Path.Path => fname
adamc@767 275
adamc@767 276 val absDir = OS.Path.mkAbsolute {path = dir, relativeTo = OS.FileSys.getDir ()}
adamc@767 277
adamc@767 278 fun relifyA fname = OS.Path.mkAbsolute {path = fname, relativeTo = absDir}
adamc@767 279
adamc@767 280 fun readSources acc =
adamc@767 281 case TextIO.inputLine inf of
adamc@767 282 NONE => rev acc
adamc@767 283 | SOME line =>
adamc@767 284 let
adamc@767 285 val acc = if CharVector.all Char.isSpace line then
adamc@767 286 acc
adamc@767 287 else
adamc@767 288 let
adamc@767 289 val fname = String.implode (List.filter (fn x => not (Char.isSpace x))
adamc@767 290 (String.explode line))
adamc@767 291 val fname = relify fname
adamc@767 292 in
adamc@767 293 fname :: acc
adamc@767 294 end
adamc@767 295 in
adamc@767 296 readSources acc
adamc@767 297 end
adamc@767 298
adamc@767 299 val prefix = ref NONE
adamc@767 300 val database = ref NONE
adamc@767 301 val exe = ref NONE
adamc@767 302 val sql = ref NONE
adamc@767 303 val debug = ref false
adamc@767 304 val profile = ref false
adamc@767 305 val timeout = ref NONE
adamc@767 306 val ffi = ref []
adamc@767 307 val link = ref []
adamc@767 308 val headers = ref []
adamc@767 309 val scripts = ref []
adamc@767 310 val clientToServer = ref []
adamc@767 311 val effectful = ref []
adamc@767 312 val clientOnly = ref []
adamc@767 313 val serverOnly = ref []
adamc@767 314 val jsFuncs = ref []
adamc@767 315 val libs = ref []
adamc@767 316
adamc@767 317 fun finish sources =
adamc@767 318 let
adamc@767 319 val job = {
adamc@767 320 prefix = Option.getOpt (!prefix, "/"),
adamc@767 321 database = !database,
adamc@767 322 exe = Option.getOpt (!exe, OS.Path.joinBaseExt {base = OS.Path.base filename,
adamc@767 323 ext = SOME "exe"}),
adamc@767 324 sql = !sql,
adamc@767 325 debug = !debug,
adamc@767 326 profile = !profile,
adamc@767 327 timeout = Option.getOpt (!timeout, 60),
adamc@767 328 ffi = rev (!ffi),
adamc@767 329 link = rev (!link),
adamc@767 330 headers = rev (!headers),
adamc@767 331 scripts = rev (!scripts),
adamc@767 332 clientToServer = rev (!clientToServer),
adamc@767 333 effectful = rev (!effectful),
adamc@767 334 clientOnly = rev (!clientOnly),
adamc@767 335 serverOnly = rev (!serverOnly),
adamc@767 336 jsFuncs = rev (!jsFuncs),
adamc@767 337 sources = sources
adamc@767 338 }
adamc@767 339
adamc@767 340 fun mergeO f (old, new) =
adamc@767 341 case (old, new) of
adamc@767 342 (NONE, _) => new
adamc@767 343 | (_, NONE) => old
adamc@767 344 | (SOME v1, SOME v2) => SOME (f (v1, v2))
adamc@767 345
adamc@767 346 fun same desc = mergeO (fn (x : string, y) =>
adamc@767 347 (if x = y then
adamc@767 348 ()
adamc@767 349 else
adamc@767 350 ErrorMsg.error ("Multiple "
adamc@767 351 ^ desc ^ " values that don't agree");
adamc@767 352 x))
adamc@767 353
adamc@767 354 fun merge (old : job, new : job) = {
adamc@767 355 prefix = #prefix old,
adamc@767 356 database = #database old,
adamc@767 357 exe = #exe old,
adamc@767 358 sql = #sql old,
adamc@767 359 debug = #debug old orelse #debug new,
adamc@767 360 profile = #profile old orelse #profile new,
adamc@767 361 timeout = #timeout old,
adamc@767 362 ffi = #ffi old @ #ffi new,
adamc@767 363 link = #link old @ #link new,
adamc@767 364 headers = #headers old @ #headers new,
adamc@767 365 scripts = #scripts old @ #scripts new,
adamc@767 366 clientToServer = #clientToServer old @ #clientToServer new,
adamc@767 367 effectful = #effectful old @ #effectful new,
adamc@767 368 clientOnly = #clientOnly old @ #clientOnly new,
adamc@767 369 serverOnly = #serverOnly old @ #serverOnly new,
adamc@767 370 jsFuncs = #jsFuncs old @ #jsFuncs new,
adamc@767 371 sources = #sources old @ #sources new
adamc@767 372 }
adamc@767 373 in
adamc@767 374 foldr (fn (fname, job) => merge (job, parseUrp' fname)) job (!libs)
adamc@767 375 end
adamc@767 376
adamc@767 377 fun read () =
adamc@767 378 case TextIO.inputLine inf of
adamc@767 379 NONE => finish []
adamc@767 380 | SOME "\n" => finish (readSources [])
adamc@767 381 | SOME line =>
adamc@767 382 let
adamc@767 383 val (cmd, arg) = Substring.splitl (fn x => not (Char.isSpace x)) (Substring.full line)
adamc@767 384 val cmd = Substring.string (trim cmd)
adamc@767 385 val arg = Substring.string (trim arg)
adamc@767 386
adamc@767 387 fun ffiS () =
adamc@767 388 case String.fields (fn ch => ch = #".") arg of
adamc@767 389 [m, x] => (m, x)
adamc@767 390 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func");
adamc@767 391 ("", ""))
adamc@767 392
adamc@767 393 fun ffiM () =
adamc@767 394 case String.fields (fn ch => ch = #"=") arg of
adamc@767 395 [f, s] =>
adamc@767 396 (case String.fields (fn ch => ch = #".") f of
adamc@767 397 [m, x] => ((m, x), s)
adamc@767 398 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@767 399 (("", ""), "")))
adamc@767 400 | _ => (ErrorMsg.error (cmd ^ " argument not of the form Module.func=func'");
adamc@767 401 (("", ""), ""))
adamc@767 402 in
adamc@767 403 case cmd of
adamc@767 404 "prefix" =>
adamc@767 405 (case !prefix of
adamc@767 406 NONE => ()
adamc@767 407 | SOME _ => ErrorMsg.error "Duplicate 'prefix' directive";
adamc@767 408 prefix := SOME arg)
adamc@767 409 | "database" =>
adamc@767 410 (case !database of
adamc@767 411 NONE => ()
adamc@767 412 | SOME _ => ErrorMsg.error "Duplicate 'database' directive";
adamc@767 413 database := SOME arg)
adamc@767 414 | "exe" =>
adamc@767 415 (case !exe of
adamc@767 416 NONE => ()
adamc@767 417 | SOME _ => ErrorMsg.error "Duplicate 'exe' directive";
adamc@767 418 exe := SOME (relify arg))
adamc@767 419 | "sql" =>
adamc@767 420 (case !sql of
adamc@767 421 NONE => ()
adamc@767 422 | SOME _ => ErrorMsg.error "Duplicate 'sql' directive";
adamc@767 423 sql := SOME (relify arg))
adamc@767 424 | "debug" => debug := true
adamc@767 425 | "profile" => profile := true
adamc@767 426 | "timeout" =>
adamc@767 427 (case !timeout of
adamc@767 428 NONE => ()
adamc@767 429 | SOME _ => ErrorMsg.error "Duplicate 'timeout' directive";
adamc@767 430 timeout := SOME (valOf (Int.fromString arg)))
adamc@767 431 | "ffi" => ffi := relify arg :: !ffi
adamc@767 432 | "link" => link := relifyA arg :: !link
adamc@767 433 | "include" => headers := relifyA arg :: !headers
adamc@767 434 | "script" => scripts := arg :: !scripts
adamc@767 435 | "clientToServer" => clientToServer := ffiS () :: !clientToServer
adamc@767 436 | "effectful" => effectful := ffiS () :: !effectful
adamc@767 437 | "clientOnly" => clientOnly := ffiS () :: !clientOnly
adamc@767 438 | "serverOnly" => serverOnly := ffiS () :: !serverOnly
adamc@767 439 | "jsFunc" => jsFuncs := ffiM () :: !jsFuncs
adamc@767 440 | "library" => libs := relify arg :: !libs
adamc@767 441 | _ => ErrorMsg.error ("Unrecognized command '" ^ cmd ^ "'");
adamc@767 442 read ()
adamc@767 443 end
adamc@767 444
adamc@767 445 val job = read ()
adamc@767 446 in
adamc@767 447 TextIO.closeIn inf;
adamc@767 448 Settings.setUrlPrefix (#prefix job);
adamc@767 449 Settings.setTimeout (#timeout job);
adamc@767 450 Settings.setHeaders (#headers job);
adamc@767 451 Settings.setScripts (#scripts job);
adamc@767 452 Settings.setClientToServer (#clientToServer job);
adamc@767 453 Settings.setEffectful (#effectful job);
adamc@767 454 Settings.setClientOnly (#clientOnly job);
adamc@767 455 Settings.setServerOnly (#serverOnly job);
adamc@767 456 Settings.setJsFuncs (#jsFuncs job);
adamc@767 457 job
adamc@767 458 end
adamc@767 459
adamc@270 460 val parseUrp = {
adamc@767 461 func = parseUrp',
adamc@270 462 print = p_job
adamc@270 463 }
adamc@270 464
adamc@270 465 val toParseJob = transform parseUrp "parseJob"
adamc@270 466
adamc@56 467 fun capitalize "" = ""
adamc@56 468 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 469
adamc@201 470 val parse = {
adamc@764 471 func = fn {database, sources = fnames, ffi, ...} : job =>
adamc@201 472 let
adamc@201 473 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 474
adamc@764 475 fun parseFfi fname =
adamc@764 476 let
adamc@764 477 val mname = nameOf fname
adamc@764 478 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@764 479
adamc@764 480 val loc = {file = urs,
adamc@764 481 first = ErrorMsg.dummyPos,
adamc@764 482 last = ErrorMsg.dummyPos}
adamc@764 483
adamc@764 484 val sgn = (Source.SgnConst (#func parseUrs urs), loc)
adamc@764 485 in
adamc@764 486 (Source.DFfiStr (mname, sgn), loc)
adamc@764 487 end
adamc@764 488
adamc@201 489 fun parseOne fname =
adamc@201 490 let
adamc@201 491 val mname = nameOf fname
adamc@244 492 val ur = OS.Path.joinBaseExt {base = fname, ext = SOME "ur"}
adamc@244 493 val urs = OS.Path.joinBaseExt {base = fname, ext = SOME "urs"}
adamc@56 494
adamc@201 495 val sgnO =
adamc@244 496 if Posix.FileSys.access (urs, []) then
adamc@244 497 SOME (Source.SgnConst (#func parseUrs urs),
adamc@244 498 {file = urs,
adamc@201 499 first = ErrorMsg.dummyPos,
adamc@201 500 last = ErrorMsg.dummyPos})
adamc@201 501 else
adamc@201 502 NONE
adamc@56 503
adamc@244 504 val loc = {file = ur,
adamc@201 505 first = ErrorMsg.dummyPos,
adamc@201 506 last = ErrorMsg.dummyPos}
adamc@56 507
adamc@244 508 val ds = #func parseUr ur
adamc@201 509 in
adamc@201 510 (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@201 511 end
adamc@56 512
adamc@764 513 val dsFfi = map parseFfi ffi
adamc@201 514 val ds = map parseOne fnames
adamc@201 515 in
adamc@201 516 let
adamc@201 517 val final = nameOf (List.last fnames)
adamc@271 518
adamc@764 519 val ds = dsFfi @ ds
adamc@764 520 @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@201 521 in
adamc@271 522 case database of
adamc@271 523 NONE => ds
adamc@271 524 | SOME s => (Source.DDatabase s, ErrorMsg.dummySpan) :: ds
adamc@201 525 end handle Empty => ds
adamc@201 526 end,
adamc@201 527 print = SourcePrint.p_file
adamc@201 528 }
adamc@56 529
adamc@270 530 val toParse = transform parse "parse" o toParseJob
adamc@38 531
adamc@378 532 fun libFile s = OS.Path.joinDirFile {dir = Config.libUr,
adamc@378 533 file = s}
adamc@378 534 fun clibFile s = OS.Path.joinDirFile {dir = Config.libC,
adamc@378 535 file = s}
adamc@378 536
adamc@201 537 val elaborate = {
adamc@201 538 func = fn file => let
adamc@378 539 val basis = #func parseUrs (libFile "basis.urs")
adamc@378 540 val topSgn = #func parseUrs (libFile "top.urs")
adamc@378 541 val topStr = #func parseUr (libFile "top.ur")
adamc@201 542 in
adamc@325 543 Elaborate.elabFile basis topStr topSgn ElabEnv.empty file
adamc@201 544 end,
adamc@201 545 print = ElabPrint.p_file ElabEnv.empty
adamc@201 546 }
adamc@5 547
adamc@270 548 val toElaborate = transform elaborate "elaborate" o toParse
adamc@201 549
adamc@448 550 val unnest = {
adamc@448 551 func = Unnest.unnest,
adamc@448 552 print = ElabPrint.p_file ElabEnv.empty
adamc@448 553 }
adamc@448 554
adamc@448 555 val toUnnest = transform unnest "unnest" o toElaborate
adamc@448 556
adamc@313 557 val termination = {
adamc@313 558 func = (fn file => (Termination.check file; file)),
adamc@313 559 print = ElabPrint.p_file ElabEnv.empty
adamc@313 560 }
adamc@313 561
adamc@448 562 val toTermination = transform termination "termination" o toUnnest
adamc@313 563
adamc@201 564 val explify = {
adamc@201 565 func = Explify.explify,
adamc@201 566 print = ExplPrint.p_file ExplEnv.empty
adamc@201 567 }
adamc@201 568
adamc@625 569 val toExplify = transform explify "explify" o toUnnest
adamc@201 570
adamc@201 571 val corify = {
adamc@201 572 func = Corify.corify,
adamc@201 573 print = CorePrint.p_file CoreEnv.empty
adamc@201 574 }
adamc@201 575
adamc@270 576 val toCorify = transform corify "corify" o toExplify
adamc@201 577
adamc@482 578 (*val reduce_local = {
adamc@482 579 func = ReduceLocal.reduce,
adamc@482 580 print = CorePrint.p_file CoreEnv.empty
adamc@482 581 }
adamc@482 582
adamc@482 583 val toReduce_local = transform reduce_local "reduce_local" o toCorify*)
adamc@482 584
adamc@443 585 val especialize = {
adamc@443 586 func = ESpecialize.specialize,
adamc@443 587 print = CorePrint.p_file CoreEnv.empty
adamc@443 588 }
adamc@443 589
adamc@443 590 val toEspecialize = transform especialize "especialize" o toCorify
adamc@443 591
adamc@454 592 val core_untangle = {
adamc@454 593 func = CoreUntangle.untangle,
adamc@454 594 print = CorePrint.p_file CoreEnv.empty
adamc@454 595 }
adamc@454 596
adamc@454 597 val toCore_untangle = transform core_untangle "core_untangle" o toEspecialize
adamc@454 598
adamc@202 599 val shake = {
adamc@202 600 func = Shake.shake,
adamc@202 601 print = CorePrint.p_file CoreEnv.empty
adamc@202 602 }
adamc@39 603
adamc@454 604 val toShake1 = transform shake "shake1" o toCore_untangle
adamc@110 605
adamc@607 606 val rpcify = {
adamc@607 607 func = Rpcify.frob,
adamc@607 608 print = CorePrint.p_file CoreEnv.empty
adamc@607 609 }
adamc@607 610
adamc@607 611 val toRpcify = transform rpcify "rpcify" o toShake1
adamc@607 612
adamc@642 613 val toCore_untangle2 = transform core_untangle "core_untangle2" o toRpcify
adamc@642 614 val toShake2 = transform shake "shake2" o toCore_untangle2
adamc@642 615
adamc@202 616 val tag = {
adamc@202 617 func = Tag.tag,
adamc@202 618 print = CorePrint.p_file CoreEnv.empty
adamc@202 619 }
adamc@193 620
adamc@642 621 val toTag = transform tag "tag" o toCore_untangle2
adamc@20 622
adamc@202 623 val reduce = {
adamc@202 624 func = Reduce.reduce,
adamc@202 625 print = CorePrint.p_file CoreEnv.empty
adamc@202 626 }
adamc@25 627
adamc@692 628 val toReduce = transform reduce "reduce" o toTag
adamc@23 629
adamc@315 630 val unpoly = {
adamc@315 631 func = Unpoly.unpoly,
adamc@315 632 print = CorePrint.p_file CoreEnv.empty
adamc@315 633 }
adamc@315 634
adamc@315 635 val toUnpoly = transform unpoly "unpoly" o toReduce
adamc@315 636
adamc@202 637 val specialize = {
adamc@202 638 func = Specialize.specialize,
adamc@202 639 print = CorePrint.p_file CoreEnv.empty
adamc@202 640 }
adamc@132 641
adamc@315 642 val toSpecialize = transform specialize "specialize" o toUnpoly
adamc@131 643
adamc@642 644 val toShake3 = transform shake "shake3" o toSpecialize
adamc@133 645
adamc@692 646 val marshalcheck = {
adamc@692 647 func = (fn file => (MarshalCheck.check file; file)),
adamc@692 648 print = CorePrint.p_file CoreEnv.empty
adamc@692 649 }
adamc@692 650
adamc@692 651 val toMarshalcheck = transform marshalcheck "marshalcheck" o toShake3
adamc@692 652
adamc@732 653 val effectize = {
adamc@732 654 func = Effective.effectize,
adamc@732 655 print = CorePrint.p_file CoreEnv.empty
adamc@732 656 }
adamc@732 657
adamc@732 658 val toEffectize = transform effectize "effectize" o toMarshalcheck
adamc@732 659
adamc@202 660 val monoize = {
adamc@202 661 func = Monoize.monoize CoreEnv.empty,
adamc@202 662 print = MonoPrint.p_file MonoEnv.empty
adamc@202 663 }
adamc@134 664
adamc@732 665 val toMonoize = transform monoize "monoize" o toEffectize
adamc@96 666
adamc@202 667 val mono_opt = {
adamc@202 668 func = MonoOpt.optimize,
adamc@202 669 print = MonoPrint.p_file MonoEnv.empty
adamc@202 670 }
adamc@29 671
adamc@270 672 val toMono_opt1 = transform mono_opt "mono_opt1" o toMonoize
adamc@5 673
adamc@202 674 val untangle = {
adamc@202 675 func = Untangle.untangle,
adamc@202 676 print = MonoPrint.p_file MonoEnv.empty
adamc@202 677 }
adamc@1 678
adamc@270 679 val toUntangle = transform untangle "untangle" o toMono_opt1
adamc@38 680
adamc@202 681 val mono_reduce = {
adamc@202 682 func = MonoReduce.reduce,
adamc@202 683 print = MonoPrint.p_file MonoEnv.empty
adamc@202 684 }
adamc@16 685
adamc@270 686 val toMono_reduce = transform mono_reduce "mono_reduce" o toUntangle
adamc@39 687
adamc@202 688 val mono_shake = {
adamc@202 689 func = MonoShake.shake,
adamc@202 690 print = MonoPrint.p_file MonoEnv.empty
adamc@202 691 }
adamc@110 692
adamc@270 693 val toMono_shake = transform mono_shake "mono_shake1" o toMono_reduce
adamc@193 694
adamc@572 695 val toMono_opt2 = transform mono_opt "mono_opt2" o toMono_shake
adamc@572 696
adamc@567 697 val jscomp = {
adamc@567 698 func = JsComp.process,
adamc@567 699 print = MonoPrint.p_file MonoEnv.empty
adamc@567 700 }
adamc@567 701
adamc@572 702 val toJscomp = transform jscomp "jscomp" o toMono_opt2
adamc@567 703
adamc@572 704 val toMono_opt3 = transform mono_opt "mono_opt3" o toJscomp
adamc@20 705
adamc@506 706 val fuse = {
adamc@506 707 func = Fuse.fuse,
adamc@506 708 print = MonoPrint.p_file MonoEnv.empty
adamc@506 709 }
adamc@506 710
adamc@572 711 val toFuse = transform fuse "fuse" o toMono_opt3
adamc@506 712
adamc@506 713 val toUntangle2 = transform untangle "untangle2" o toFuse
adamc@506 714
adamc@601 715 val toMono_reduce2 = transform mono_reduce "mono_reduce2" o toUntangle2
adamc@601 716 val toMono_shake2 = transform mono_shake "mono_shake2" o toMono_reduce2
adamc@506 717
adamc@377 718 val pathcheck = {
adamc@377 719 func = (fn file => (PathCheck.check file; file)),
adamc@377 720 print = MonoPrint.p_file MonoEnv.empty
adamc@377 721 }
adamc@377 722
adamc@506 723 val toPathcheck = transform pathcheck "pathcheck" o toMono_shake2
adamc@377 724
adamc@202 725 val cjrize = {
adamc@202 726 func = Cjrize.cjrize,
adamc@202 727 print = CjrPrint.p_file CjrEnv.empty
adamc@202 728 }
adamc@23 729
adamc@377 730 val toCjrize = transform cjrize "cjrize" o toPathcheck
adamc@29 731
adamc@643 732 val scriptcheck = {
adamc@643 733 func = ScriptCheck.classify,
adamc@643 734 print = CjrPrint.p_file CjrEnv.empty
adamc@643 735 }
adamc@643 736
adamc@643 737 val toScriptcheck = transform scriptcheck "scriptcheck" o toCjrize
adamc@643 738
adamc@282 739 val prepare = {
adamc@282 740 func = Prepare.prepare,
adamc@282 741 print = CjrPrint.p_file CjrEnv.empty
adamc@282 742 }
adamc@282 743
adamc@643 744 val toPrepare = transform prepare "prepare" o toScriptcheck
adamc@282 745
adamc@274 746 val sqlify = {
adamc@274 747 func = Cjrize.cjrize,
adamc@274 748 print = CjrPrint.p_sql CjrEnv.empty
adamc@274 749 }
adamc@274 750
adamc@274 751 val toSqlify = transform sqlify "sqlify" o toMono_opt2
adamc@274 752
adamc@764 753 fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
adamc@183 754 let
adamc@378 755 val urweb_o = clibFile "urweb.o"
adamc@378 756 val driver_o = clibFile "driver.o"
adamc@378 757
adamc@435 758 val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@734 759 val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
adamc@502 760
adamc@502 761 val (compile, link) =
adamc@502 762 if profile then
adamc@502 763 (compile ^ " -pg", link ^ " -pg")
adamc@502 764 else
adamc@502 765 (compile, link)
adamc@742 766
adamc@742 767 val (compile, link) =
adamc@742 768 if debug then
adamc@742 769 (compile ^ " -g", link ^ " -g")
adamc@742 770 else
adamc@742 771 (compile, link)
adamc@764 772
adamc@764 773 val link = foldl (fn (s, link) => link ^ " " ^ s) link link'
adamc@183 774 in
adamc@183 775 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@183 776 print "C compilation failed\n"
adamc@183 777 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@186 778 print "C linking failed\n"
adamc@183 779 else
adamc@673 780 ()
adamc@183 781 end
adamc@183 782
adamc@202 783 fun compile job =
adamc@282 784 case run toPrepare job of
adamc@244 785 NONE => print "Ur compilation failed\n"
adamc@29 786 | SOME file =>
adamc@202 787 let
adamc@274 788 val job = valOf (run (transform parseUrp "parseUrp") job)
adamc@102 789
adamc@274 790 val (cname, oname, cleanup) =
adamc@274 791 if #debug job then
adamc@457 792 ("/tmp/webapp.c", "/tmp/webapp.o", fn () => ())
adamc@274 793 else
adamc@274 794 let
adamc@274 795 val dir = OS.FileSys.tmpName ()
adamc@403 796 val () = if OS.FileSys.access (dir, []) then
adamc@403 797 OS.FileSys.remove dir
adamc@403 798 else
adamc@403 799 ()
adamc@457 800 val cname = OS.Path.joinDirFile {dir = dir, file = "webapp.c"}
adamc@457 801 val oname = OS.Path.joinDirFile {dir = dir, file = "webapp.o"}
adamc@274 802 in
adamc@274 803 OS.FileSys.mkDir dir;
adamc@274 804 (cname, oname,
adamc@274 805 fn () => (OS.FileSys.remove cname;
adamc@274 806 OS.FileSys.remove oname;
adamc@473 807 OS.FileSys.rmDir dir)
adamc@473 808 handle OS.SysErr _ => OS.FileSys.rmDir dir)
adamc@274 809 end
adamc@274 810 val ename = #exe job
adamc@202 811 in
adamc@274 812 let
adamc@274 813 val outf = TextIO.openOut cname
adamc@274 814 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@432 815
adamc@432 816 val hasDb = List.exists (fn (Cjr.DDatabase _, _) => true | _ => false) (#1 file)
adamc@432 817 val libs =
adamc@432 818 if hasDb then
adamc@432 819 "-lpq"
adamc@432 820 else
adamc@432 821 ""
adamc@274 822 in
adamc@274 823 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@415 824 TextIO.output1 (outf, #"\n");
adamc@274 825 TextIO.closeOut outf;
adamc@102 826
adamc@274 827 case #sql job of
adamc@274 828 NONE => ()
adamc@274 829 | SOME sql =>
adamc@274 830 let
adamc@274 831 val outf = TextIO.openOut sql
adamc@274 832 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@274 833 in
adamc@274 834 Print.fprint s (CjrPrint.p_sql CjrEnv.empty file);
adamc@274 835 TextIO.closeOut outf
adamc@274 836 end;
adamc@274 837
adamc@742 838 compileC {cname = cname, oname = oname, ename = ename, libs = libs,
adamc@764 839 profile = #profile job, debug = #debug job, link = #link job};
adamc@274 840
adamc@274 841 cleanup ()
adamc@274 842 end
adamc@274 843 handle ex => (((cleanup ()) handle _ => ()); raise ex)
adamc@202 844 end
adamc@29 845
adamc@1 846 end