annotate src/compiler.sml @ 155:4334bb734187

Password inputs
author Adam Chlipala <adamc@hcoop.net>
date Thu, 24 Jul 2008 11:32:01 -0400
parents d6d78055f001
children 80192edca30d
rev   line source
adamc@1 1 (* Copyright (c) 2008, 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 (* Laconic/Web language parser *)
adamc@1 29
adamc@1 30 structure Compiler :> COMPILER = struct
adamc@1 31
adamc@1 32 structure LacwebLrVals = LacwebLrValsFn(structure Token = LrParser.Token)
adamc@1 33 structure Lex = LacwebLexFn(structure Tokens = LacwebLrVals.Tokens)
adamc@1 34 structure LacwebP = Join(structure ParserData = LacwebLrVals.ParserData
adamc@1 35 structure Lex = Lex
adamc@1 36 structure LrParser = LrParser)
adamc@1 37
adamc@55 38 fun parseLig filename =
adamc@55 39 let
adamc@55 40 val fname = OS.FileSys.tmpName ()
adamc@55 41 val outf = TextIO.openOut fname
adamc@55 42 val () = TextIO.output (outf, "sig\n")
adamc@55 43 val inf = TextIO.openIn filename
adamc@55 44 fun loop () =
adamc@55 45 case TextIO.inputLine inf of
adamc@55 46 NONE => ()
adamc@55 47 | SOME line => (TextIO.output (outf, line);
adamc@55 48 loop ())
adamc@55 49 val () = loop ()
adamc@55 50 val () = TextIO.closeIn inf
adamc@55 51 val () = TextIO.closeOut outf
adamc@55 52
adamc@55 53 val () = (ErrorMsg.resetErrors ();
adamc@135 54 ErrorMsg.resetPositioning filename;
adamc@135 55 Lex.UserDeclarations.initialize ())
adamc@55 56 val file = TextIO.openIn fname
adamc@55 57 fun get _ = TextIO.input file
adamc@55 58 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@55 59 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@55 60 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@55 61 in
adamc@55 62 TextIO.closeIn file;
adamc@55 63 if ErrorMsg.anyErrors () then
adamc@55 64 NONE
adamc@55 65 else
adamc@55 66 case absyn of
adamc@55 67 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => SOME sgis
adamc@55 68 | _ => NONE
adamc@55 69 end
adamc@55 70 handle LrParser.ParseError => NONE
adamc@55 71
adamc@55 72 fun testLig fname =
adamc@55 73 case parseLig fname of
adamc@55 74 NONE => ()
adamc@55 75 | SOME sgis =>
adamc@55 76 app (fn sgi => (Print.print (SourcePrint.p_sgn_item sgi);
adamc@55 77 print "\n")) sgis
adamc@55 78
adamc@1 79 (* The main parsing routine *)
adamc@56 80 fun parseLac filename =
adamc@1 81 let
adamc@1 82 val () = (ErrorMsg.resetErrors ();
adamc@135 83 ErrorMsg.resetPositioning filename;
adamc@135 84 Lex.UserDeclarations.initialize ())
adamc@1 85 val file = TextIO.openIn filename
adamc@1 86 fun get _ = TextIO.input file
adamc@1 87 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@1 88 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@1 89 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@1 90 in
adamc@1 91 TextIO.closeIn file;
adamc@5 92 if ErrorMsg.anyErrors () then
adamc@5 93 NONE
adamc@5 94 else
adamc@55 95 case absyn of
adamc@55 96 [(Source.DSgn ("?", _), _)] =>
adamc@55 97 (ErrorMsg.error "File starts with 'sig'";
adamc@55 98 NONE)
adamc@55 99 | _ => SOME absyn
adamc@1 100 end
adamc@1 101 handle LrParser.ParseError => NONE
adamc@1 102
adamc@56 103 fun testLac fname =
adamc@56 104 case parseLac fname of
adamc@56 105 NONE => ()
adamc@56 106 | SOME file => (Print.print (SourcePrint.p_file file);
adamc@56 107 print "\n")
adamc@56 108
adamc@56 109 type job = string list
adamc@56 110
adamc@56 111 fun capitalize "" = ""
adamc@56 112 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 113
adamc@56 114 fun parse fnames =
adamc@56 115 let
adamc@109 116 fun nameOf fname = capitalize (OS.Path.file fname)
adamc@109 117
adamc@56 118 fun parseOne fname =
adamc@56 119 let
adamc@109 120 val mname = nameOf fname
adamc@56 121 val lac = OS.Path.joinBaseExt {base = fname, ext = SOME "lac"}
adamc@56 122 val lig = OS.Path.joinBaseExt {base = fname, ext = SOME "lig"}
adamc@56 123
adamc@56 124 val sgnO =
adamc@56 125 if Posix.FileSys.access (lig, []) then
adamc@56 126 case parseLig lig of
adamc@56 127 NONE => NONE
adamc@56 128 | SOME sgis => SOME (Source.SgnConst sgis, {file = lig,
adamc@56 129 first = ErrorMsg.dummyPos,
adamc@56 130 last = ErrorMsg.dummyPos})
adamc@56 131 else
adamc@56 132 NONE
adamc@56 133
adamc@56 134 val loc = {file = lac,
adamc@56 135 first = ErrorMsg.dummyPos,
adamc@56 136 last = ErrorMsg.dummyPos}
adamc@56 137 in
adamc@56 138 case parseLac lac of
adamc@56 139 NONE => NONE
adamc@56 140 | SOME ds =>
adamc@56 141 SOME (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@56 142 end
adamc@56 143
adamc@109 144 val ds = List.mapPartial parseOne fnames
adamc@109 145 val ds =
adamc@109 146 let
adamc@109 147 val final = nameOf (List.last fnames)
adamc@109 148 in
adamc@109 149 ds @ [(Source.DExport (Source.StrVar final, ErrorMsg.dummySpan), ErrorMsg.dummySpan)]
adamc@109 150 end handle Empty => ds
adamc@56 151 in
adamc@56 152 if ErrorMsg.anyErrors () then
adamc@56 153 NONE
adamc@56 154 else
adamc@56 155 SOME ds
adamc@56 156 end
adamc@56 157
adamc@56 158 fun elaborate job =
adamc@56 159 case parseLig "lib/basis.lig" of
adamc@56 160 NONE => NONE
adamc@56 161 | SOME empty =>
adamc@56 162 case parse job of
adamc@56 163 NONE => NONE
adamc@56 164 | SOME file =>
adamc@56 165 let
adamc@56 166 val out = Elaborate.elabFile empty ElabEnv.empty file
adamc@56 167 in
adamc@56 168 if ErrorMsg.anyErrors () then
adamc@56 169 NONE
adamc@56 170 else
adamc@56 171 SOME out
adamc@56 172 end
adamc@56 173
adamc@56 174 fun explify job =
adamc@56 175 case elaborate job of
adamc@5 176 NONE => NONE
adamc@5 177 | SOME file =>
adamc@38 178 if ErrorMsg.anyErrors () then
adamc@38 179 NONE
adamc@38 180 else
adamc@38 181 SOME (Explify.explify file)
adamc@38 182
adamc@56 183 fun corify job =
adamc@56 184 case explify job of
adamc@16 185 NONE => NONE
adamc@39 186 | SOME file =>
adamc@25 187 if ErrorMsg.anyErrors () then
adamc@25 188 NONE
adamc@25 189 else
adamc@25 190 SOME (Corify.corify file)
adamc@5 191
adamc@56 192 fun shake' job =
adamc@56 193 case corify job of
adamc@39 194 NONE => NONE
adamc@39 195 | SOME file =>
adamc@39 196 if ErrorMsg.anyErrors () then
adamc@39 197 NONE
adamc@39 198 else
adamc@39 199 SOME (Shake.shake file)
adamc@39 200
adamc@110 201 fun tag job =
adamc@110 202 case shake' job of
adamc@110 203 NONE => NONE
adamc@110 204 | SOME file =>
adamc@110 205 if ErrorMsg.anyErrors () then
adamc@110 206 NONE
adamc@110 207 else
adamc@110 208 SOME (Tag.tag file)
adamc@110 209
adamc@56 210 fun reduce job =
adamc@110 211 case tag job of
adamc@20 212 NONE => NONE
adamc@25 213 | SOME file =>
adamc@25 214 if ErrorMsg.anyErrors () then
adamc@25 215 NONE
adamc@25 216 else
adamc@25 217 SOME (Reduce.reduce (Shake.shake file))
adamc@20 218
adamc@56 219 fun shake job =
adamc@56 220 case reduce job of
adamc@23 221 NONE => NONE
adamc@25 222 | SOME file =>
adamc@25 223 if ErrorMsg.anyErrors () then
adamc@25 224 NONE
adamc@25 225 else
adamc@25 226 SOME (Shake.shake file)
adamc@25 227
adamc@56 228 fun monoize job =
adamc@56 229 case shake job of
adamc@25 230 NONE => NONE
adamc@25 231 | SOME file =>
adamc@25 232 if ErrorMsg.anyErrors () then
adamc@25 233 NONE
adamc@25 234 else
adamc@56 235 SOME (Monoize.monoize CoreEnv.empty file)
adamc@23 236
adamc@132 237 fun mono_opt' job =
adamc@132 238 case monoize job of
adamc@132 239 NONE => NONE
adamc@132 240 | SOME file =>
adamc@132 241 if ErrorMsg.anyErrors () then
adamc@132 242 NONE
adamc@132 243 else
adamc@132 244 SOME (MonoOpt.optimize file)
adamc@132 245
adamc@131 246 fun untangle job =
adamc@132 247 case mono_opt' job of
adamc@131 248 NONE => NONE
adamc@131 249 | SOME file =>
adamc@131 250 if ErrorMsg.anyErrors () then
adamc@131 251 NONE
adamc@131 252 else
adamc@131 253 SOME (Untangle.untangle file)
adamc@131 254
adamc@133 255 fun mono_reduce job =
adamc@133 256 case untangle job of
adamc@133 257 NONE => NONE
adamc@133 258 | SOME file =>
adamc@133 259 if ErrorMsg.anyErrors () then
adamc@133 260 NONE
adamc@133 261 else
adamc@133 262 SOME (MonoReduce.reduce file)
adamc@133 263
adamc@134 264 fun mono_shake job =
adamc@134 265 case mono_reduce job of
adamc@134 266 NONE => NONE
adamc@134 267 | SOME file =>
adamc@134 268 if ErrorMsg.anyErrors () then
adamc@134 269 NONE
adamc@134 270 else
adamc@134 271 SOME (MonoShake.shake file)
adamc@134 272
adamc@96 273 fun mono_opt job =
adamc@134 274 case mono_shake job of
adamc@96 275 NONE => NONE
adamc@96 276 | SOME file =>
adamc@96 277 if ErrorMsg.anyErrors () then
adamc@96 278 NONE
adamc@96 279 else
adamc@96 280 SOME (MonoOpt.optimize file)
adamc@96 281
adamc@109 282 fun cjrize job =
adamc@96 283 case mono_opt job of
adamc@26 284 NONE => NONE
adamc@26 285 | SOME file =>
adamc@26 286 if ErrorMsg.anyErrors () then
adamc@26 287 NONE
adamc@26 288 else
adamc@29 289 SOME (Cjrize.cjrize file)
adamc@29 290
adamc@56 291 fun testParse job =
adamc@56 292 case parse job of
adamc@5 293 NONE => print "Failed\n"
adamc@1 294 | SOME file =>
adamc@5 295 (Print.print (SourcePrint.p_file file);
adamc@5 296 print "\n")
adamc@5 297
adamc@56 298 fun testElaborate job =
adamc@56 299 (case elaborate job of
adamc@5 300 NONE => print "Failed\n"
adamc@56 301 | SOME file =>
adamc@32 302 (print "Succeeded\n";
adamc@56 303 Print.print (ElabPrint.p_file ElabEnv.empty file);
adamc@5 304 print "\n"))
adamc@5 305 handle ElabEnv.UnboundNamed n =>
adamc@5 306 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@1 307
adamc@56 308 fun testExplify job =
adamc@56 309 (case explify job of
adamc@38 310 NONE => print "Failed\n"
adamc@38 311 | SOME file =>
adamc@56 312 (Print.print (ExplPrint.p_file ExplEnv.empty file);
adamc@38 313 print "\n"))
adamc@38 314 handle ExplEnv.UnboundNamed n =>
adamc@38 315 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@38 316
adamc@56 317 fun testCorify job =
adamc@56 318 (case corify job of
adamc@16 319 NONE => print "Failed\n"
adamc@16 320 | SOME file =>
adamc@56 321 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@16 322 print "\n"))
adamc@16 323 handle CoreEnv.UnboundNamed n =>
adamc@16 324 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@16 325
adamc@56 326 fun testShake' job =
adamc@56 327 (case shake' job of
adamc@39 328 NONE => print "Failed\n"
adamc@39 329 | SOME file =>
adamc@56 330 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@39 331 print "\n"))
adamc@39 332 handle CoreEnv.UnboundNamed n =>
adamc@39 333 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@39 334
adamc@110 335 fun testTag job =
adamc@110 336 (case tag job of
adamc@110 337 NONE => print "Failed\n"
adamc@110 338 | SOME file =>
adamc@110 339 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@110 340 print "\n"))
adamc@110 341 handle CoreEnv.UnboundNamed n =>
adamc@110 342 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@110 343
adamc@56 344 fun testReduce job =
adamc@131 345 (case tag job of
adamc@20 346 NONE => print "Failed\n"
adamc@20 347 | SOME file =>
adamc@56 348 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@20 349 print "\n"))
adamc@20 350 handle CoreEnv.UnboundNamed n =>
adamc@20 351 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@20 352
adamc@56 353 fun testShake job =
adamc@56 354 (case shake job of
adamc@23 355 NONE => print "Failed\n"
adamc@23 356 | SOME file =>
adamc@56 357 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@23 358 print "\n"))
adamc@23 359 handle CoreEnv.UnboundNamed n =>
adamc@23 360 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@23 361
adamc@56 362 fun testMonoize job =
adamc@56 363 (case monoize job of
adamc@25 364 NONE => print "Failed\n"
adamc@25 365 | SOME file =>
adamc@56 366 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@25 367 print "\n"))
adamc@25 368 handle MonoEnv.UnboundNamed n =>
adamc@25 369 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@25 370
adamc@132 371 fun testMono_opt' job =
adamc@132 372 (case mono_opt' job of
adamc@132 373 NONE => print "Failed\n"
adamc@132 374 | SOME file =>
adamc@132 375 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@132 376 print "\n"))
adamc@132 377 handle MonoEnv.UnboundNamed n =>
adamc@132 378 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@132 379
adamc@131 380 fun testUntangle job =
adamc@131 381 (case untangle job of
adamc@131 382 NONE => print "Failed\n"
adamc@131 383 | SOME file =>
adamc@131 384 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@131 385 print "\n"))
adamc@131 386 handle MonoEnv.UnboundNamed n =>
adamc@131 387 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@131 388
adamc@133 389 fun testMono_reduce job =
adamc@133 390 (case mono_reduce job of
adamc@133 391 NONE => print "Failed\n"
adamc@133 392 | SOME file =>
adamc@133 393 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@133 394 print "\n"))
adamc@133 395 handle MonoEnv.UnboundNamed n =>
adamc@133 396 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@133 397
adamc@134 398 fun testMono_shake job =
adamc@134 399 (case mono_shake job of
adamc@134 400 NONE => print "Failed\n"
adamc@134 401 | SOME file =>
adamc@134 402 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@134 403 print "\n"))
adamc@134 404 handle MonoEnv.UnboundNamed n =>
adamc@134 405 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@134 406
adamc@96 407 fun testMono_opt job =
adamc@96 408 (case mono_opt job of
adamc@96 409 NONE => print "Failed\n"
adamc@96 410 | SOME file =>
adamc@96 411 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@96 412 print "\n"))
adamc@96 413 handle MonoEnv.UnboundNamed n =>
adamc@96 414 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@96 415
adamc@56 416 fun testCjrize job =
adamc@56 417 (case cjrize job of
adamc@29 418 NONE => print "Failed\n"
adamc@29 419 | SOME file =>
adamc@56 420 (Print.print (CjrPrint.p_file CjrEnv.empty file);
adamc@29 421 print "\n"))
adamc@29 422 handle CjrEnv.UnboundNamed n =>
adamc@29 423 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@29 424
adamc@56 425 fun compile job =
adamc@56 426 case cjrize job of
adamc@114 427 NONE => print "Laconic compilation failed\n"
adamc@29 428 | SOME file =>
adamc@29 429 let
adamc@102 430 val cname = "/tmp/lacweb.c"
adamc@102 431 val oname = "/tmp/lacweb.o"
adamc@102 432 val ename = "/tmp/webapp"
adamc@102 433
adamc@113 434 val compile = "gcc -O3 -I include -c " ^ cname ^ " -o " ^ oname
adamc@138 435 val link = "gcc -pthread -O3 clib/lacweb.o " ^ oname ^ " clib/driver.o -o " ^ ename
adamc@102 436
adamc@102 437 val outf = TextIO.openOut cname
adamc@29 438 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@29 439 in
adamc@56 440 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@102 441 TextIO.closeOut outf;
adamc@102 442
adamc@102 443 if not (OS.Process.isSuccess (OS.Process.system compile)) then
adamc@102 444 print "C compilation failed\n"
adamc@102 445 else if not (OS.Process.isSuccess (OS.Process.system link)) then
adamc@102 446 print "C linking failed\n"
adamc@102 447 else
adamc@102 448 print "Success\n"
adamc@29 449 end
adamc@29 450
adamc@1 451 end