annotate src/compiler.sml @ 38:d16ef24de78b

Explify
author Adam Chlipala <adamc@hcoop.net>
date Thu, 19 Jun 2008 10:06:59 -0400
parents 0ff8c2728634
children 02f42e9a1825
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@1 38 (* The main parsing routine *)
adamc@1 39 fun parse filename =
adamc@1 40 let
adamc@1 41 val () = (ErrorMsg.resetErrors ();
adamc@1 42 ErrorMsg.resetPositioning filename)
adamc@1 43 val file = TextIO.openIn filename
adamc@1 44 fun get _ = TextIO.input file
adamc@1 45 fun parseerror (s, p1, p2) = ErrorMsg.errorAt' (p1, p2) s
adamc@1 46 val lexer = LrParser.Stream.streamify (Lex.makeLexer get)
adamc@1 47 val (absyn, _) = LacwebP.parse (30, lexer, parseerror, ())
adamc@1 48 in
adamc@1 49 TextIO.closeIn file;
adamc@5 50 if ErrorMsg.anyErrors () then
adamc@5 51 NONE
adamc@5 52 else
adamc@5 53 SOME absyn
adamc@1 54 end
adamc@1 55 handle LrParser.ParseError => NONE
adamc@1 56
adamc@5 57 fun elaborate env filename =
adamc@5 58 case parse filename of
adamc@5 59 NONE => NONE
adamc@5 60 | SOME file =>
adamc@5 61 let
adamc@5 62 val out = Elaborate.elabFile env file
adamc@5 63 in
adamc@5 64 if ErrorMsg.anyErrors () then
adamc@5 65 NONE
adamc@5 66 else
adamc@5 67 SOME out
adamc@5 68 end
adamc@16 69
adamc@38 70 fun explify eenv filename =
adamc@38 71 case elaborate eenv filename of
adamc@38 72 NONE => NONE
adamc@38 73 | SOME (file, _) =>
adamc@38 74 if ErrorMsg.anyErrors () then
adamc@38 75 NONE
adamc@38 76 else
adamc@38 77 SOME (Explify.explify file)
adamc@38 78
adamc@38 79 fun corify eenv filename =
adamc@16 80 case elaborate eenv filename of
adamc@16 81 NONE => NONE
adamc@31 82 | SOME (file, _) =>
adamc@25 83 if ErrorMsg.anyErrors () then
adamc@25 84 NONE
adamc@25 85 else
adamc@25 86 SOME (Corify.corify file)
adamc@5 87
adamc@38 88 fun reduce eenv filename =
adamc@38 89 case corify eenv filename of
adamc@20 90 NONE => NONE
adamc@25 91 | SOME file =>
adamc@25 92 if ErrorMsg.anyErrors () then
adamc@25 93 NONE
adamc@25 94 else
adamc@25 95 SOME (Reduce.reduce (Shake.shake file))
adamc@20 96
adamc@38 97 fun shake eenv filename =
adamc@38 98 case reduce eenv filename of
adamc@23 99 NONE => NONE
adamc@25 100 | SOME file =>
adamc@25 101 if ErrorMsg.anyErrors () then
adamc@25 102 NONE
adamc@25 103 else
adamc@25 104 SOME (Shake.shake file)
adamc@25 105
adamc@25 106 fun monoize eenv cenv filename =
adamc@38 107 case shake eenv filename of
adamc@25 108 NONE => NONE
adamc@25 109 | SOME file =>
adamc@25 110 if ErrorMsg.anyErrors () then
adamc@25 111 NONE
adamc@25 112 else
adamc@25 113 SOME (Monoize.monoize cenv file)
adamc@23 114
adamc@26 115 fun cloconv eenv cenv filename =
adamc@26 116 case monoize eenv cenv filename of
adamc@26 117 NONE => NONE
adamc@26 118 | SOME file =>
adamc@26 119 if ErrorMsg.anyErrors () then
adamc@26 120 NONE
adamc@26 121 else
adamc@26 122 SOME (Cloconv.cloconv file)
adamc@26 123
adamc@29 124 fun cjrize eenv cenv filename =
adamc@29 125 case cloconv eenv cenv filename of
adamc@29 126 NONE => NONE
adamc@29 127 | SOME file =>
adamc@29 128 if ErrorMsg.anyErrors () then
adamc@29 129 NONE
adamc@29 130 else
adamc@29 131 SOME (Cjrize.cjrize file)
adamc@29 132
adamc@1 133 fun testParse filename =
adamc@1 134 case parse filename of
adamc@5 135 NONE => print "Failed\n"
adamc@1 136 | SOME file =>
adamc@5 137 (Print.print (SourcePrint.p_file file);
adamc@5 138 print "\n")
adamc@5 139
adamc@5 140 fun testElaborate filename =
adamc@14 141 (case elaborate ElabEnv.basis filename of
adamc@5 142 NONE => print "Failed\n"
adamc@31 143 | SOME (file, _) =>
adamc@32 144 (print "Succeeded\n";
adamc@32 145 Print.print (ElabPrint.p_file ElabEnv.basis file);
adamc@5 146 print "\n"))
adamc@5 147 handle ElabEnv.UnboundNamed n =>
adamc@5 148 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@1 149
adamc@38 150 fun testExplify filename =
adamc@38 151 (case explify ElabEnv.basis filename of
adamc@38 152 NONE => print "Failed\n"
adamc@38 153 | SOME file =>
adamc@38 154 (Print.print (ExplPrint.p_file ExplEnv.basis file);
adamc@38 155 print "\n"))
adamc@38 156 handle ExplEnv.UnboundNamed n =>
adamc@38 157 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@38 158
adamc@16 159 fun testCorify filename =
adamc@38 160 (case corify ElabEnv.basis filename of
adamc@16 161 NONE => print "Failed\n"
adamc@16 162 | SOME file =>
adamc@16 163 (Print.print (CorePrint.p_file CoreEnv.basis file);
adamc@16 164 print "\n"))
adamc@16 165 handle CoreEnv.UnboundNamed n =>
adamc@16 166 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@16 167
adamc@20 168 fun testReduce filename =
adamc@38 169 (case reduce ElabEnv.basis filename of
adamc@20 170 NONE => print "Failed\n"
adamc@20 171 | SOME file =>
adamc@20 172 (Print.print (CorePrint.p_file CoreEnv.basis file);
adamc@20 173 print "\n"))
adamc@20 174 handle CoreEnv.UnboundNamed n =>
adamc@20 175 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@20 176
adamc@23 177 fun testShake filename =
adamc@38 178 (case shake ElabEnv.basis filename of
adamc@23 179 NONE => print "Failed\n"
adamc@23 180 | SOME file =>
adamc@23 181 (Print.print (CorePrint.p_file CoreEnv.basis file);
adamc@23 182 print "\n"))
adamc@23 183 handle CoreEnv.UnboundNamed n =>
adamc@23 184 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@23 185
adamc@25 186 fun testMonoize filename =
adamc@25 187 (case monoize ElabEnv.basis CoreEnv.basis filename of
adamc@25 188 NONE => print "Failed\n"
adamc@25 189 | SOME file =>
adamc@25 190 (Print.print (MonoPrint.p_file MonoEnv.basis file);
adamc@25 191 print "\n"))
adamc@25 192 handle MonoEnv.UnboundNamed n =>
adamc@25 193 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@25 194
adamc@26 195 fun testCloconv filename =
adamc@26 196 (case cloconv ElabEnv.basis CoreEnv.basis filename of
adamc@26 197 NONE => print "Failed\n"
adamc@26 198 | SOME file =>
adamc@26 199 (Print.print (FlatPrint.p_file FlatEnv.basis file);
adamc@26 200 print "\n"))
adamc@26 201 handle FlatEnv.UnboundNamed n =>
adamc@26 202 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@26 203
adamc@29 204 fun testCjrize filename =
adamc@29 205 (case cjrize ElabEnv.basis CoreEnv.basis filename of
adamc@29 206 NONE => print "Failed\n"
adamc@29 207 | SOME file =>
adamc@29 208 (Print.print (CjrPrint.p_file CjrEnv.basis file);
adamc@29 209 print "\n"))
adamc@29 210 handle CjrEnv.UnboundNamed n =>
adamc@29 211 print ("Unbound named " ^ Int.toString n ^ "\n")
adamc@29 212
adamc@29 213 fun compile filename =
adamc@29 214 case cjrize ElabEnv.basis CoreEnv.basis filename of
adamc@29 215 NONE => ()
adamc@29 216 | SOME file =>
adamc@29 217 let
adamc@29 218 val outf = TextIO.openOut "/tmp/lacweb.c"
adamc@29 219 val s = TextIOPP.openOut {dst = outf, wid = 80}
adamc@29 220 in
adamc@29 221 Print.fprint s (CjrPrint.p_file CjrEnv.basis file);
adamc@29 222 TextIO.closeOut outf
adamc@29 223 end
adamc@29 224
adamc@1 225 end