annotate src/expl_print.sml @ 455:d4a81273d4b1

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