annotate src/mono_print.sml @ 270:b9b02613c0c2

Parsing jobs
author Adam Chlipala <adamc@hcoop.net>
date Tue, 02 Sep 2008 10:31:16 -0400
parents 7e9bd70ad3ce
children 42dfb0d61cf0
rev   line source
adamc@25 1 (* Copyright (c) 2008, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@25 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@244 28 (* Pretty-printing monomorphic Ur/Web *)
adamc@25 29
adamc@25 30 structure MonoPrint :> MONO_PRINT = struct
adamc@25 31
adamc@25 32 open Print.PD
adamc@25 33 open Print
adamc@25 34
adamc@25 35 open Mono
adamc@25 36
adamc@25 37 structure E = MonoEnv
adamc@25 38
adamc@25 39 val debug = ref false
adamc@25 40
adamc@252 41 val dummyt = (TRecord [], ErrorMsg.dummySpan)
adamc@252 42
adamc@25 43 fun p_typ' par env (t, _) =
adamc@25 44 case t of
adamc@25 45 TFun (t1, t2) => parenIf par (box [p_typ' true env t1,
adamc@25 46 space,
adamc@25 47 string "->",
adamc@25 48 space,
adamc@25 49 p_typ env t2])
adamc@25 50 | TRecord xcs => box [string "{",
adamc@25 51 p_list (fn (x, t) =>
adamc@25 52 box [string x,
adamc@25 53 space,
adamc@25 54 string ":",
adamc@25 55 space,
adamc@25 56 p_typ env t]) xcs,
adamc@25 57 string "}"]
adamc@196 58 | TDatatype (n, _) =>
adamc@178 59 ((if !debug then
adamc@178 60 string (#1 (E.lookupDatatype env n) ^ "__" ^ Int.toString n)
adamc@178 61 else
adamc@178 62 string (#1 (E.lookupDatatype env n)))
adamc@178 63 handle E.UnboundNamed _ => string ("UNBOUND_DATATYPE_" ^ Int.toString n))
adamc@51 64 | TFfi (m, x) => box [string "FFI(", string m, string ".", string x, string ")"]
adamc@25 65
adamc@25 66 and p_typ env = p_typ' false env
adamc@25 67
adamc@109 68 fun p_enamed env n =
adamc@178 69 (if !debug then
adamc@178 70 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@178 71 else
adamc@178 72 string (#1 (E.lookupENamed env n)))
adamc@178 73 handle E.UnboundNamed _ => string ("UNBOUNDN_" ^ Int.toString n)
adamc@178 74
adamc@178 75 fun p_con_named env n =
adamc@178 76 (if !debug then
adamc@178 77 string (#1 (E.lookupConstructor env n) ^ "__" ^ Int.toString n)
adamc@178 78 else
adamc@178 79 string (#1 (E.lookupConstructor env n)))
adamc@178 80 handle E.UnboundNamed _ => string ("CONSTRUCTOR_" ^ Int.toString n)
adamc@178 81
adamc@178 82 fun p_patCon env pc =
adamc@178 83 case pc of
adamc@178 84 PConVar n => p_con_named env n
adamc@186 85 | PConFfi {mod = m, con, ...} => box [string "FFIC(",
adamc@185 86 string m,
adamc@185 87 string ".",
adamc@185 88 string con,
adamc@185 89 string ")"]
adamc@178 90
adamc@178 91 fun p_pat' par env (p, _) =
adamc@178 92 case p of
adamc@178 93 PWild => string "_"
adamc@182 94 | PVar (s, _) => string s
adamc@178 95 | PPrim p => Prim.p_t p
adamc@188 96 | PCon (_, n, NONE) => p_patCon env n
adamc@188 97 | PCon (_, n, SOME p) => parenIf par (box [p_patCon env n,
adamc@178 98 space,
adamc@178 99 p_pat' true env p])
adamc@178 100 | PRecord xps =>
adamc@178 101 box [string "{",
adamc@182 102 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
adamc@178 103 box [string x,
adamc@178 104 space,
adamc@178 105 string "=",
adamc@178 106 space,
adamc@178 107 p_pat env p]) xps,
adamc@178 108 string "}"]
adamc@178 109
adamc@178 110 and p_pat x = p_pat' false x
adamc@109 111
adamc@25 112 fun p_exp' par env (e, _) =
adamc@25 113 case e of
adamc@25 114 EPrim p => Prim.p_t p
adamc@25 115 | ERel n =>
adamc@178 116 ((if !debug then
adamc@178 117 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@178 118 else
adamc@178 119 string (#1 (E.lookupERel env n)))
adamc@178 120 handle E.UnboundRel _ => string ("UNBOUND_" ^ Int.toString n))
adamc@109 121 | ENamed n => p_enamed env n
adamc@188 122 | ECon (_, pc, NONE) => p_patCon env pc
adamc@188 123 | ECon (_, pc, SOME e) => parenIf par (box [p_patCon env pc,
adamc@188 124 space,
adamc@188 125 p_exp' true env e])
adamc@109 126
adamc@51 127 | EFfi (m, x) => box [string "FFI(", string m, string ".", string x, string ")"]
adamc@51 128 | EFfiApp (m, x, es) => box [string "FFI(",
adamc@51 129 string m,
adamc@51 130 string ".",
adamc@51 131 string x,
adamc@51 132 string "(",
adamc@51 133 p_list (p_exp env) es,
adamc@51 134 string "))"]
adamc@25 135 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@25 136 space,
adamc@25 137 p_exp' true env e2])
adamc@252 138 | EAbs (x, t, _, e) => parenIf true (box [string "fn",
adamc@252 139 space,
adamc@252 140 string x,
adamc@252 141 space,
adamc@252 142 string ":",
adamc@252 143 space,
adamc@252 144 p_typ env t,
adamc@252 145 space,
adamc@252 146 string "=>",
adamc@252 147 space,
adamc@252 148 p_exp (E.pushERel env x t NONE) e])
adamc@25 149
adamc@25 150 | ERecord xes => box [string "{",
adamc@29 151 p_list (fn (x, e, _) =>
adamc@25 152 box [string x,
adamc@25 153 space,
adamc@25 154 string "=",
adamc@25 155 space,
adamc@25 156 p_exp env e]) xes,
adamc@25 157 string "}"]
adamc@25 158 | EField (e, x) =>
adamc@25 159 box [p_exp' true env e,
adamc@25 160 string ".",
adamc@25 161 string x]
adamc@25 162
adamc@252 163 | ECase (e, pes, _) => parenIf true (box [string "case",
adamc@252 164 space,
adamc@252 165 p_exp env e,
adamc@252 166 space,
adamc@252 167 string "of",
adamc@252 168 space,
adamc@252 169 p_list_sep (box [space, string "|", space])
adamc@252 170 (fn (p, e) => box [p_pat env p,
adamc@252 171 space,
adamc@252 172 string "=>",
adamc@252 173 space,
adamc@252 174 p_exp (E.patBinds env p) e]) pes])
adamc@94 175
adamc@94 176 | EStrcat (e1, e2) => box [p_exp' true env e1,
adamc@94 177 space,
adamc@102 178 string "^",
adamc@94 179 space,
adamc@94 180 p_exp' true env e2]
adamc@94 181
adamc@102 182 | EWrite e => box [string "write(",
adamc@102 183 p_exp env e,
adamc@102 184 string ")"]
adamc@102 185
adamc@106 186 | ESeq (e1, e2) => box [p_exp env e1,
adamc@106 187 string ";",
adamc@106 188 space,
adamc@106 189 p_exp env e2]
adamc@252 190 | ELet (x, t, e1, e2) => box [string "(let",
adamc@251 191 space,
adamc@251 192 string x,
adamc@251 193 space,
adamc@251 194 string ":",
adamc@251 195 space,
adamc@251 196 p_typ env t,
adamc@251 197 space,
adamc@251 198 string "=",
adamc@251 199 space,
adamc@252 200 string "(",
adamc@251 201 p_exp env e1,
adamc@252 202 string ")",
adamc@251 203 space,
adamc@251 204 string "in",
adamc@251 205 space,
adamc@252 206 string "(",
adamc@252 207 p_exp (E.pushERel env x t NONE) e2,
adamc@252 208 string "))"]
adamc@106 209
adamc@111 210 | EClosure (n, es) => box [string "CLOSURE(",
adamc@111 211 p_enamed env n,
adamc@111 212 p_list_sep (string "") (fn e => box [string ", ",
adamc@111 213 p_exp env e]) es,
adamc@111 214 string ")"]
adamc@111 215
adamc@252 216 | EQuery {exps, tables, state, query, body, initial} =>
adamc@252 217 box [string "query[",
adamc@252 218 p_list (fn (x, t) => box [string x, space, string ":", space, p_typ env t]) exps,
adamc@252 219 string "] [",
adamc@252 220 p_list (fn (x, xts) => box [string x,
adamc@252 221 space,
adamc@252 222 string ":",
adamc@252 223 space,
adamc@252 224 string "{",
adamc@252 225 p_list (fn (x, t) => box [string x, space, string ":", space, p_typ env t]) xts,
adamc@252 226 string "}"]) tables,
adamc@252 227 string "] [",
adamc@252 228 p_typ env state,
adamc@252 229 string "]",
adamc@252 230 space,
adamc@252 231 p_exp env query,
adamc@252 232 space,
adamc@252 233 string "initial",
adamc@252 234 space,
adamc@252 235 p_exp env initial,
adamc@252 236 space,
adamc@252 237 string "in",
adamc@252 238 space,
adamc@252 239 p_exp (E.pushERel (E.pushERel env "r" dummyt NONE) "acc" dummyt NONE) body]
adamc@252 240
adamc@25 241 and p_exp env = p_exp' false env
adamc@25 242
adamc@126 243 fun p_vali env (x, n, t, e, s) =
adamc@126 244 let
adamc@126 245 val xp = if !debug then
adamc@126 246 box [string x,
adamc@126 247 string "__",
adamc@126 248 string (Int.toString n)]
adamc@126 249 else
adamc@126 250 string x
adamc@126 251 in
adamc@126 252 box [xp,
adamc@126 253 space,
adamc@126 254 string "as",
adamc@126 255 space,
adamc@126 256 string s,
adamc@126 257 space,
adamc@126 258 string ":",
adamc@126 259 space,
adamc@126 260 p_typ env t,
adamc@126 261 space,
adamc@126 262 string "=",
adamc@126 263 space,
adamc@126 264 p_exp env e]
adamc@126 265 end
adamc@126 266
adamc@164 267 fun p_datatype env (x, n, cons) =
adamc@164 268 let
adamc@168 269 val env = E.pushDatatype env x n cons
adamc@164 270 in
adamc@164 271 box [string "datatype",
adamc@164 272 space,
adamc@164 273 string x,
adamc@164 274 space,
adamc@164 275 string "=",
adamc@164 276 space,
adamc@164 277 p_list_sep (box [space, string "|", space])
adamc@164 278 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@164 279 else string x
adamc@164 280 | (x, _, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@164 281 else string x, space, string "of", space, p_typ env t])
adamc@164 282 cons]
adamc@164 283 end
adamc@164 284
adamc@126 285 fun p_decl env (dAll as (d, _) : decl) =
adamc@25 286 case d of
adamc@164 287 DDatatype x => p_datatype env x
adamc@164 288 | DVal vi => box [string "val",
adamc@126 289 space,
adamc@126 290 p_vali env vi]
adamc@126 291 | DValRec vis =>
adamc@25 292 let
adamc@126 293 val env = E.declBinds env dAll
adamc@25 294 in
adamc@25 295 box [string "val",
adamc@25 296 space,
adamc@126 297 string "rec",
adamc@25 298 space,
adamc@126 299 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@25 300 end
adamc@109 301
adamc@144 302 | DExport (ek, s, n, ts) => box [string "export",
adamc@144 303 space,
adamc@144 304 CorePrint.p_export_kind ek,
adamc@144 305 space,
adamc@144 306 p_enamed env n,
adamc@144 307 space,
adamc@144 308 string "as",
adamc@144 309 space,
adamc@144 310 string s,
adamc@144 311 p_list_sep (string "") (fn t => box [space,
adamc@144 312 string "(",
adamc@144 313 p_typ env t,
adamc@144 314 string ")"]) ts]
adamc@100 315
adamc@25 316 fun p_file env file =
adamc@25 317 let
adamc@31 318 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 319 (p_decl env d,
adamc@31 320 E.declBinds env d))
adamc@25 321 env file
adamc@25 322 in
adamc@25 323 p_list_sep newline (fn x => x) pds
adamc@25 324 end
adamc@25 325
adamc@25 326 end