annotate src/expl_print.sml @ 447:b77863cd0be2

Elaborating 'let'
author Adam Chlipala <adamc@hcoop.net>
date Sat, 01 Nov 2008 11:17:29 -0400
parents dfc8c991abd0
children 89f766f19d5b
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@445 292 | EConcat (e1, c1, e2, c2) =>
adamc@339 293 parenIf par (if !debug then
adamc@445 294 box [p_exp' true env e1,
adamc@445 295 space,
adamc@445 296 string ":",
adamc@445 297 space,
adamc@445 298 p_con env c1,
adamc@445 299 space,
adamc@445 300 string "++",
adamc@445 301 space,
adamc@445 302 p_exp' true env e2,
adamc@445 303 space,
adamc@445 304 string ":",
adamc@445 305 space,
adamc@445 306 p_con env c2]
adamc@445 307 else
adamc@445 308 box [p_exp' true env e1,
adamc@339 309 space,
adamc@339 310 string "with",
adamc@339 311 space,
adamc@339 312 p_exp' true env e2])
adamc@149 313 | ECut (e, c, {field, rest}) =>
adamc@149 314 parenIf par (if !debug then
adamc@149 315 box [p_exp' true env e,
adamc@149 316 space,
adamc@149 317 string "--",
adamc@149 318 space,
adamc@149 319 p_con' true env c,
adamc@149 320 space,
adamc@149 321 string "[",
adamc@149 322 p_con env field,
adamc@149 323 space,
adamc@149 324 string " in ",
adamc@149 325 space,
adamc@149 326 p_con env rest,
adamc@149 327 string "]"]
adamc@149 328 else
adamc@149 329 box [p_exp' true env e,
adamc@149 330 space,
adamc@149 331 string "--",
adamc@149 332 space,
adamc@149 333 p_con' true env c])
adamc@72 334 | EFold _ => string "fold"
adamc@38 335
adamc@109 336 | EWrite e => box [string "write(",
adamc@109 337 p_exp env e,
adamc@109 338 string ")"]
adamc@109 339
adamc@288 340 | ECase (e, pes, {disc, result}) =>
adamc@288 341 parenIf par (box [string "case",
adamc@288 342 space,
adamc@288 343 p_exp env e,
adamc@288 344 space,
adamc@288 345 if !debug then
adamc@288 346 box [string "in",
adamc@288 347 space,
adamc@288 348 p_con env disc,
adamc@288 349 space,
adamc@288 350 string "return",
adamc@288 351 space,
adamc@288 352 p_con env result,
adamc@288 353 space]
adamc@288 354 else
adamc@288 355 box [],
adamc@288 356 string "of",
adamc@288 357 space,
adamc@288 358 p_list_sep (box [space, string "|", space])
adamc@288 359 (fn (p, e) => box [p_pat env p,
adamc@288 360 space,
adamc@288 361 string "=>",
adamc@288 362 space,
adamc@288 363 p_exp env e]) pes])
adamc@176 364
adamc@38 365 and p_exp env = p_exp' false env
adamc@38 366
adamc@38 367 fun p_named x n =
adamc@38 368 if !debug then
adamc@38 369 box [string x,
adamc@38 370 string "__",
adamc@38 371 string (Int.toString n)]
adamc@38 372 else
adamc@38 373 string x
adamc@38 374
adamc@191 375 fun p_datatype env (x, n, xs, cons) =
adamc@162 376 let
adamc@191 377 val k = (KType, ErrorMsg.dummySpan)
adamc@191 378 val env = E.pushCNamed env x n k NONE
adamc@191 379 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
adamc@162 380 in
adamc@162 381 box [string "datatype",
adamc@162 382 space,
adamc@162 383 string x,
adamc@191 384 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@162 385 space,
adamc@162 386 string "=",
adamc@162 387 space,
adamc@162 388 p_list_sep (box [space, string "|", space])
adamc@163 389 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 390 else string x
adamc@193 391 | (x, n, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 392 else string x, space, string "of", space, p_con env t])
adamc@162 393 cons]
adamc@162 394 end
adamc@162 395
adamc@38 396 fun p_sgn_item env (sgi, _) =
adamc@38 397 case sgi of
adamc@38 398 SgiConAbs (x, n, k) => box [string "con",
adamc@38 399 space,
adamc@38 400 p_named x n,
adamc@38 401 space,
adamc@38 402 string "::",
adamc@38 403 space,
adamc@38 404 p_kind k]
adamc@38 405 | SgiCon (x, n, k, c) => box [string "con",
adamc@38 406 space,
adamc@38 407 p_named x n,
adamc@38 408 space,
adamc@38 409 string "::",
adamc@38 410 space,
adamc@38 411 p_kind k,
adamc@38 412 space,
adamc@38 413 string "=",
adamc@38 414 space,
adamc@38 415 p_con env c]
adamc@162 416 | SgiDatatype x => p_datatype env x
adamc@191 417 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 418 let
adamc@162 419 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 420 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 421 in
adamc@162 422 box [string "datatype",
adamc@162 423 space,
adamc@162 424 string x,
adamc@162 425 space,
adamc@162 426 string "=",
adamc@162 427 space,
adamc@162 428 string "datatype",
adamc@162 429 space,
adamc@162 430 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 431 end
adamc@38 432 | SgiVal (x, n, c) => box [string "val",
adamc@38 433 space,
adamc@38 434 p_named x n,
adamc@38 435 space,
adamc@38 436 string ":",
adamc@38 437 space,
adamc@38 438 p_con env c]
adamc@38 439 | SgiStr (x, n, sgn) => box [string "structure",
adamc@38 440 space,
adamc@38 441 p_named x n,
adamc@38 442 space,
adamc@38 443 string ":",
adamc@38 444 space,
adamc@38 445 p_sgn env sgn]
adamc@64 446 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@64 447 space,
adamc@64 448 p_named x n,
adamc@64 449 space,
adamc@64 450 string "=",
adamc@64 451 space,
adamc@64 452 p_sgn env sgn]
adamc@246 453 | SgiTable (_, x, n, c) => box [string "table",
adamc@246 454 space,
adamc@246 455 p_named x n,
adamc@246 456 space,
adamc@246 457 string ":",
adamc@246 458 space,
adamc@246 459 p_con env c]
adamc@338 460 | SgiSequence (_, x, n) => box [string "sequence",
adamc@338 461 space,
adamc@338 462 p_named x n]
adamc@38 463
adamc@146 464 and p_sgn env (sgn, loc) =
adamc@38 465 case sgn of
adamc@38 466 SgnConst sgis => box [string "sig",
adamc@38 467 newline,
adamc@38 468 let
adamc@38 469 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@38 470 (p_sgn_item env sgi,
adamc@38 471 E.sgiBinds env sgi))
adamc@38 472 env sgis
adamc@38 473 in
adamc@38 474 p_list_sep newline (fn x => x) psgis
adamc@38 475 end,
adamc@38 476 newline,
adamc@38 477 string "end"]
adamc@146 478 | SgnVar n => string ((#1 (E.lookupSgnNamed env n))
adamc@146 479 handle E.UnboundNamed _ => "UNBOUND")
adamc@45 480 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@45 481 space,
adamc@45 482 string "(",
adamc@45 483 string x,
adamc@45 484 space,
adamc@45 485 string ":",
adamc@45 486 space,
adamc@45 487 p_sgn env sgn,
adamc@45 488 string ")",
adamc@45 489 space,
adamc@45 490 string ":",
adamc@45 491 space,
adamc@45 492 p_sgn (E.pushStrNamed env x n sgn) sgn']
adamc@45 493 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@45 494 space,
adamc@45 495 string "where",
adamc@45 496 space,
adamc@45 497 string "con",
adamc@45 498 space,
adamc@45 499 string x,
adamc@45 500 space,
adamc@45 501 string "=",
adamc@45 502 space,
adamc@45 503 p_con env c]
adamc@64 504 | SgnProj (m1, ms, x) =>
adamc@64 505 let
adamc@64 506 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@146 507 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
adamc@64 508
adamc@64 509 val m1s = if !debug then
adamc@64 510 m1x ^ "__" ^ Int.toString m1
adamc@64 511 else
adamc@64 512 m1x
adamc@64 513 in
adamc@64 514 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@64 515 end
adamc@38 516
adamc@124 517 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@124 518 space,
adamc@124 519 string ":",
adamc@124 520 space,
adamc@124 521 p_con env t,
adamc@124 522 space,
adamc@124 523 string "=",
adamc@124 524 space,
adamc@124 525 p_exp env e]
adamc@124 526
adamc@124 527 fun p_decl env (dAll as (d, _) : decl) =
adamc@38 528 case d of
adamc@38 529 DCon (x, n, k, c) => box [string "con",
adamc@38 530 space,
adamc@38 531 p_named x n,
adamc@38 532 space,
adamc@38 533 string "::",
adamc@38 534 space,
adamc@38 535 p_kind k,
adamc@38 536 space,
adamc@38 537 string "=",
adamc@38 538 space,
adamc@38 539 p_con env c]
adamc@162 540 | DDatatype x => p_datatype env x
adamc@191 541 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 542 let
adamc@162 543 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 544 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 545 in
adamc@162 546 box [string "datatype",
adamc@162 547 space,
adamc@162 548 string x,
adamc@162 549 space,
adamc@162 550 string "=",
adamc@162 551 space,
adamc@162 552 string "datatype",
adamc@162 553 space,
adamc@162 554 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 555 end
adamc@124 556 | DVal vi => box [string "val",
adamc@124 557 space,
adamc@124 558 p_vali env vi]
adamc@124 559 | DValRec vis =>
adamc@124 560 let
adamc@124 561 val env = E.declBinds env dAll
adamc@124 562 in
adamc@124 563 box [string "val",
adamc@124 564 space,
adamc@124 565 string "rec",
adamc@124 566 space,
adamc@124 567 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@124 568 end
adamc@38 569
adamc@38 570 | DSgn (x, n, sgn) => box [string "signature",
adamc@38 571 space,
adamc@38 572 p_named x n,
adamc@38 573 space,
adamc@38 574 string "=",
adamc@38 575 space,
adamc@38 576 p_sgn env sgn]
adamc@38 577 | DStr (x, n, sgn, str) => box [string "structure",
adamc@38 578 space,
adamc@38 579 p_named x n,
adamc@38 580 space,
adamc@38 581 string ":",
adamc@38 582 space,
adamc@38 583 p_sgn env sgn,
adamc@38 584 space,
adamc@38 585 string "=",
adamc@38 586 space,
adamc@38 587 p_str env str]
adamc@48 588 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 589 space,
adamc@48 590 string "structure",
adamc@48 591 space,
adamc@48 592 p_named x n,
adamc@48 593 space,
adamc@48 594 string ":",
adamc@48 595 space,
adamc@48 596 p_sgn env sgn]
adamc@109 597 | DExport (_, sgn, str) => box [string "export",
adamc@109 598 space,
adamc@109 599 p_str env str,
adamc@109 600 space,
adamc@109 601 string ":",
adamc@109 602 space,
adamc@109 603 p_sgn env sgn]
adamc@246 604 | DTable (_, x, n, c) => box [string "table",
adamc@246 605 space,
adamc@246 606 p_named x n,
adamc@246 607 space,
adamc@246 608 string ":",
adamc@246 609 space,
adamc@246 610 p_con env c]
adamc@338 611 | DSequence (_, x, n) => box [string "sequence",
adamc@338 612 space,
adamc@338 613 p_named x n]
adamc@271 614 | DDatabase s => box [string "database",
adamc@271 615 space,
adamc@271 616 string s]
adamc@38 617
adamc@38 618 and p_str env (str, _) =
adamc@38 619 case str of
adamc@38 620 StrConst ds => box [string "struct",
adamc@38 621 newline,
adamc@38 622 p_file env ds,
adamc@38 623 newline,
adamc@38 624 string "end"]
adamc@146 625 | StrVar n =>
adamc@146 626 let
adamc@146 627 val x = #1 (E.lookupStrNamed env n)
adamc@146 628 handle E.UnboundNamed _ => "UNBOUND"
adamc@146 629
adamc@146 630 val s = if !debug then
adamc@146 631 x ^ "__" ^ Int.toString n
adamc@146 632 else
adamc@146 633 x
adamc@146 634 in
adamc@146 635 string s
adamc@146 636 end
adamc@38 637 | StrProj (str, s) => box [p_str env str,
adamc@38 638 string ".",
adamc@38 639 string s]
adamc@45 640 | StrFun (x, n, sgn, sgn', str) =>
adamc@45 641 let
adamc@45 642 val env' = E.pushStrNamed env x n sgn
adamc@45 643 in
adamc@45 644 box [string "functor",
adamc@45 645 space,
adamc@45 646 string "(",
adamc@45 647 string x,
adamc@45 648 space,
adamc@45 649 string ":",
adamc@45 650 space,
adamc@45 651 p_sgn env sgn,
adamc@45 652 string ")",
adamc@45 653 space,
adamc@45 654 string ":",
adamc@45 655 space,
adamc@45 656 p_sgn env' sgn',
adamc@45 657 space,
adamc@45 658 string "=>",
adamc@45 659 space,
adamc@45 660 p_str env' str]
adamc@45 661 end
adamc@45 662 | StrApp (str1, str2) => box [p_str env str1,
adamc@45 663 string "(",
adamc@45 664 p_str env str2,
adamc@45 665 string ")"]
adamc@38 666
adamc@38 667 and p_file env file =
adamc@38 668 let
adamc@38 669 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@38 670 (p_decl env d,
adamc@38 671 E.declBinds env d))
adamc@38 672 env file
adamc@38 673 in
adamc@38 674 p_list_sep newline (fn x => x) pds
adamc@38 675 end
adamc@38 676
adamc@38 677 end