annotate src/flat_print.sml @ 46:44a1bc863f0f

Corifying functors
author Adam Chlipala <adamc@hcoop.net>
date Thu, 19 Jun 2008 17:55:36 -0400
parents 1c91c5e6840f
children 198172560b73
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@29 45 TTop => string "?"
adamc@29 46 | TFun (t1, t2) => parenIf par (box [p_typ' true env t1,
adamc@26 47 space,
adamc@26 48 string "->",
adamc@26 49 space,
adamc@26 50 p_typ env t2])
adamc@26 51 | TCode (t1, t2) => parenIf par (box [p_typ' true env t1,
adamc@26 52 space,
adamc@26 53 string "-->",
adamc@26 54 space,
adamc@26 55 p_typ env t2])
adamc@26 56 | TRecord xcs => box [string "{",
adamc@26 57 p_list (fn (x, t) =>
adamc@26 58 box [string x,
adamc@26 59 space,
adamc@26 60 string ":",
adamc@26 61 space,
adamc@26 62 p_typ env t]) xcs,
adamc@26 63 string "}"]
adamc@26 64 | TNamed n =>
adamc@26 65 if !debug then
adamc@26 66 string (#1 (E.lookupTNamed env n) ^ "__" ^ Int.toString n)
adamc@26 67 else
adamc@26 68 string (#1 (E.lookupTNamed env n))
adamc@26 69
adamc@26 70 and p_typ env = p_typ' false env
adamc@26 71
adamc@26 72 fun p_exp' par env (e, _) =
adamc@26 73 case e of
adamc@26 74 EPrim p => Prim.p_t p
adamc@26 75 | ERel n =>
adamc@26 76 ((if !debug then
adamc@26 77 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@26 78 else
adamc@26 79 string (#1 (E.lookupERel env n)))
adamc@26 80 handle E.UnboundRel _ => string ("UNBOUND" ^ Int.toString n))
adamc@26 81 | ENamed n =>
adamc@26 82 if !debug then
adamc@26 83 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@26 84 else
adamc@26 85 string (#1 (E.lookupENamed env n))
adamc@26 86 | ECode n => string ("code$" ^ Int.toString n)
adamc@26 87 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@26 88 space,
adamc@26 89 p_exp' true env e2])
adamc@26 90
adamc@26 91 | ERecord xes => box [string "{",
adamc@29 92 p_list (fn (x, e, _) =>
adamc@26 93 box [string x,
adamc@26 94 space,
adamc@26 95 string "=",
adamc@26 96 space,
adamc@26 97 p_exp env e]) xes,
adamc@26 98 string "}"]
adamc@26 99 | EField (e, x) =>
adamc@26 100 box [p_exp' true env e,
adamc@26 101 string ".",
adamc@26 102 string x]
adamc@26 103
adamc@26 104 | ELet (xes, e) =>
adamc@26 105 let
adamc@29 106 val (env, pps) = foldl (fn ((x, _, e), (env, pps)) =>
adamc@26 107 (E.pushERel env x dummyTyp,
adamc@26 108 List.revAppend ([space,
adamc@26 109 string "val",
adamc@26 110 space,
adamc@26 111 string x,
adamc@26 112 space,
adamc@26 113 string "=",
adamc@26 114 space,
adamc@26 115 p_exp env e],
adamc@26 116 pps)))
adamc@26 117 (env, []) xes
adamc@26 118 in
adamc@26 119 box [string "let",
adamc@26 120 space,
adamc@26 121 box (rev pps),
adamc@26 122 space,
adamc@26 123 string "in",
adamc@26 124 space,
adamc@26 125 p_exp env e,
adamc@26 126 space,
adamc@26 127 string "end"]
adamc@26 128 end
adamc@26 129
adamc@26 130 and p_exp env = p_exp' false env
adamc@26 131
adamc@26 132 fun p_decl env ((d, _) : decl) =
adamc@26 133 case d of
adamc@26 134 DVal (x, n, t, e) =>
adamc@26 135 let
adamc@26 136 val xp = if !debug then
adamc@26 137 box [string x,
adamc@26 138 string "__",
adamc@26 139 string (Int.toString n)]
adamc@26 140 else
adamc@26 141 string x
adamc@26 142 in
adamc@26 143 box [string "val",
adamc@26 144 space,
adamc@26 145 xp,
adamc@26 146 space,
adamc@26 147 string ":",
adamc@26 148 space,
adamc@26 149 p_typ env t,
adamc@26 150 space,
adamc@26 151 string "=",
adamc@26 152 space,
adamc@26 153 p_exp env e]
adamc@26 154
adamc@26 155 end
adamc@26 156 | DFun (n, x, dom, ran, e) =>
adamc@26 157 let
adamc@26 158 val xp = if !debug then
adamc@26 159 box [string x,
adamc@26 160 string "__",
adamc@26 161 string (Int.toString n)]
adamc@26 162 else
adamc@26 163 string x
adamc@26 164 in
adamc@26 165 box [string "fun",
adamc@26 166 space,
adamc@26 167 string "code$",
adamc@26 168 string (Int.toString n),
adamc@26 169 space,
adamc@26 170 string "(",
adamc@26 171 xp,
adamc@26 172 space,
adamc@26 173 string ":",
adamc@26 174 space,
adamc@26 175 p_typ env dom,
adamc@26 176 string ")",
adamc@26 177 space,
adamc@26 178 string ":",
adamc@26 179 space,
adamc@26 180 p_typ env ran,
adamc@26 181 space,
adamc@26 182 string "=",
adamc@26 183 space,
adamc@26 184 p_exp (E.pushERel env x dom) e]
adamc@26 185
adamc@26 186 end
adamc@26 187
adamc@26 188 fun p_file env file =
adamc@26 189 let
adamc@31 190 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 191 (p_decl env d,
adamc@31 192 E.declBinds env d))
adamc@26 193 env file
adamc@26 194 in
adamc@26 195 p_list_sep newline (fn x => x) pds
adamc@26 196 end
adamc@26 197
adamc@26 198 end