annotate src/compiler.sml @ 66:1ec5703c09c4

Proper subsignaturing for sub-structures
author Adam Chlipala <adamc@hcoop.net>
date Thu, 26 Jun 2008 09:09:30 -0400
parents d3cc191cb25f
children 82aaa1c406d3
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@55 54 ErrorMsg.resetPositioning filename)
adamc@55 55 val file = TextIO.openIn fname
adamc@55 56 fun get _ = TextIO.input file
adamc@55 57 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@55 58 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@55 59 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@55 60 in
adamc@55 61 TextIO.closeIn file;
adamc@55 62 if ErrorMsg.anyErrors () then
adamc@55 63 NONE
adamc@55 64 else
adamc@55 65 case absyn of
adamc@55 66 [(Source.DSgn ("?", (Source.SgnConst sgis, _)), _)] => SOME sgis
adamc@55 67 | _ => NONE
adamc@55 68 end
adamc@55 69 handle LrParser.ParseError => NONE
adamc@55 70
adamc@55 71 fun testLig fname =
adamc@55 72 case parseLig fname of
adamc@55 73 NONE => ()
adamc@55 74 | SOME sgis =>
adamc@55 75 app (fn sgi => (Print.print (SourcePrint.p_sgn_item sgi);
adamc@55 76 print "\n")) sgis
adamc@55 77
adamc@1 78 (* The main parsing routine *)
adamc@56 79 fun parseLac filename =
adamc@1 80 let
adamc@1 81 val () = (ErrorMsg.resetErrors ();
adamc@1 82 ErrorMsg.resetPositioning filename)
adamc@1 83 val file = TextIO.openIn filename
adamc@1 84 fun get _ = TextIO.input file
adamc@1 85 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@1 86 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@1 87 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@1 88 in
adamc@1 89 TextIO.closeIn file;
adamc@5 90 if ErrorMsg.anyErrors () then
adamc@5 91 NONE
adamc@5 92 else
adamc@55 93 case absyn of
adamc@55 94 [(Source.DSgn ("?", _), _)] =>
adamc@55 95 (ErrorMsg.error "File starts with 'sig'";
adamc@55 96 NONE)
adamc@55 97 | _ => SOME absyn
adamc@1 98 end
adamc@1 99 handle LrParser.ParseError => NONE
adamc@1 100
adamc@56 101 fun testLac fname =
adamc@56 102 case parseLac fname of
adamc@56 103 NONE => ()
adamc@56 104 | SOME file => (Print.print (SourcePrint.p_file file);
adamc@56 105 print "\n")
adamc@56 106
adamc@56 107 type job = string list
adamc@56 108
adamc@56 109 fun capitalize "" = ""
adamc@56 110 | capitalize s = str (Char.toUpper (String.sub (s, 0))) ^ String.extract (s, 1, NONE)
adamc@56 111
adamc@56 112 fun parse fnames =
adamc@56 113 let
adamc@56 114 fun parseOne fname =
adamc@56 115 let
adamc@56 116 val mname = capitalize (OS.Path.file fname)
adamc@56 117 val lac = OS.Path.joinBaseExt {base = fname, ext = SOME "lac"}
adamc@56 118 val lig = OS.Path.joinBaseExt {base = fname, ext = SOME "lig"}
adamc@56 119
adamc@56 120 val sgnO =
adamc@56 121 if Posix.FileSys.access (lig, []) then
adamc@56 122 case parseLig lig of
adamc@56 123 NONE => NONE
adamc@56 124 | SOME sgis => SOME (Source.SgnConst sgis, {file = lig,
adamc@56 125 first = ErrorMsg.dummyPos,
adamc@56 126 last = ErrorMsg.dummyPos})
adamc@56 127 else
adamc@56 128 NONE
adamc@56 129
adamc@56 130 val loc = {file = lac,
adamc@56 131 first = ErrorMsg.dummyPos,
adamc@56 132 last = ErrorMsg.dummyPos}
adamc@56 133 in
adamc@56 134 case parseLac lac of
adamc@56 135 NONE => NONE
adamc@56 136 | SOME ds =>
adamc@56 137 SOME (Source.DStr (mname, sgnO, (Source.StrConst ds, loc)), loc)
adamc@56 138 end
adamc@56 139
adamc@56 140 val ds = List.mapPartial parseOne fnames
adamc@56 141 in
adamc@56 142 if ErrorMsg.anyErrors () then
adamc@56 143 NONE
adamc@56 144 else
adamc@56 145 SOME ds
adamc@56 146 end
adamc@56 147
adamc@56 148 fun elaborate job =
adamc@56 149 case parseLig "lib/basis.lig" of
adamc@56 150 NONE => NONE
adamc@56 151 | SOME empty =>
adamc@56 152 case parse job of
adamc@56 153 NONE => NONE
adamc@56 154 | SOME file =>
adamc@56 155 let
adamc@56 156 val out = Elaborate.elabFile empty ElabEnv.empty file
adamc@56 157 in
adamc@56 158 if ErrorMsg.anyErrors () then
adamc@56 159 NONE
adamc@56 160 else
adamc@56 161 SOME out
adamc@56 162 end
adamc@56 163
adamc@56 164 fun explify job =
adamc@56 165 case elaborate job of
adamc@5 166 NONE => NONE
adamc@5 167 | SOME file =>
adamc@38 168 if ErrorMsg.anyErrors () then
adamc@38 169 NONE
adamc@38 170 else
adamc@38 171 SOME (Explify.explify file)
adamc@38 172
adamc@56 173 fun corify job =
adamc@56 174 case explify job of
adamc@16 175 NONE => NONE
adamc@39 176 | SOME file =>
adamc@25 177 if ErrorMsg.anyErrors () then
adamc@25 178 NONE
adamc@25 179 else
adamc@25 180 SOME (Corify.corify file)
adamc@5 181
adamc@56 182 fun shake' job =
adamc@56 183 case corify job of
adamc@39 184 NONE => NONE
adamc@39 185 | SOME file =>
adamc@39 186 if ErrorMsg.anyErrors () then
adamc@39 187 NONE
adamc@39 188 else
adamc@39 189 SOME (Shake.shake file)
adamc@39 190
adamc@56 191 fun reduce job =
adamc@56 192 case corify job of
adamc@20 193 NONE => NONE
adamc@25 194 | SOME file =>
adamc@25 195 if ErrorMsg.anyErrors () then
adamc@25 196 NONE
adamc@25 197 else
adamc@25 198 SOME (Reduce.reduce (Shake.shake file))
adamc@20 199
adamc@56 200 fun shake job =
adamc@56 201 case reduce job of
adamc@23 202 NONE => NONE
adamc@25 203 | SOME file =>
adamc@25 204 if ErrorMsg.anyErrors () then
adamc@25 205 NONE
adamc@25 206 else
adamc@25 207 SOME (Shake.shake file)
adamc@25 208
adamc@56 209 fun monoize job =
adamc@56 210 case shake job of
adamc@25 211 NONE => NONE
adamc@25 212 | SOME file =>
adamc@25 213 if ErrorMsg.anyErrors () then
adamc@25 214 NONE
adamc@25 215 else
adamc@56 216 SOME (Monoize.monoize CoreEnv.empty file)
adamc@23 217
adamc@56 218 fun cloconv job =
adamc@56 219 case monoize job of
adamc@26 220 NONE => NONE
adamc@26 221 | SOME file =>
adamc@26 222 if ErrorMsg.anyErrors () then
adamc@26 223 NONE
adamc@26 224 else
adamc@26 225 SOME (Cloconv.cloconv file)
adamc@26 226
adamc@56 227 fun cjrize job =
adamc@56 228 case cloconv job of
adamc@29 229 NONE => NONE
adamc@29 230 | SOME file =>
adamc@29 231 if ErrorMsg.anyErrors () then
adamc@29 232 NONE
adamc@29 233 else
adamc@29 234 SOME (Cjrize.cjrize file)
adamc@29 235
adamc@56 236 fun testParse job =
adamc@56 237 case parse job of
adamc@5 238 NONE => print "Failed\n"
adamc@1 239 | SOME file =>
adamc@5 240 (Print.print (SourcePrint.p_file file);
adamc@5 241 print "\n")
adamc@5 242
adamc@56 243 fun testElaborate job =
adamc@56 244 (case elaborate job of
adamc@5 245 NONE => print "Failed\n"
adamc@56 246 | SOME file =>
adamc@32 247 (print "Succeeded\n";
adamc@56 248 Print.print (ElabPrint.p_file ElabEnv.empty file);
adamc@5 249 print "\n"))
adamc@5 250 handle ElabEnv.UnboundNamed n =>
adamc@5 251 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@1 252
adamc@56 253 fun testExplify job =
adamc@56 254 (case explify job of
adamc@38 255 NONE => print "Failed\n"
adamc@38 256 | SOME file =>
adamc@56 257 (Print.print (ExplPrint.p_file ExplEnv.empty file);
adamc@38 258 print "\n"))
adamc@38 259 handle ExplEnv.UnboundNamed n =>
adamc@38 260 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@38 261
adamc@56 262 fun testCorify job =
adamc@56 263 (case corify job of
adamc@16 264 NONE => print "Failed\n"
adamc@16 265 | SOME file =>
adamc@56 266 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@16 267 print "\n"))
adamc@16 268 handle CoreEnv.UnboundNamed n =>
adamc@16 269 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@16 270
adamc@56 271 fun testShake' job =
adamc@56 272 (case shake' job of
adamc@39 273 NONE => print "Failed\n"
adamc@39 274 | SOME file =>
adamc@56 275 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@39 276 print "\n"))
adamc@39 277 handle CoreEnv.UnboundNamed n =>
adamc@39 278 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@39 279
adamc@56 280 fun testReduce job =
adamc@56 281 (case reduce job of
adamc@20 282 NONE => print "Failed\n"
adamc@20 283 | SOME file =>
adamc@56 284 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@20 285 print "\n"))
adamc@20 286 handle CoreEnv.UnboundNamed n =>
adamc@20 287 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@20 288
adamc@56 289 fun testShake job =
adamc@56 290 (case shake job of
adamc@23 291 NONE => print "Failed\n"
adamc@23 292 | SOME file =>
adamc@56 293 (Print.print (CorePrint.p_file CoreEnv.empty file);
adamc@23 294 print "\n"))
adamc@23 295 handle CoreEnv.UnboundNamed n =>
adamc@23 296 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@23 297
adamc@56 298 fun testMonoize job =
adamc@56 299 (case monoize job of
adamc@25 300 NONE => print "Failed\n"
adamc@25 301 | SOME file =>
adamc@56 302 (Print.print (MonoPrint.p_file MonoEnv.empty file);
adamc@25 303 print "\n"))
adamc@25 304 handle MonoEnv.UnboundNamed n =>
adamc@25 305 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@25 306
adamc@56 307 fun testCloconv job =
adamc@56 308 (case cloconv job of
adamc@26 309 NONE => print "Failed\n"
adamc@26 310 | SOME file =>
adamc@56 311 (Print.print (FlatPrint.p_file FlatEnv.empty file);
adamc@26 312 print "\n"))
adamc@26 313 handle FlatEnv.UnboundNamed n =>
adamc@26 314 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@26 315
adamc@56 316 fun testCjrize job =
adamc@56 317 (case cjrize job of
adamc@29 318 NONE => print "Failed\n"
adamc@29 319 | SOME file =>
adamc@56 320 (Print.print (CjrPrint.p_file CjrEnv.empty file);
adamc@29 321 print "\n"))
adamc@29 322 handle CjrEnv.UnboundNamed n =>
adamc@29 323 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@29 324
adamc@56 325 fun compile job =
adamc@56 326 case cjrize job of
adamc@29 327 NONE => ()
adamc@29 328 | SOME file =>
adamc@29 329 let
adamc@29 330 val outf = TextIO.openOut "/tmp/lacweb.c"
adamc@29 331 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@29 332 in
adamc@56 333 Print.fprint s (CjrPrint.p_file CjrEnv.empty file);
adamc@29 334 TextIO.closeOut outf
adamc@29 335 end
adamc@29 336
adamc@1 337 end