annotate src/flat_print.sml @ 26:4ab19c19665f

Closure conversion
author Adam Chlipala <adamc@hcoop.net>
date Tue, 10 Jun 2008 15:56:33 -0400
parents
children 537db4ee89f4
rev   line source
adamc@26 1 (* Copyright (c) 2008, Adam Chlipala
adamc@26 2 * All rights reserved.
adamc@26 3 *
adamc@26 4 * Redistribution and use in source and binary forms, with or without
adamc@26 5 * modification, are permitted provided that the following conditions are met:
adamc@26 6 *
adamc@26 7 * - Redistributions of source code must retain the above copyright notice,
adamc@26 8 * this list of conditions and the following disclaimer.
adamc@26 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@26 10 * this list of conditions and the following disclaimer in the documentation
adamc@26 11 * and/or other materials provided with the distribution.
adamc@26 12 * - The names of contributors may not be used to endorse or promote products
adamc@26 13 * derived from this software without specific prior written permission.
adamc@26 14 *
adamc@26 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@26 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@26 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@26 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@26 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@26 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@26 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@26 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@26 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@26 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@26 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@26 26 *)
adamc@26 27
adamc@26 28 (* Pretty-printing flat-code Laconic/Web *)
adamc@26 29
adamc@26 30 structure FlatPrint :> FLAT_PRINT = struct
adamc@26 31
adamc@26 32 open Print.PD
adamc@26 33 open Print
adamc@26 34
adamc@26 35 open Flat
adamc@26 36
adamc@26 37 structure E = FlatEnv
adamc@26 38
adamc@26 39 val debug = ref false
adamc@26 40
adamc@26 41 val dummyTyp = (TNamed 0, ErrorMsg.dummySpan)
adamc@26 42
adamc@26 43 fun p_typ' par env (t, _) =
adamc@26 44 case t of
adamc@26 45 TFun (t1, t2) => parenIf par (box [p_typ' true env t1,
adamc@26 46 space,
adamc@26 47 string "->",
adamc@26 48 space,
adamc@26 49 p_typ env t2])
adamc@26 50 | TCode (t1, t2) => parenIf par (box [p_typ' true env t1,
adamc@26 51 space,
adamc@26 52 string "-->",
adamc@26 53 space,
adamc@26 54 p_typ env t2])
adamc@26 55 | TRecord xcs => box [string "{",
adamc@26 56 p_list (fn (x, t) =>
adamc@26 57 box [string x,
adamc@26 58 space,
adamc@26 59 string ":",
adamc@26 60 space,
adamc@26 61 p_typ env t]) xcs,
adamc@26 62 string "}"]
adamc@26 63 | TNamed n =>
adamc@26 64 if !debug then
adamc@26 65 string (#1 (E.lookupTNamed env n) ^ "__" ^ Int.toString n)
adamc@26 66 else
adamc@26 67 string (#1 (E.lookupTNamed env n))
adamc@26 68
adamc@26 69 and p_typ env = p_typ' false env
adamc@26 70
adamc@26 71 fun p_exp' par env (e, _) =
adamc@26 72 case e of
adamc@26 73 EPrim p => Prim.p_t p
adamc@26 74 | ERel n =>
adamc@26 75 ((if !debug then
adamc@26 76 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@26 77 else
adamc@26 78 string (#1 (E.lookupERel env n)))
adamc@26 79 handle E.UnboundRel _ => string ("UNBOUND" ^ Int.toString n))
adamc@26 80 | ENamed n =>
adamc@26 81 if !debug then
adamc@26 82 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@26 83 else
adamc@26 84 string (#1 (E.lookupENamed env n))
adamc@26 85 | ECode n => string ("code$" ^ Int.toString n)
adamc@26 86 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@26 87 space,
adamc@26 88 p_exp' true env e2])
adamc@26 89
adamc@26 90 | ERecord xes => box [string "{",
adamc@26 91 p_list (fn (x, e) =>
adamc@26 92 box [string x,
adamc@26 93 space,
adamc@26 94 string "=",
adamc@26 95 space,
adamc@26 96 p_exp env e]) xes,
adamc@26 97 string "}"]
adamc@26 98 | EField (e, x) =>
adamc@26 99 box [p_exp' true env e,
adamc@26 100 string ".",
adamc@26 101 string x]
adamc@26 102
adamc@26 103 | ELet (xes, e) =>
adamc@26 104 let
adamc@26 105 val (env, pps) = foldl (fn ((x, e), (env, pps)) =>
adamc@26 106 (E.pushERel env x dummyTyp,
adamc@26 107 List.revAppend ([space,
adamc@26 108 string "val",
adamc@26 109 space,
adamc@26 110 string x,
adamc@26 111 space,
adamc@26 112 string "=",
adamc@26 113 space,
adamc@26 114 p_exp env e],
adamc@26 115 pps)))
adamc@26 116 (env, []) xes
adamc@26 117 in
adamc@26 118 box [string "let",
adamc@26 119 space,
adamc@26 120 box (rev pps),
adamc@26 121 space,
adamc@26 122 string "in",
adamc@26 123 space,
adamc@26 124 p_exp env e,
adamc@26 125 space,
adamc@26 126 string "end"]
adamc@26 127 end
adamc@26 128
adamc@26 129 and p_exp env = p_exp' false env
adamc@26 130
adamc@26 131 fun p_decl env ((d, _) : decl) =
adamc@26 132 case d of
adamc@26 133 DVal (x, n, t, e) =>
adamc@26 134 let
adamc@26 135 val xp = if !debug then
adamc@26 136 box [string x,
adamc@26 137 string "__",
adamc@26 138 string (Int.toString n)]
adamc@26 139 else
adamc@26 140 string x
adamc@26 141 in
adamc@26 142 box [string "val",
adamc@26 143 space,
adamc@26 144 xp,
adamc@26 145 space,
adamc@26 146 string ":",
adamc@26 147 space,
adamc@26 148 p_typ env t,
adamc@26 149 space,
adamc@26 150 string "=",
adamc@26 151 space,
adamc@26 152 p_exp env e]
adamc@26 153
adamc@26 154 end
adamc@26 155 | DFun (n, x, dom, ran, e) =>
adamc@26 156 let
adamc@26 157 val xp = if !debug then
adamc@26 158 box [string x,
adamc@26 159 string "__",
adamc@26 160 string (Int.toString n)]
adamc@26 161 else
adamc@26 162 string x
adamc@26 163 in
adamc@26 164 box [string "fun",
adamc@26 165 space,
adamc@26 166 string "code$",
adamc@26 167 string (Int.toString n),
adamc@26 168 space,
adamc@26 169 string "(",
adamc@26 170 xp,
adamc@26 171 space,
adamc@26 172 string ":",
adamc@26 173 space,
adamc@26 174 p_typ env dom,
adamc@26 175 string ")",
adamc@26 176 space,
adamc@26 177 string ":",
adamc@26 178 space,
adamc@26 179 p_typ env ran,
adamc@26 180 space,
adamc@26 181 string "=",
adamc@26 182 space,
adamc@26 183 p_exp (E.pushERel env x dom) e]
adamc@26 184
adamc@26 185 end
adamc@26 186
adamc@26 187 fun p_file env file =
adamc@26 188 let
adamc@26 189 val (_, pds) = ListUtil.mapfoldl (fn (d, env) =>
adamc@26 190 (E.declBinds env d,
adamc@26 191 p_decl env d))
adamc@26 192 env file
adamc@26 193 in
adamc@26 194 p_list_sep newline (fn x => x) pds
adamc@26 195 end
adamc@26 196
adamc@26 197 end