annotate src/mono_print.sml @ 288:4260ad920c36

Converting string to int
author Adam Chlipala <adamc@hcoop.net>
date Sun, 07 Sep 2008 11:33:13 -0400
parents c0e4ac23522d
children df00701f2323
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@288 65 | TOption t =>
adamc@288 66 (case #1 t of
adamc@288 67 TDatatype _ => p_typ env t
adamc@288 68 | TFfi ("Basis", "string") => p_typ env t
adamc@288 69 | _ => box [p_typ env t, string "*"])
adamc@25 70
adamc@25 71 and p_typ env = p_typ' false env
adamc@25 72
adamc@109 73 fun p_enamed env n =
adamc@178 74 (if !debug then
adamc@178 75 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@178 76 else
adamc@178 77 string (#1 (E.lookupENamed env n)))
adamc@178 78 handle E.UnboundNamed _ => string ("UNBOUNDN_" ^ Int.toString n)
adamc@178 79
adamc@178 80 fun p_con_named env n =
adamc@178 81 (if !debug then
adamc@178 82 string (#1 (E.lookupConstructor env n) ^ "__" ^ Int.toString n)
adamc@178 83 else
adamc@178 84 string (#1 (E.lookupConstructor env n)))
adamc@178 85 handle E.UnboundNamed _ => string ("CONSTRUCTOR_" ^ Int.toString n)
adamc@178 86
adamc@178 87 fun p_patCon env pc =
adamc@178 88 case pc of
adamc@178 89 PConVar n => p_con_named env n
adamc@186 90 | PConFfi {mod = m, con, ...} => box [string "FFIC(",
adamc@185 91 string m,
adamc@185 92 string ".",
adamc@185 93 string con,
adamc@185 94 string ")"]
adamc@178 95
adamc@178 96 fun p_pat' par env (p, _) =
adamc@178 97 case p of
adamc@178 98 PWild => string "_"
adamc@182 99 | PVar (s, _) => string s
adamc@178 100 | PPrim p => Prim.p_t p
adamc@188 101 | PCon (_, n, NONE) => p_patCon env n
adamc@188 102 | PCon (_, n, SOME p) => parenIf par (box [p_patCon env n,
adamc@288 103 space,
adamc@288 104 p_pat' true env p])
adamc@178 105 | PRecord xps =>
adamc@178 106 box [string "{",
adamc@182 107 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
adamc@178 108 box [string x,
adamc@178 109 space,
adamc@178 110 string "=",
adamc@178 111 space,
adamc@178 112 p_pat env p]) xps,
adamc@178 113 string "}"]
adamc@288 114 | PNone _ => string "None"
adamc@288 115 | PSome (_, p) => box [string "Some",
adamc@288 116 space,
adamc@288 117 p_pat' true env p]
adamc@178 118
adamc@178 119 and p_pat x = p_pat' false x
adamc@109 120
adamc@25 121 fun p_exp' par env (e, _) =
adamc@25 122 case e of
adamc@25 123 EPrim p => Prim.p_t p
adamc@25 124 | ERel n =>
adamc@178 125 ((if !debug then
adamc@178 126 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@178 127 else
adamc@178 128 string (#1 (E.lookupERel env n)))
adamc@178 129 handle E.UnboundRel _ => string ("UNBOUND_" ^ Int.toString n))
adamc@109 130 | ENamed n => p_enamed env n
adamc@188 131 | ECon (_, pc, NONE) => p_patCon env pc
adamc@188 132 | ECon (_, pc, SOME e) => parenIf par (box [p_patCon env pc,
adamc@188 133 space,
adamc@188 134 p_exp' true env e])
adamc@109 135
adamc@51 136 | EFfi (m, x) => box [string "FFI(", string m, string ".", string x, string ")"]
adamc@51 137 | EFfiApp (m, x, es) => box [string "FFI(",
adamc@51 138 string m,
adamc@51 139 string ".",
adamc@51 140 string x,
adamc@51 141 string "(",
adamc@51 142 p_list (p_exp env) es,
adamc@51 143 string "))"]
adamc@25 144 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@25 145 space,
adamc@25 146 p_exp' true env e2])
adamc@252 147 | EAbs (x, t, _, e) => parenIf true (box [string "fn",
adamc@252 148 space,
adamc@252 149 string x,
adamc@252 150 space,
adamc@252 151 string ":",
adamc@252 152 space,
adamc@252 153 p_typ env t,
adamc@252 154 space,
adamc@252 155 string "=>",
adamc@252 156 space,
adamc@252 157 p_exp (E.pushERel env x t NONE) e])
adamc@25 158
adamc@25 159 | ERecord xes => box [string "{",
adamc@29 160 p_list (fn (x, e, _) =>
adamc@25 161 box [string x,
adamc@25 162 space,
adamc@25 163 string "=",
adamc@25 164 space,
adamc@25 165 p_exp env e]) xes,
adamc@25 166 string "}"]
adamc@25 167 | EField (e, x) =>
adamc@25 168 box [p_exp' true env e,
adamc@25 169 string ".",
adamc@25 170 string x]
adamc@25 171
adamc@252 172 | ECase (e, pes, _) => parenIf true (box [string "case",
adamc@252 173 space,
adamc@252 174 p_exp env e,
adamc@252 175 space,
adamc@252 176 string "of",
adamc@252 177 space,
adamc@252 178 p_list_sep (box [space, string "|", space])
adamc@252 179 (fn (p, e) => box [p_pat env p,
adamc@252 180 space,
adamc@252 181 string "=>",
adamc@252 182 space,
adamc@252 183 p_exp (E.patBinds env p) e]) pes])
adamc@94 184
adamc@283 185 | EError (e, t) => box [string "(error",
adamc@283 186 space,
adamc@283 187 p_exp env e,
adamc@283 188 space,
adamc@283 189 string ":",
adamc@283 190 space,
adamc@283 191 p_typ env t,
adamc@283 192 string ")"]
adamc@283 193
adamc@94 194 | EStrcat (e1, e2) => box [p_exp' true env e1,
adamc@94 195 space,
adamc@102 196 string "^",
adamc@94 197 space,
adamc@94 198 p_exp' true env e2]
adamc@94 199
adamc@102 200 | EWrite e => box [string "write(",
adamc@102 201 p_exp env e,
adamc@102 202 string ")"]
adamc@102 203
adamc@106 204 | ESeq (e1, e2) => box [p_exp env e1,
adamc@106 205 string ";",
adamc@106 206 space,
adamc@106 207 p_exp env e2]
adamc@252 208 | ELet (x, t, e1, e2) => box [string "(let",
adamc@251 209 space,
adamc@251 210 string x,
adamc@251 211 space,
adamc@251 212 string ":",
adamc@251 213 space,
adamc@251 214 p_typ env t,
adamc@251 215 space,
adamc@251 216 string "=",
adamc@251 217 space,
adamc@252 218 string "(",
adamc@251 219 p_exp env e1,
adamc@252 220 string ")",
adamc@251 221 space,
adamc@251 222 string "in",
adamc@251 223 space,
adamc@252 224 string "(",
adamc@252 225 p_exp (E.pushERel env x t NONE) e2,
adamc@252 226 string "))"]
adamc@106 227
adamc@111 228 | EClosure (n, es) => box [string "CLOSURE(",
adamc@111 229 p_enamed env n,
adamc@111 230 p_list_sep (string "") (fn e => box [string ", ",
adamc@111 231 p_exp env e]) es,
adamc@111 232 string ")"]
adamc@111 233
adamc@252 234 | EQuery {exps, tables, state, query, body, initial} =>
adamc@252 235 box [string "query[",
adamc@252 236 p_list (fn (x, t) => box [string x, space, string ":", space, p_typ env t]) exps,
adamc@252 237 string "] [",
adamc@252 238 p_list (fn (x, xts) => box [string x,
adamc@252 239 space,
adamc@252 240 string ":",
adamc@252 241 space,
adamc@252 242 string "{",
adamc@252 243 p_list (fn (x, t) => box [string x, space, string ":", space, p_typ env t]) xts,
adamc@252 244 string "}"]) tables,
adamc@252 245 string "] [",
adamc@252 246 p_typ env state,
adamc@252 247 string "]",
adamc@252 248 space,
adamc@252 249 p_exp env query,
adamc@252 250 space,
adamc@252 251 string "initial",
adamc@252 252 space,
adamc@252 253 p_exp env initial,
adamc@252 254 space,
adamc@252 255 string "in",
adamc@252 256 space,
adamc@252 257 p_exp (E.pushERel (E.pushERel env "r" dummyt NONE) "acc" dummyt NONE) body]
adamc@252 258
adamc@25 259 and p_exp env = p_exp' false env
adamc@25 260
adamc@126 261 fun p_vali env (x, n, t, e, s) =
adamc@126 262 let
adamc@126 263 val xp = if !debug then
adamc@126 264 box [string x,
adamc@126 265 string "__",
adamc@126 266 string (Int.toString n)]
adamc@126 267 else
adamc@126 268 string x
adamc@126 269 in
adamc@126 270 box [xp,
adamc@126 271 space,
adamc@126 272 string "as",
adamc@126 273 space,
adamc@126 274 string s,
adamc@126 275 space,
adamc@126 276 string ":",
adamc@126 277 space,
adamc@126 278 p_typ env t,
adamc@126 279 space,
adamc@126 280 string "=",
adamc@126 281 space,
adamc@126 282 p_exp env e]
adamc@126 283 end
adamc@126 284
adamc@164 285 fun p_datatype env (x, n, cons) =
adamc@164 286 let
adamc@168 287 val env = E.pushDatatype env x n cons
adamc@164 288 in
adamc@164 289 box [string "datatype",
adamc@164 290 space,
adamc@164 291 string x,
adamc@164 292 space,
adamc@164 293 string "=",
adamc@164 294 space,
adamc@164 295 p_list_sep (box [space, string "|", space])
adamc@164 296 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@164 297 else string x
adamc@164 298 | (x, _, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@164 299 else string x, space, string "of", space, p_typ env t])
adamc@164 300 cons]
adamc@164 301 end
adamc@164 302
adamc@126 303 fun p_decl env (dAll as (d, _) : decl) =
adamc@25 304 case d of
adamc@164 305 DDatatype x => p_datatype env x
adamc@164 306 | DVal vi => box [string "val",
adamc@126 307 space,
adamc@126 308 p_vali env vi]
adamc@126 309 | DValRec vis =>
adamc@25 310 let
adamc@126 311 val env = E.declBinds env dAll
adamc@25 312 in
adamc@25 313 box [string "val",
adamc@25 314 space,
adamc@126 315 string "rec",
adamc@25 316 space,
adamc@126 317 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@25 318 end
adamc@109 319
adamc@144 320 | DExport (ek, s, n, ts) => box [string "export",
adamc@144 321 space,
adamc@144 322 CorePrint.p_export_kind ek,
adamc@144 323 space,
adamc@144 324 p_enamed env n,
adamc@144 325 space,
adamc@144 326 string "as",
adamc@144 327 space,
adamc@144 328 string s,
adamc@144 329 p_list_sep (string "") (fn t => box [space,
adamc@144 330 string "(",
adamc@144 331 p_typ env t,
adamc@144 332 string ")"]) ts]
adamc@271 333
adamc@273 334 | DTable (s, xts) => box [string "(* SQL table ",
adamc@273 335 string s,
adamc@273 336 space,
adamc@273 337 string ":",
adamc@273 338 space,
adamc@273 339 p_list (fn (x, t) => box [string x,
adamc@273 340 space,
adamc@273 341 string ":",
adamc@273 342 space,
adamc@273 343 p_typ env t]) xts,
adamc@273 344 space,
adamc@273 345 string "*)"]
adamc@271 346 | DDatabase s => box [string "database",
adamc@271 347 space,
adamc@271 348 string s]
adamc@100 349
adamc@25 350 fun p_file env file =
adamc@25 351 let
adamc@31 352 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@31 353 (p_decl env d,
adamc@31 354 E.declBinds env d))
adamc@25 355 env file
adamc@25 356 in
adamc@25 357 p_list_sep newline (fn x => x) pds
adamc@25 358 end
adamc@25 359
adamc@25 360 end