annotate src/expl_print.sml @ 605:1a9171e31fd1

dlist2 working swimmingly
author Adam Chlipala <adamc@hcoop.net>
date Sat, 17 Jan 2009 09:55:12 -0500
parents ae03d09043c1
children 8998114760c1
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@449 86 ((if !debug then
adamc@449 87 string (#1 (E.lookupCRel env n) ^ "_" ^ Int.toString n)
adamc@449 88 else
adamc@449 89 string (#1 (E.lookupCRel env n)))
adamc@449 90 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@38 91 | CNamed n =>
adamc@38 92 ((if !debug then
adamc@38 93 string (#1 (E.lookupCNamed env n) ^ "__" ^ Int.toString n)
adamc@38 94 else
adamc@38 95 string (#1 (E.lookupCNamed env n)))
adamc@38 96 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@38 97 | CModProj (m1, ms, x) =>
adamc@38 98 let
adamc@109 99 val m1x = #1 (E.lookupStrNamed env m1)
adamc@480 100 handle E.UnboundNamed _ => "UNBOUND" ^ Int.toString m1
adamc@38 101
adamc@38 102 val m1s = if !debug then
adamc@38 103 m1x ^ "__" ^ Int.toString m1
adamc@38 104 else
adamc@38 105 m1x
adamc@38 106 in
adamc@146 107 p_list_sep (string ".") string (m1s :: ms @ [x])
adamc@38 108 end
adamc@38 109
adamc@38 110 | CApp (c1, c2) => parenIf par (box [p_con env c1,
adamc@38 111 space,
adamc@38 112 p_con' true env c2])
adamc@38 113 | CAbs (x, k, c) => parenIf par (box [string "fn",
adamc@38 114 space,
adamc@38 115 string x,
adamc@38 116 space,
adamc@38 117 string "::",
adamc@38 118 space,
adamc@38 119 p_kind k,
adamc@38 120 space,
adamc@38 121 string "=>",
adamc@38 122 space,
adamc@38 123 p_con (E.pushCRel env x k) c])
adamc@38 124
adamc@38 125 | CName s => box [string "#", string s]
adamc@38 126
adamc@38 127 | CRecord (k, xcs) =>
adamc@38 128 if !debug then
adamc@38 129 parenIf par (box [string "[",
adamc@38 130 p_list (fn (x, c) =>
adamc@38 131 box [p_con env x,
adamc@38 132 space,
adamc@38 133 string "=",
adamc@38 134 space,
adamc@38 135 p_con env c]) xcs,
adamc@38 136 string "]::",
adamc@38 137 p_kind k])
adamc@38 138 else
adamc@38 139 parenIf par (box [string "[",
adamc@38 140 p_list (fn (x, c) =>
adamc@38 141 box [p_con env x,
adamc@38 142 space,
adamc@38 143 string "=",
adamc@38 144 space,
adamc@38 145 p_con env c]) xcs,
adamc@38 146 string "]"])
adamc@38 147 | CConcat (c1, c2) => parenIf par (box [p_con' true env c1,
adamc@38 148 space,
adamc@38 149 string "++",
adamc@38 150 space,
adamc@38 151 p_con env c2])
adamc@68 152 | CFold _ => string "fold"
adamc@87 153 | CUnit => string "()"
adamc@213 154
adamc@213 155 | CTuple cs => box [string "(",
adamc@213 156 p_list (p_con env) cs,
adamc@213 157 string ")"]
adamc@213 158 | CProj (c, n) => box [p_con env c,
adamc@213 159 string ".",
adamc@213 160 string (Int.toString n)]
adamc@38 161
adamc@38 162 and p_con env = p_con' false env
adamc@38 163
adamc@38 164 and p_name env (all as (c, _)) =
adamc@38 165 case c of
adamc@38 166 CName s => string s
adamc@38 167 | _ => p_con env all
adamc@38 168
adamc@176 169 fun p_patCon env pc =
adamc@176 170 case pc of
adamc@176 171 PConVar n =>
adamc@176 172 ((if !debug then
adamc@176 173 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@176 174 else
adamc@176 175 string (#1 (E.lookupENamed env n)))
adamc@449 176 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@176 177 | PConProj (m1, ms, x) =>
adamc@176 178 let
adamc@176 179 val m1x = #1 (E.lookupStrNamed env m1)
adamc@176 180 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@176 181
adamc@176 182 val m1s = if !debug then
adamc@176 183 m1x ^ "__" ^ Int.toString m1
adamc@176 184 else
adamc@176 185 m1x
adamc@176 186 in
adamc@176 187 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@176 188 end
adamc@176 189
adamc@176 190 fun p_pat' par env (p, _) =
adamc@176 191 case p of
adamc@176 192 PWild => string "_"
adamc@182 193 | PVar (s, _) => string s
adamc@176 194 | PPrim p => Prim.p_t p
adamc@191 195 | PCon (_, pc, _, NONE) => p_patCon env pc
adamc@191 196 | PCon (_, pc, _, SOME p) => parenIf par (box [p_patCon env pc,
adamc@191 197 space,
adamc@191 198 p_pat' true env p])
adamc@176 199 | PRecord xps =>
adamc@176 200 box [string "{",
adamc@182 201 p_list_sep (box [string ",", space]) (fn (x, p, _) =>
adamc@176 202 box [string x,
adamc@176 203 space,
adamc@176 204 string "=",
adamc@176 205 space,
adamc@176 206 p_pat env p]) xps,
adamc@176 207 string "}"]
adamc@176 208
adamc@176 209 and p_pat x = p_pat' false x
adamc@176 210
adamc@146 211 fun p_exp' par env (e, loc) =
adamc@38 212 case e of
adamc@38 213 EPrim p => Prim.p_t p
adamc@38 214 | ERel n =>
adamc@449 215 ((if !debug then
adamc@449 216 string (#1 (E.lookupERel env n) ^ "_" ^ Int.toString n)
adamc@449 217 else
adamc@449 218 string (#1 (E.lookupERel env n)))
adamc@449 219 handle E.UnboundRel _ => string ("UNBOUND_REL" ^ Int.toString n))
adamc@38 220 | ENamed n =>
adamc@449 221 ((if !debug then
adamc@449 222 string (#1 (E.lookupENamed env n) ^ "__" ^ Int.toString n)
adamc@449 223 else
adamc@449 224 string (#1 (E.lookupENamed env n)))
adamc@449 225 handle E.UnboundNamed _ => string ("UNBOUND_NAMED" ^ Int.toString n))
adamc@38 226 | EModProj (m1, ms, x) =>
adamc@38 227 let
adamc@38 228 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@480 229 handle E.UnboundNamed _ => ("UNBOUND" ^ Int.toString m1, (SgnConst [], loc))
adamc@38 230
adamc@38 231 val m1s = if !debug then
adamc@38 232 m1x ^ "__" ^ Int.toString m1
adamc@38 233 else
adamc@38 234 m1x
adamc@38 235 in
adamc@146 236 p_list_sep (string ".") string (m1s :: ms @ [x])
adamc@38 237 end
adamc@38 238
adamc@38 239 | EApp (e1, e2) => parenIf par (box [p_exp env e1,
adamc@38 240 space,
adamc@38 241 p_exp' true env e2])
adamc@38 242 | EAbs (x, t, _, e) => parenIf par (box [string "fn",
adamc@38 243 space,
adamc@38 244 string x,
adamc@38 245 space,
adamc@38 246 string ":",
adamc@38 247 space,
adamc@38 248 p_con env t,
adamc@38 249 space,
adamc@38 250 string "=>",
adamc@38 251 space,
adamc@38 252 p_exp (E.pushERel env x t) e])
adamc@38 253 | ECApp (e, c) => parenIf par (box [p_exp env e,
adamc@38 254 space,
adamc@38 255 string "[",
adamc@38 256 p_con env c,
adamc@38 257 string "]"])
adamc@38 258 | ECAbs (x, k, e) => parenIf par (box [string "fn",
adamc@38 259 space,
adamc@38 260 string x,
adamc@38 261 space,
adamc@38 262 string "::",
adamc@38 263 space,
adamc@38 264 p_kind k,
adamc@38 265 space,
adamc@38 266 string "=>",
adamc@38 267 space,
adamc@38 268 p_exp (E.pushCRel env x k) e])
adamc@38 269
adamc@38 270 | ERecord xes => box [string "{",
adamc@38 271 p_list (fn (x, e, _) =>
adamc@38 272 box [p_name env x,
adamc@38 273 space,
adamc@38 274 string "=",
adamc@38 275 space,
adamc@38 276 p_exp env e]) xes,
adamc@38 277 string "}"]
adamc@38 278 | EField (e, c, {field, rest}) =>
adamc@38 279 if !debug then
adamc@38 280 box [p_exp' true env e,
adamc@38 281 string ".",
adamc@38 282 p_con' true env c,
adamc@38 283 space,
adamc@38 284 string "[",
adamc@38 285 p_con env field,
adamc@38 286 space,
adamc@38 287 string " in ",
adamc@38 288 space,
adamc@38 289 p_con env rest,
adamc@38 290 string "]"]
adamc@38 291 else
adamc@38 292 box [p_exp' true env e,
adamc@38 293 string ".",
adamc@38 294 p_con' true env c]
adamc@445 295 | EConcat (e1, c1, e2, c2) =>
adamc@339 296 parenIf par (if !debug then
adamc@445 297 box [p_exp' true env e1,
adamc@445 298 space,
adamc@445 299 string ":",
adamc@445 300 space,
adamc@445 301 p_con env c1,
adamc@445 302 space,
adamc@445 303 string "++",
adamc@445 304 space,
adamc@445 305 p_exp' true env e2,
adamc@445 306 space,
adamc@445 307 string ":",
adamc@445 308 space,
adamc@445 309 p_con env c2]
adamc@445 310 else
adamc@445 311 box [p_exp' true env e1,
adamc@339 312 space,
adamc@339 313 string "with",
adamc@339 314 space,
adamc@339 315 p_exp' true env e2])
adamc@149 316 | ECut (e, c, {field, rest}) =>
adamc@149 317 parenIf par (if !debug then
adamc@149 318 box [p_exp' true env e,
adamc@149 319 space,
adamc@149 320 string "--",
adamc@149 321 space,
adamc@149 322 p_con' true env c,
adamc@149 323 space,
adamc@149 324 string "[",
adamc@149 325 p_con env field,
adamc@149 326 space,
adamc@149 327 string " in ",
adamc@149 328 space,
adamc@149 329 p_con env rest,
adamc@149 330 string "]"]
adamc@149 331 else
adamc@149 332 box [p_exp' true env e,
adamc@149 333 space,
adamc@149 334 string "--",
adamc@149 335 space,
adamc@149 336 p_con' true env c])
adamc@493 337 | ECutMulti (e, c, {rest}) =>
adamc@493 338 parenIf par (if !debug then
adamc@493 339 box [p_exp' true env e,
adamc@493 340 space,
adamc@493 341 string "---",
adamc@493 342 space,
adamc@493 343 p_con' true env c,
adamc@493 344 space,
adamc@493 345 string "[",
adamc@493 346 p_con env rest,
adamc@493 347 string "]"]
adamc@493 348 else
adamc@493 349 box [p_exp' true env e,
adamc@493 350 space,
adamc@493 351 string "---",
adamc@493 352 space,
adamc@493 353 p_con' true env c])
adamc@72 354 | EFold _ => string "fold"
adamc@38 355
adamc@109 356 | EWrite e => box [string "write(",
adamc@109 357 p_exp env e,
adamc@109 358 string ")"]
adamc@109 359
adamc@288 360 | ECase (e, pes, {disc, result}) =>
adamc@288 361 parenIf par (box [string "case",
adamc@288 362 space,
adamc@288 363 p_exp env e,
adamc@288 364 space,
adamc@288 365 if !debug then
adamc@288 366 box [string "in",
adamc@288 367 space,
adamc@288 368 p_con env disc,
adamc@288 369 space,
adamc@288 370 string "return",
adamc@288 371 space,
adamc@288 372 p_con env result,
adamc@288 373 space]
adamc@288 374 else
adamc@288 375 box [],
adamc@288 376 string "of",
adamc@288 377 space,
adamc@288 378 p_list_sep (box [space, string "|", space])
adamc@288 379 (fn (p, e) => box [p_pat env p,
adamc@288 380 space,
adamc@288 381 string "=>",
adamc@288 382 space,
adamc@288 383 p_exp env e]) pes])
adamc@176 384
adamc@449 385 | ELet (x, t, e1, e2) => box [string "let",
adamc@449 386 space,
adamc@449 387 string x,
adamc@449 388 space,
adamc@449 389 string ":",
adamc@453 390 space,
adamc@449 391 p_con env t,
adamc@449 392 space,
adamc@449 393 string "=",
adamc@449 394 space,
adamc@449 395 p_exp env e1,
adamc@449 396 space,
adamc@449 397 string "in",
adamc@449 398 newline,
adamc@449 399 p_exp (E.pushERel env x t) e2]
adamc@449 400
adamc@449 401
adamc@449 402
adamc@38 403 and p_exp env = p_exp' false env
adamc@38 404
adamc@38 405 fun p_named x n =
adamc@38 406 if !debug then
adamc@38 407 box [string x,
adamc@38 408 string "__",
adamc@38 409 string (Int.toString n)]
adamc@38 410 else
adamc@38 411 string x
adamc@38 412
adamc@191 413 fun p_datatype env (x, n, xs, cons) =
adamc@162 414 let
adamc@191 415 val k = (KType, ErrorMsg.dummySpan)
adamc@191 416 val env = E.pushCNamed env x n k NONE
adamc@191 417 val env = foldl (fn (x, env) => E.pushCRel env x k) env xs
adamc@162 418 in
adamc@162 419 box [string "datatype",
adamc@162 420 space,
adamc@162 421 string x,
adamc@191 422 p_list_sep (box []) (fn x => box [space, string x]) xs,
adamc@162 423 space,
adamc@162 424 string "=",
adamc@162 425 space,
adamc@162 426 p_list_sep (box [space, string "|", space])
adamc@163 427 (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 428 else string x
adamc@193 429 | (x, n, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n))
adamc@163 430 else string x, space, string "of", space, p_con env t])
adamc@162 431 cons]
adamc@162 432 end
adamc@162 433
adamc@38 434 fun p_sgn_item env (sgi, _) =
adamc@38 435 case sgi of
adamc@38 436 SgiConAbs (x, n, k) => box [string "con",
adamc@38 437 space,
adamc@38 438 p_named x n,
adamc@38 439 space,
adamc@38 440 string "::",
adamc@38 441 space,
adamc@38 442 p_kind k]
adamc@38 443 | SgiCon (x, n, k, c) => box [string "con",
adamc@38 444 space,
adamc@38 445 p_named x n,
adamc@38 446 space,
adamc@38 447 string "::",
adamc@38 448 space,
adamc@38 449 p_kind k,
adamc@38 450 space,
adamc@38 451 string "=",
adamc@38 452 space,
adamc@38 453 p_con env c]
adamc@162 454 | SgiDatatype x => p_datatype env x
adamc@191 455 | SgiDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 456 let
adamc@162 457 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 458 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 459 in
adamc@162 460 box [string "datatype",
adamc@162 461 space,
adamc@162 462 string x,
adamc@162 463 space,
adamc@162 464 string "=",
adamc@162 465 space,
adamc@162 466 string "datatype",
adamc@162 467 space,
adamc@162 468 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 469 end
adamc@38 470 | SgiVal (x, n, c) => box [string "val",
adamc@38 471 space,
adamc@38 472 p_named x n,
adamc@38 473 space,
adamc@38 474 string ":",
adamc@38 475 space,
adamc@38 476 p_con env c]
adamc@38 477 | SgiStr (x, n, sgn) => box [string "structure",
adamc@38 478 space,
adamc@38 479 p_named x n,
adamc@38 480 space,
adamc@38 481 string ":",
adamc@38 482 space,
adamc@38 483 p_sgn env sgn]
adamc@64 484 | SgiSgn (x, n, sgn) => box [string "signature",
adamc@64 485 space,
adamc@64 486 p_named x n,
adamc@64 487 space,
adamc@64 488 string "=",
adamc@64 489 space,
adamc@64 490 p_sgn env sgn]
adamc@38 491
adamc@146 492 and p_sgn env (sgn, loc) =
adamc@38 493 case sgn of
adamc@38 494 SgnConst sgis => box [string "sig",
adamc@38 495 newline,
adamc@38 496 let
adamc@38 497 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@38 498 (p_sgn_item env sgi,
adamc@38 499 E.sgiBinds env sgi))
adamc@38 500 env sgis
adamc@38 501 in
adamc@38 502 p_list_sep newline (fn x => x) psgis
adamc@38 503 end,
adamc@38 504 newline,
adamc@38 505 string "end"]
adamc@146 506 | SgnVar n => string ((#1 (E.lookupSgnNamed env n))
adamc@480 507 handle E.UnboundNamed _ => "UNBOUND" ^ Int.toString n)
adamc@45 508 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@45 509 space,
adamc@45 510 string "(",
adamc@480 511 p_named x n,
adamc@45 512 space,
adamc@45 513 string ":",
adamc@45 514 space,
adamc@45 515 p_sgn env sgn,
adamc@45 516 string ")",
adamc@45 517 space,
adamc@45 518 string ":",
adamc@45 519 space,
adamc@45 520 p_sgn (E.pushStrNamed env x n sgn) sgn']
adamc@45 521 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@45 522 space,
adamc@45 523 string "where",
adamc@45 524 space,
adamc@45 525 string "con",
adamc@45 526 space,
adamc@45 527 string x,
adamc@45 528 space,
adamc@45 529 string "=",
adamc@45 530 space,
adamc@45 531 p_con env c]
adamc@64 532 | SgnProj (m1, ms, x) =>
adamc@64 533 let
adamc@64 534 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@480 535 handle E.UnboundNamed _ => ("UNBOUND" ^ Int.toString m1, (SgnConst [], loc))
adamc@64 536
adamc@64 537 val m1s = if !debug then
adamc@64 538 m1x ^ "__" ^ Int.toString m1
adamc@64 539 else
adamc@64 540 m1x
adamc@64 541 in
adamc@64 542 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@64 543 end
adamc@38 544
adamc@124 545 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@124 546 space,
adamc@124 547 string ":",
adamc@124 548 space,
adamc@124 549 p_con env t,
adamc@124 550 space,
adamc@124 551 string "=",
adamc@124 552 space,
adamc@124 553 p_exp env e]
adamc@124 554
adamc@124 555 fun p_decl env (dAll as (d, _) : decl) =
adamc@38 556 case d of
adamc@38 557 DCon (x, n, k, c) => box [string "con",
adamc@38 558 space,
adamc@38 559 p_named x n,
adamc@38 560 space,
adamc@38 561 string "::",
adamc@38 562 space,
adamc@38 563 p_kind k,
adamc@38 564 space,
adamc@38 565 string "=",
adamc@38 566 space,
adamc@38 567 p_con env c]
adamc@162 568 | DDatatype x => p_datatype env x
adamc@191 569 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 570 let
adamc@162 571 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 572 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 573 in
adamc@162 574 box [string "datatype",
adamc@162 575 space,
adamc@162 576 string x,
adamc@162 577 space,
adamc@162 578 string "=",
adamc@162 579 space,
adamc@162 580 string "datatype",
adamc@162 581 space,
adamc@162 582 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 583 end
adamc@124 584 | DVal vi => box [string "val",
adamc@124 585 space,
adamc@124 586 p_vali env vi]
adamc@124 587 | DValRec vis =>
adamc@124 588 let
adamc@124 589 val env = E.declBinds env dAll
adamc@124 590 in
adamc@124 591 box [string "val",
adamc@124 592 space,
adamc@124 593 string "rec",
adamc@124 594 space,
adamc@124 595 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@124 596 end
adamc@38 597
adamc@38 598 | DSgn (x, n, sgn) => box [string "signature",
adamc@38 599 space,
adamc@38 600 p_named x n,
adamc@38 601 space,
adamc@38 602 string "=",
adamc@38 603 space,
adamc@38 604 p_sgn env sgn]
adamc@38 605 | DStr (x, n, sgn, str) => box [string "structure",
adamc@38 606 space,
adamc@38 607 p_named x n,
adamc@38 608 space,
adamc@38 609 string ":",
adamc@38 610 space,
adamc@38 611 p_sgn env sgn,
adamc@38 612 space,
adamc@38 613 string "=",
adamc@38 614 space,
adamc@38 615 p_str env str]
adamc@48 616 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 617 space,
adamc@48 618 string "structure",
adamc@48 619 space,
adamc@48 620 p_named x n,
adamc@48 621 space,
adamc@48 622 string ":",
adamc@48 623 space,
adamc@48 624 p_sgn env sgn]
adamc@109 625 | DExport (_, sgn, str) => box [string "export",
adamc@109 626 space,
adamc@109 627 p_str env str,
adamc@109 628 space,
adamc@109 629 string ":",
adamc@109 630 space,
adamc@109 631 p_sgn env sgn]
adamc@246 632 | DTable (_, x, n, c) => box [string "table",
adamc@246 633 space,
adamc@246 634 p_named x n,
adamc@246 635 space,
adamc@246 636 string ":",
adamc@246 637 space,
adamc@246 638 p_con env c]
adamc@338 639 | DSequence (_, x, n) => box [string "sequence",
adamc@338 640 space,
adamc@338 641 p_named x n]
adamc@271 642 | DDatabase s => box [string "database",
adamc@271 643 space,
adamc@271 644 string s]
adamc@460 645 | DCookie (_, x, n, c) => box [string "cookie",
adamc@460 646 space,
adamc@460 647 p_named x n,
adamc@460 648 space,
adamc@460 649 string ":",
adamc@460 650 space,
adamc@460 651 p_con env c]
adamc@38 652
adamc@38 653 and p_str env (str, _) =
adamc@38 654 case str of
adamc@38 655 StrConst ds => box [string "struct",
adamc@38 656 newline,
adamc@38 657 p_file env ds,
adamc@38 658 newline,
adamc@38 659 string "end"]
adamc@146 660 | StrVar n =>
adamc@146 661 let
adamc@146 662 val x = #1 (E.lookupStrNamed env n)
adamc@480 663 handle E.UnboundNamed _ => "UNBOUND" ^ Int.toString n
adamc@146 664
adamc@146 665 val s = if !debug then
adamc@146 666 x ^ "__" ^ Int.toString n
adamc@146 667 else
adamc@146 668 x
adamc@146 669 in
adamc@146 670 string s
adamc@146 671 end
adamc@38 672 | StrProj (str, s) => box [p_str env str,
adamc@38 673 string ".",
adamc@38 674 string s]
adamc@45 675 | StrFun (x, n, sgn, sgn', str) =>
adamc@45 676 let
adamc@45 677 val env' = E.pushStrNamed env x n sgn
adamc@45 678 in
adamc@45 679 box [string "functor",
adamc@45 680 space,
adamc@45 681 string "(",
adamc@480 682 p_named x n,
adamc@45 683 space,
adamc@45 684 string ":",
adamc@45 685 space,
adamc@45 686 p_sgn env sgn,
adamc@45 687 string ")",
adamc@45 688 space,
adamc@45 689 string ":",
adamc@45 690 space,
adamc@45 691 p_sgn env' sgn',
adamc@45 692 space,
adamc@45 693 string "=>",
adamc@45 694 space,
adamc@45 695 p_str env' str]
adamc@45 696 end
adamc@45 697 | StrApp (str1, str2) => box [p_str env str1,
adamc@45 698 string "(",
adamc@45 699 p_str env str2,
adamc@45 700 string ")"]
adamc@38 701
adamc@38 702 and p_file env file =
adamc@38 703 let
adamc@38 704 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@38 705 (p_decl env d,
adamc@38 706 E.declBinds env d))
adamc@38 707 env file
adamc@38 708 in
adamc@38 709 p_list_sep newline (fn x => x) pds
adamc@38 710 end
adamc@38 711
adamc@38 712 end