annotate src/cjrize.sml @ 101:717b6f8d8505

First executable generated
author Adam Chlipala <adamc@hcoop.net>
date Thu, 10 Jul 2008 11:13:49 -0400
parents 4f641f8fddaa
children 5f04adf47f48
rev   line source
adamc@29 1 (* Copyright (c) 2008, Adam Chlipala
adamc@29 2 * All rights reserved.
adamc@29 3 *
adamc@29 4 * Redistribution and use in source and binary forms, with or without
adamc@29 5 * modification, are permitted provided that the following conditions are met:
adamc@29 6 *
adamc@29 7 * - Redistributions of source code must retain the above copyright notice,
adamc@29 8 * this list of conditions and the following disclaimer.
adamc@29 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@29 10 * this list of conditions and the following disclaimer in the documentation
adamc@29 11 * and/or other materials provided with the distribution.
adamc@29 12 * - The names of contributors may not be used to endorse or promote products
adamc@29 13 * derived from this software without specific prior written permission.
adamc@29 14 *
adamc@29 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@29 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@29 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@29 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@29 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@29 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@29 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@29 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@29 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@29 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@29 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@29 26 *)
adamc@29 27
adamc@29 28 structure Cjrize :> CJRIZE = struct
adamc@29 29
adamc@29 30 structure L = Flat
adamc@29 31 structure L' = Cjr
adamc@29 32
adamc@29 33 structure Sm :> sig
adamc@29 34 type t
adamc@29 35
adamc@29 36 val empty : t
adamc@29 37 val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int
adamc@29 38
adamc@29 39 val declares : t -> (int * (string * L'.typ) list) list
adamc@29 40 end = struct
adamc@29 41
adamc@29 42 structure FM = BinaryMapFn(struct
adamc@29 43 type ord_key = L.typ
adamc@29 44 val compare = FlatUtil.Typ.compare
adamc@29 45 end)
adamc@29 46
adamc@29 47 type t = int * int FM.map * (int * (string * L'.typ) list) list
adamc@29 48
adamc@29 49 val empty = (0, FM.empty, [])
adamc@29 50
adamc@29 51 fun find ((n, m, ds), xts, xts') =
adamc@29 52 let
adamc@29 53 val t = (L.TRecord xts, ErrorMsg.dummySpan)
adamc@29 54 in
adamc@29 55 case FM.find (m, t) of
adamc@29 56 NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n)
adamc@29 57 | SOME i => ((n, m, ds), i)
adamc@29 58 end
adamc@29 59
adamc@29 60 fun declares (_, _, ds) = ds
adamc@29 61
adamc@29 62 end
adamc@29 63
adamc@29 64 fun cifyTyp ((t, loc), sm) =
adamc@29 65 case t of
adamc@29 66 L.TTop => ((L'.TTop, loc), sm)
adamc@29 67 | L.TFun (t1, t2) =>
adamc@29 68 let
adamc@29 69 val (_, sm) = cifyTyp (t1, sm)
adamc@29 70 val (_, sm) = cifyTyp (t2, sm)
adamc@29 71 in
adamc@29 72 ((L'.TFun, loc), sm)
adamc@29 73 end
adamc@29 74 | L.TCode (t1, t2) =>
adamc@29 75 let
adamc@29 76 val (t1, sm) = cifyTyp (t1, sm)
adamc@29 77 val (t2, sm) = cifyTyp (t2, sm)
adamc@29 78 in
adamc@29 79 ((L'.TCode (t1, t2), loc), sm)
adamc@29 80 end
adamc@29 81 | L.TRecord xts =>
adamc@29 82 let
adamc@29 83 val old_xts = xts
adamc@29 84 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@29 85 let
adamc@29 86 val (t, sm) = cifyTyp (t, sm)
adamc@29 87 in
adamc@29 88 ((x, t), sm)
adamc@29 89 end)
adamc@29 90 sm xts
adamc@29 91 val (sm, si) = Sm.find (sm, old_xts, xts)
adamc@29 92 in
adamc@29 93 ((L'.TRecord si, loc), sm)
adamc@29 94 end
adamc@29 95 | L.TNamed n => ((L'.TNamed n, loc), sm)
adamc@53 96 | L.TFfi mx => ((L'.TFfi mx, loc), sm)
adamc@29 97
adamc@29 98 fun cifyExp ((e, loc), sm) =
adamc@29 99 case e of
adamc@29 100 L.EPrim p => ((L'.EPrim p, loc), sm)
adamc@29 101 | L.ERel n => ((L'.ERel n, loc), sm)
adamc@29 102 | L.ENamed n => ((L'.ENamed n, loc), sm)
adamc@53 103 | L.EFfi mx => ((L'.EFfi mx, loc), sm)
adamc@53 104 | L.EFfiApp (m, x, es) =>
adamc@53 105 let
adamc@53 106 val (es, sm) = ListUtil.foldlMap cifyExp sm es
adamc@53 107 in
adamc@53 108 ((L'.EFfiApp (m, x, es), loc), sm)
adamc@53 109 end
adamc@29 110 | L.ECode n => ((L'.ECode n, loc), sm)
adamc@29 111 | L.EApp (e1, e2) =>
adamc@29 112 let
adamc@29 113 val (e1, sm) = cifyExp (e1, sm)
adamc@29 114 val (e2, sm) = cifyExp (e2, sm)
adamc@29 115 in
adamc@29 116 ((L'.EApp (e1, e2), loc), sm)
adamc@29 117 end
adamc@29 118
adamc@29 119 | L.ERecord xes =>
adamc@29 120 let
adamc@29 121 val old_xts = map (fn (x, _, t) => (x, t)) xes
adamc@29 122
adamc@29 123 val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) =>
adamc@29 124 let
adamc@29 125 val (e, sm) = cifyExp (e, sm)
adamc@29 126 val (t, sm) = cifyTyp (t, sm)
adamc@29 127 in
adamc@29 128 ((x, e, t), sm)
adamc@29 129 end)
adamc@29 130 sm xes
adamc@29 131
adamc@29 132 val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets)
adamc@29 133
adamc@29 134 val xes = map (fn (x, e, _) => (x, e)) xets
adamc@29 135 val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes
adamc@29 136 in
adamc@29 137 ((L'.ERecord (si, xes), loc), sm)
adamc@29 138 end
adamc@29 139 | L.EField (e, x) =>
adamc@29 140 let
adamc@29 141 val (e, sm) = cifyExp (e, sm)
adamc@29 142 in
adamc@29 143 ((L'.EField (e, x), loc), sm)
adamc@29 144 end
adamc@29 145
adamc@29 146 | L.ELet (xes, e) =>
adamc@29 147 let
adamc@29 148 val (xes, sm) = ListUtil.foldlMap (fn ((x, t, e), sm) =>
adamc@29 149 let
adamc@29 150 val (t, sm) = cifyTyp (t, sm)
adamc@29 151 val (e, sm) = cifyExp (e, sm)
adamc@29 152 in
adamc@29 153 ((x, t, e), sm)
adamc@29 154 end)
adamc@29 155 sm xes
adamc@29 156 val (e, sm) = cifyExp (e, sm)
adamc@29 157 in
adamc@29 158 ((L'.ELet (xes, e), loc), sm)
adamc@29 159 end
adamc@29 160
adamc@29 161 fun cifyDecl ((d, loc), sm) =
adamc@29 162 case d of
adamc@29 163 L.DVal (x, n, t, e) =>
adamc@29 164 let
adamc@29 165 val (t, sm) = cifyTyp (t, sm)
adamc@29 166 val (e, sm) = cifyExp (e, sm)
adamc@29 167 in
adamc@101 168 (SOME (L'.DVal (x, n, t, e), loc), NONE, sm)
adamc@29 169 end
adamc@29 170 | L.DFun (n, x, dom, ran, e) =>
adamc@29 171 let
adamc@29 172 val (dom, sm) = cifyTyp (dom, sm)
adamc@29 173 val (ran, sm) = cifyTyp (ran, sm)
adamc@29 174 val (e, sm) = cifyExp (e, sm)
adamc@29 175 in
adamc@101 176 (SOME (L'.DFun (n, x, dom, ran, e), loc), NONE, sm)
adamc@101 177 end
adamc@101 178 | L.DPage (xts, e) =>
adamc@101 179 let
adamc@101 180 val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) =>
adamc@101 181 let
adamc@101 182 val (t, sm) = cifyTyp (t, sm)
adamc@101 183 in
adamc@101 184 ((x, t), sm)
adamc@101 185 end)
adamc@101 186 sm xts
adamc@101 187 val (e, sm) = cifyExp (e, sm)
adamc@101 188 in
adamc@101 189 (NONE, SOME (xts, e), sm)
adamc@29 190 end
adamc@29 191
adamc@29 192 fun cjrize ds =
adamc@29 193 let
adamc@101 194 val (ds, ps, sm) = foldl (fn (d, (ds, ps, sm)) =>
adamc@101 195 let
adamc@101 196 val (dop, pop, sm) = cifyDecl (d, sm)
adamc@101 197 val ds = case dop of
adamc@101 198 NONE => ds
adamc@101 199 | SOME d => d :: ds
adamc@101 200 val ps = case pop of
adamc@101 201 NONE => ps
adamc@101 202 | SOME p => p :: ps
adamc@101 203 in
adamc@101 204 (ds, ps, sm)
adamc@101 205 end)
adamc@101 206 ([], [], Sm.empty) ds
adamc@29 207 in
adamc@101 208 (List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm),
adamc@101 209 rev ds),
adamc@101 210 ps)
adamc@29 211 end
adamc@29 212
adamc@29 213 end