annotate src/expl_print.sml @ 400:e756d3a47726

Fix a nasty driver bug, involving memory restarts and marked-up request strings
author Adam Chlipala <adamc@hcoop.net>
date Tue, 21 Oct 2008 13:54:48 -0400
parents 075b36dbb1a4
children dfc8c991abd0
rev   line source
adamc@38 1 (* Copyright (c) 2008, Adam Chlipala
adamc@38 2 * All rights reserved.
adamc@38 3 *
adamc@38 4 * Redistribution and use in source and binary forms, with or without
adamc@38 5 * modification, are permitted provided that the following conditions are met:
adamc@38 6 *
adamc@38 7 * - Redistributions of source code must retain the above copyright notice,
adamc@38 8 * this list of conditions and the following disclaimer.
adamc@38 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@38 10 * this list of conditions and the following disclaimer in the documentation
adamc@38 11 * and/or other materials provided with the distribution.
adamc@38 12 * - The names of contributors may not be used to endorse or promote products
adamc@38 13 * derived from this software without specific prior written permission.
adamc@38 14 *
adamc@38 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@38 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@38 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@38 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@38 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@38 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@38 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@38 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@38 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@38 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@38 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@38 26 *)
adamc@38 27
adamc@244 28 (* Pretty-printing elaborated Ur/Web *)
adamc@38 29
adamc@38 30 structure ExplPrint :> EXPL_PRINT = struct
adamc@38 31
adamc@38 32 open Print.PD
adamc@38 33 open Print
adamc@38 34
adamc@38 35 open Expl
adamc@38 36
adamc@38 37 structure E = ExplEnv
adamc@38 38
adamc@38 39 val debug = ref false
adamc@38 40
adamc@38 41 fun p_kind' par (k, _) =
adamc@38 42 case k of
adamc@38 43 KType => string "Type"
adamc@38 44 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
adamc@38 45 space,
adamc@38 46 string "->",
adamc@38 47 space,
adamc@38 48 p_kind k2])
adamc@38 49 | KName => string "Name"
adamc@38 50 | KRecord k => box [string "{", p_kind k, string "}"]
adamc@87 51 | KUnit => string "Unit"
adamc@213 52 | KTuple ks => box [string "(",
adamc@213 53 p_list_sep (box [space, string "*", space]) p_kind ks,
adamc@213 54 string ")"]
adamc@38 55
adamc@38 56 and p_kind k = p_kind' false k
adamc@38 57
adamc@38 58 fun p_con' par env (c, _) =
adamc@38 59 case c of
adamc@38 60 TFun (t1, t2) => parenIf par (box [p_con' true env t1,
adamc@38 61 space,
adamc@38 62 string "->",
adamc@38 63 space,
adamc@38 64 p_con env t2])
adamc@38 65 | TCFun (x, k, c) => parenIf par (box [string x,
adamc@38 66 space,
adamc@38 67 string "::",
adamc@38 68 space,
adamc@38 69 p_kind k,
adamc@38 70 space,
adamc@38 71 string "->",
adamc@38 72 space,
adamc@38 73 p_con (E.pushCRel env x k) c])
adamc@38 74 | TRecord (CRecord (_, xcs), _) => box [string "{",
adamc@38 75 p_list (fn (x, c) =>
adamc@38 76 box [p_name env x,
adamc@38 77 space,
adamc@38 78 string ":",
adamc@38 79 space,
adamc@38 80 p_con env c]) xcs,
adamc@38 81 string "}"]
adamc@38 82 | TRecord c => box [string "$",
adamc@38 83 p_con' true env c]
adamc@38 84
adamc@38 85 | CRel n =>
adamc@38 86 if !debug then
adamc@38 87 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@38 88 else
adamc@38 89 string (#1 (E.lookupCRel env n))
adamc@38 90 | CNamed n =>
adamc@38 91 ((if !debug then
adamc@38 92 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@38 93 else
adamc@38 94 string (#1 (E.lookupCNamed env n)))
adamc@38 95 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@38 96 | CModProj (m1, ms, x) =>
adamc@38 97 let
adamc@109 98 val m1x = #1 (E.lookupStrNamed env m1)
adamc@109 99 handle E.UnboundNamed _ => "UNBOUND"
adamc@38 100
adamc@38 101 val m1s = if !debug then
adamc@38 102 m1x ^ "__" ^ Int.toString m1
adamc@38 103 else
adamc@38 104 m1x
adamc@38 105 in
adamc@146 106 p_list_sep (string ".") string (m1s :: ms @ [x])
adamc@38 107 end
adamc@38 108
adamc@38 109 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@38 110 space,
adamc@38 111 p_con' true env c2])
adamc@38 112 | CAbs (x, k, c) => parenIf par (box [string "fn",
adamc@38 113 space,
adamc@38 114 string x,
adamc@38 115 space,
adamc@38 116 string "::",
adamc@38 117 space,
adamc@38 118 p_kind k,
adamc@38 119 space,
adamc@38 120 string "=>",
adamc@38 121 space,
adamc@38 122 p_con (E.pushCRel env x k) c])
adamc@38 123
adamc@38 124 | CName s => box [string "#", string s]
adamc@38 125
adamc@38 126 | CRecord (k, xcs) =>
adamc@38 127 if !debug then
adamc@38 128 parenIf par (box [string "[",
adamc@38 129 p_list (fn (x, c) =>
adamc@38 130 box [p_con env x,
adamc@38 131 space,
adamc@38 132 string "=",
adamc@38 133 space,
adamc@38 134 p_con env c]) xcs,
adamc@38 135 string "]::",
adamc@38 136 p_kind k])
adamc@38 137 else
adamc@38 138 parenIf par (box [string "[",
adamc@38 139 p_list (fn (x, c) =>
adamc@38 140 box [p_con env x,
adamc@38 141 space,
adamc@38 142 string "=",
adamc@38 143 space,
adamc@38 144 p_con env c]) xcs,
adamc@38 145 string "]"])
adamc@38 146 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@38 147 space,
adamc@38 148 string "++",
adamc@38 149 space,
adamc@38 150 p_con env c2])
adamc@68 151 | CFold _ => string "fold"
adamc@87 152 | CUnit => string "()"
adamc@213 153
adamc@213 154 | CTuple cs => box [string "(",
adamc@213 155 p_list (p_con env) cs,
adamc@213 156 string ")"]
adamc@213 157 | CProj (c, n) => box [p_con env c,
adamc@213 158 string ".",
adamc@213 159 string (Int.toString n)]
adamc@38 160
adamc@38 161 and p_con env = p_con' false env
adamc@38 162
adamc@38 163 and p_name env (all as (c, _)) =
adamc@38 164 case c of
adamc@38 165 CName s => string s
adamc@38 166 | _ => p_con env all
adamc@38 167
adamc@176 168 fun p_patCon env pc =
adamc@176 169 case pc of
adamc@176 170 PConVar n =>
adamc@176 171 ((if !debug then
adamc@176 172 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@176 173 else
adamc@176 174 string (#1 (E.lookupENamed env n)))
adamc@176 175 handle E.UnboundRel _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@176 176 | PConProj (m1, ms, x) =>
adamc@176 177 let
adamc@176 178 val m1x = #1 (E.lookupStrNamed env m1)
adamc@176 179 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@176 180
adamc@176 181 val m1s = if !debug then
adamc@176 182 m1x ^ "__" ^ Int.toString m1
adamc@176 183 else
adamc@176 184 m1x
adamc@176 185 in
adamc@176 186 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@176 187 end
adamc@176 188
adamc@176 189 fun p_pat' par env (p, _) =
adamc@176 190 case p of
adamc@176 191 PWild => string "_"
adamc@182 192 | PVar (s, _) => string s
adamc@176 193 | PPrim p => Prim.p_t p
adamc@191 194 | PCon (_, pc, _, NONE) => p_patCon env pc
adamc@191 195 | PCon (_, pc, _, SOME p) => parenIf par (box [p_patCon env pc,
adamc@191 196 space,
adamc@191 197 p_pat' true env p])
adamc@176 198 | PRecord xps =>
adamc@176 199 box [string "{",
adamc@182 200 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
adamc@176 201 box [string x,
adamc@176 202 space,
adamc@176 203 string "=",
adamc@176 204 space,
adamc@176 205 p_pat env p]) xps,
adamc@176 206 string "}"]
adamc@176 207
adamc@176 208 and p_pat x = p_pat' false x
adamc@176 209
adamc@146 210 fun p_exp' par env (e, loc) =
adamc@38 211 case e of
adamc@38 212 EPrim p => Prim.p_t p
adamc@38 213 | ERel n =>
adamc@38 214 if !debug then
adamc@38 215 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@38 216 else
adamc@38 217 string (#1 (E.lookupERel env n))
adamc@38 218 | ENamed n =>
adamc@38 219 if !debug then
adamc@38 220 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@38 221 else
adamc@38 222 string (#1 (E.lookupENamed env n))
adamc@38 223 | EModProj (m1, ms, x) =>
adamc@38 224 let
adamc@38 225 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@146 226 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
adamc@38 227
adamc@38 228 val m1s = if !debug then
adamc@38 229 m1x ^ "__" ^ Int.toString m1
adamc@38 230 else
adamc@38 231 m1x
adamc@38 232 in
adamc@146 233 p_list_sep (string ".") string (m1s :: ms @ [x])
adamc@38 234 end
adamc@38 235
adamc@38 236 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@38 237 space,
adamc@38 238 p_exp' true env e2])
adamc@38 239 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@38 240 space,
adamc@38 241 string x,
adamc@38 242 space,
adamc@38 243 string ":",
adamc@38 244 space,
adamc@38 245 p_con env t,
adamc@38 246 space,
adamc@38 247 string "=>",
adamc@38 248 space,
adamc@38 249 p_exp (E.pushERel env x t) e])
adamc@38 250 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@38 251 space,
adamc@38 252 string "[",
adamc@38 253 p_con env c,
adamc@38 254 string "]"])
adamc@38 255 | ECAbs (x, k, e) => parenIf par (box [string "fn",
adamc@38 256 space,
adamc@38 257 string x,
adamc@38 258 space,
adamc@38 259 string "::",
adamc@38 260 space,
adamc@38 261 p_kind k,
adamc@38 262 space,
adamc@38 263 string "=>",
adamc@38 264 space,
adamc@38 265 p_exp (E.pushCRel env x k) e])
adamc@38 266
adamc@38 267 | ERecord xes => box [string "{",
adamc@38 268 p_list (fn (x, e, _) =>
adamc@38 269 box [p_name env x,
adamc@38 270 space,
adamc@38 271 string "=",
adamc@38 272 space,
adamc@38 273 p_exp env e]) xes,
adamc@38 274 string "}"]
adamc@38 275 | EField (e, c, {field, rest}) =>
adamc@38 276 if !debug then
adamc@38 277 box [p_exp' true env e,
adamc@38 278 string ".",
adamc@38 279 p_con' true env c,
adamc@38 280 space,
adamc@38 281 string "[",
adamc@38 282 p_con env field,
adamc@38 283 space,
adamc@38 284 string " in ",
adamc@38 285 space,
adamc@38 286 p_con env rest,
adamc@38 287 string "]"]
adamc@38 288 else
adamc@38 289 box [p_exp' true env e,
adamc@38 290 string ".",
adamc@38 291 p_con' true env c]
adamc@339 292 | EWith (e1, c, e2, {field, rest}) =>
adamc@339 293 parenIf par (if !debug then
adamc@339 294 box [p_exp env e1,
adamc@339 295 space,
adamc@339 296 string "with",
adamc@339 297 space,
adamc@339 298 p_con' true env c,
adamc@339 299 space,
adamc@339 300 string "=",
adamc@339 301 p_exp' true env e2,
adamc@339 302 space,
adamc@339 303 string "[",
adamc@339 304 p_con env field,
adamc@339 305 space,
adamc@339 306 string " in ",
adamc@339 307 space,
adamc@339 308 p_con env rest,
adamc@339 309 string "]"]
adamc@339 310 else
adamc@339 311 box [p_exp env e1,
adamc@339 312 space,
adamc@339 313 string "with",
adamc@339 314 space,
adamc@339 315 p_con' true env c,
adamc@339 316 space,
adamc@339 317 p_exp' true env e2])
adamc@149 318 | ECut (e, c, {field, rest}) =>
adamc@149 319 parenIf par (if !debug then
adamc@149 320 box [p_exp' true env e,
adamc@149 321 space,
adamc@149 322 string "--",
adamc@149 323 space,
adamc@149 324 p_con' true env c,
adamc@149 325 space,
adamc@149 326 string "[",
adamc@149 327 p_con env field,
adamc@149 328 space,
adamc@149 329 string " in ",
adamc@149 330 space,
adamc@149 331 p_con env rest,
adamc@149 332 string "]"]
adamc@149 333 else
adamc@149 334 box [p_exp' true env e,
adamc@149 335 space,
adamc@149 336 string "--",
adamc@149 337 space,
adamc@149 338 p_con' true env c])
adamc@72 339 | EFold _ => string "fold"
adamc@38 340
adamc@109 341 | EWrite e => box [string "write(",
adamc@109 342 p_exp env e,
adamc@109 343 string ")"]
adamc@109 344
adamc@288 345 | ECase (e, pes, {disc, result}) =>
adamc@288 346 parenIf par (box [string "case",
adamc@288 347 space,
adamc@288 348 p_exp env e,
adamc@288 349 space,
adamc@288 350 if !debug then
adamc@288 351 box [string "in",
adamc@288 352 space,
adamc@288 353 p_con env disc,
adamc@288 354 space,
adamc@288 355 string "return",
adamc@288 356 space,
adamc@288 357 p_con env result,
adamc@288 358 space]
adamc@288 359 else
adamc@288 360 box [],
adamc@288 361 string "of",
adamc@288 362 space,
adamc@288 363 p_list_sep (box [space, string "|", space])
adamc@288 364 (fn (p, e) => box [p_pat env p,
adamc@288 365 space,
adamc@288 366 string "=>",
adamc@288 367 space,
adamc@288 368 p_exp env e]) pes])
adamc@176 369
adamc@38 370 and p_exp env = p_exp' false env
adamc@38 371
adamc@38 372 fun p_named x n =
adamc@38 373 if !debug then
adamc@38 374 box [string x,
adamc@38 375 string "__",
adamc@38 376 string (Int.toString n)]
adamc@38 377 else
adamc@38 378 string x
adamc@38 379
adamc@191 380 fun p_datatype env (x, n, xs, cons) =
adamc@162 381 let
adamc@191 382 val k = (KType, ErrorMsg.dummySpan)
adamc@191 383 val env = E.pushCNamed env x n k NONE
adamc@191 384 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
adamc@162 385 in
adamc@162 386 box [string "datatype",
adamc@162 387 space,
adamc@162 388 string x,
adamc@191 389 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@162 390 space,
adamc@162 391 string "=",
adamc@162 392 space,
adamc@162 393 p_list_sep (box [space, string "|", space])
adamc@163 394 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 395 else string x
adamc@193 396 | (x, n, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 397 else string x, space, string "of", space, p_con env t])
adamc@162 398 cons]
adamc@162 399 end
adamc@162 400
adamc@38 401 fun p_sgn_item env (sgi, _) =
adamc@38 402 case sgi of
adamc@38 403 SgiConAbs (x, n, k) => box [string "con",
adamc@38 404 space,
adamc@38 405 p_named x n,
adamc@38 406 space,
adamc@38 407 string "::",
adamc@38 408 space,
adamc@38 409 p_kind k]
adamc@38 410 | SgiCon (x, n, k, c) => box [string "con",
adamc@38 411 space,
adamc@38 412 p_named x n,
adamc@38 413 space,
adamc@38 414 string "::",
adamc@38 415 space,
adamc@38 416 p_kind k,
adamc@38 417 space,
adamc@38 418 string "=",
adamc@38 419 space,
adamc@38 420 p_con env c]
adamc@162 421 | SgiDatatype x => p_datatype env x
adamc@191 422 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 423 let
adamc@162 424 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 425 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 426 in
adamc@162 427 box [string "datatype",
adamc@162 428 space,
adamc@162 429 string x,
adamc@162 430 space,
adamc@162 431 string "=",
adamc@162 432 space,
adamc@162 433 string "datatype",
adamc@162 434 space,
adamc@162 435 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 436 end
adamc@38 437 | SgiVal (x, n, c) => box [string "val",
adamc@38 438 space,
adamc@38 439 p_named x n,
adamc@38 440 space,
adamc@38 441 string ":",
adamc@38 442 space,
adamc@38 443 p_con env c]
adamc@38 444 | SgiStr (x, n, sgn) => box [string "structure",
adamc@38 445 space,
adamc@38 446 p_named x n,
adamc@38 447 space,
adamc@38 448 string ":",
adamc@38 449 space,
adamc@38 450 p_sgn env sgn]
adamc@64 451 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@64 452 space,
adamc@64 453 p_named x n,
adamc@64 454 space,
adamc@64 455 string "=",
adamc@64 456 space,
adamc@64 457 p_sgn env sgn]
adamc@246 458 | SgiTable (_, x, n, c) => box [string "table",
adamc@246 459 space,
adamc@246 460 p_named x n,
adamc@246 461 space,
adamc@246 462 string ":",
adamc@246 463 space,
adamc@246 464 p_con env c]
adamc@338 465 | SgiSequence (_, x, n) => box [string "sequence",
adamc@338 466 space,
adamc@338 467 p_named x n]
adamc@38 468
adamc@146 469 and p_sgn env (sgn, loc) =
adamc@38 470 case sgn of
adamc@38 471 SgnConst sgis => box [string "sig",
adamc@38 472 newline,
adamc@38 473 let
adamc@38 474 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@38 475 (p_sgn_item env sgi,
adamc@38 476 E.sgiBinds env sgi))
adamc@38 477 env sgis
adamc@38 478 in
adamc@38 479 p_list_sep newline (fn x => x) psgis
adamc@38 480 end,
adamc@38 481 newline,
adamc@38 482 string "end"]
adamc@146 483 | SgnVar n => string ((#1 (E.lookupSgnNamed env n))
adamc@146 484 handle E.UnboundNamed _ => "UNBOUND")
adamc@45 485 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@45 486 space,
adamc@45 487 string "(",
adamc@45 488 string x,
adamc@45 489 space,
adamc@45 490 string ":",
adamc@45 491 space,
adamc@45 492 p_sgn env sgn,
adamc@45 493 string ")",
adamc@45 494 space,
adamc@45 495 string ":",
adamc@45 496 space,
adamc@45 497 p_sgn (E.pushStrNamed env x n sgn) sgn']
adamc@45 498 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@45 499 space,
adamc@45 500 string "where",
adamc@45 501 space,
adamc@45 502 string "con",
adamc@45 503 space,
adamc@45 504 string x,
adamc@45 505 space,
adamc@45 506 string "=",
adamc@45 507 space,
adamc@45 508 p_con env c]
adamc@64 509 | SgnProj (m1, ms, x) =>
adamc@64 510 let
adamc@64 511 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@146 512 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
adamc@64 513
adamc@64 514 val m1s = if !debug then
adamc@64 515 m1x ^ "__" ^ Int.toString m1
adamc@64 516 else
adamc@64 517 m1x
adamc@64 518 in
adamc@64 519 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@64 520 end
adamc@38 521
adamc@124 522 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@124 523 space,
adamc@124 524 string ":",
adamc@124 525 space,
adamc@124 526 p_con env t,
adamc@124 527 space,
adamc@124 528 string "=",
adamc@124 529 space,
adamc@124 530 p_exp env e]
adamc@124 531
adamc@124 532 fun p_decl env (dAll as (d, _) : decl) =
adamc@38 533 case d of
adamc@38 534 DCon (x, n, k, c) => box [string "con",
adamc@38 535 space,
adamc@38 536 p_named x n,
adamc@38 537 space,
adamc@38 538 string "::",
adamc@38 539 space,
adamc@38 540 p_kind k,
adamc@38 541 space,
adamc@38 542 string "=",
adamc@38 543 space,
adamc@38 544 p_con env c]
adamc@162 545 | DDatatype x => p_datatype env x
adamc@191 546 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 547 let
adamc@162 548 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 549 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 550 in
adamc@162 551 box [string "datatype",
adamc@162 552 space,
adamc@162 553 string x,
adamc@162 554 space,
adamc@162 555 string "=",
adamc@162 556 space,
adamc@162 557 string "datatype",
adamc@162 558 space,
adamc@162 559 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 560 end
adamc@124 561 | DVal vi => box [string "val",
adamc@124 562 space,
adamc@124 563 p_vali env vi]
adamc@124 564 | DValRec vis =>
adamc@124 565 let
adamc@124 566 val env = E.declBinds env dAll
adamc@124 567 in
adamc@124 568 box [string "val",
adamc@124 569 space,
adamc@124 570 string "rec",
adamc@124 571 space,
adamc@124 572 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@124 573 end
adamc@38 574
adamc@38 575 | DSgn (x, n, sgn) => box [string "signature",
adamc@38 576 space,
adamc@38 577 p_named x n,
adamc@38 578 space,
adamc@38 579 string "=",
adamc@38 580 space,
adamc@38 581 p_sgn env sgn]
adamc@38 582 | DStr (x, n, sgn, str) => box [string "structure",
adamc@38 583 space,
adamc@38 584 p_named x n,
adamc@38 585 space,
adamc@38 586 string ":",
adamc@38 587 space,
adamc@38 588 p_sgn env sgn,
adamc@38 589 space,
adamc@38 590 string "=",
adamc@38 591 space,
adamc@38 592 p_str env str]
adamc@48 593 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 594 space,
adamc@48 595 string "structure",
adamc@48 596 space,
adamc@48 597 p_named x n,
adamc@48 598 space,
adamc@48 599 string ":",
adamc@48 600 space,
adamc@48 601 p_sgn env sgn]
adamc@109 602 | DExport (_, sgn, str) => box [string "export",
adamc@109 603 space,
adamc@109 604 p_str env str,
adamc@109 605 space,
adamc@109 606 string ":",
adamc@109 607 space,
adamc@109 608 p_sgn env sgn]
adamc@246 609 | DTable (_, x, n, c) => box [string "table",
adamc@246 610 space,
adamc@246 611 p_named x n,
adamc@246 612 space,
adamc@246 613 string ":",
adamc@246 614 space,
adamc@246 615 p_con env c]
adamc@338 616 | DSequence (_, x, n) => box [string "sequence",
adamc@338 617 space,
adamc@338 618 p_named x n]
adamc@271 619 | DDatabase s => box [string "database",
adamc@271 620 space,
adamc@271 621 string s]
adamc@38 622
adamc@38 623 and p_str env (str, _) =
adamc@38 624 case str of
adamc@38 625 StrConst ds => box [string "struct",
adamc@38 626 newline,
adamc@38 627 p_file env ds,
adamc@38 628 newline,
adamc@38 629 string "end"]
adamc@146 630 | StrVar n =>
adamc@146 631 let
adamc@146 632 val x = #1 (E.lookupStrNamed env n)
adamc@146 633 handle E.UnboundNamed _ => "UNBOUND"
adamc@146 634
adamc@146 635 val s = if !debug then
adamc@146 636 x ^ "__" ^ Int.toString n
adamc@146 637 else
adamc@146 638 x
adamc@146 639 in
adamc@146 640 string s
adamc@146 641 end
adamc@38 642 | StrProj (str, s) => box [p_str env str,
adamc@38 643 string ".",
adamc@38 644 string s]
adamc@45 645 | StrFun (x, n, sgn, sgn', str) =>
adamc@45 646 let
adamc@45 647 val env' = E.pushStrNamed env x n sgn
adamc@45 648 in
adamc@45 649 box [string "functor",
adamc@45 650 space,
adamc@45 651 string "(",
adamc@45 652 string x,
adamc@45 653 space,
adamc@45 654 string ":",
adamc@45 655 space,
adamc@45 656 p_sgn env sgn,
adamc@45 657 string ")",
adamc@45 658 space,
adamc@45 659 string ":",
adamc@45 660 space,
adamc@45 661 p_sgn env' sgn',
adamc@45 662 space,
adamc@45 663 string "=>",
adamc@45 664 space,
adamc@45 665 p_str env' str]
adamc@45 666 end
adamc@45 667 | StrApp (str1, str2) => box [p_str env str1,
adamc@45 668 string "(",
adamc@45 669 p_str env str2,
adamc@45 670 string ")"]
adamc@38 671
adamc@38 672 and p_file env file =
adamc@38 673 let
adamc@38 674 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@38 675 (p_decl env d,
adamc@38 676 E.declBinds env d))
adamc@38 677 env file
adamc@38 678 in
adamc@38 679 p_list_sep newline (fn x => x) pds
adamc@38 680 end
adamc@38 681
adamc@38 682 end