annotate src/expl_print.sml @ 460:d34834af4512

Cookies through explify
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Nov 2008 10:43:48 -0500
parents 787d4931fb07
children 40c737913075
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@38 474
adamc@146 475 and p_sgn env (sgn, loc) =
adamc@38 476 case sgn of
adamc@38 477 SgnConst sgis => box [string "sig",
adamc@38 478 newline,
adamc@38 479 let
adamc@38 480 val (psgis, _) = ListUtil.foldlMap (fn (sgi, env) =>
adamc@38 481 (p_sgn_item env sgi,
adamc@38 482 E.sgiBinds env sgi))
adamc@38 483 env sgis
adamc@38 484 in
adamc@38 485 p_list_sep newline (fn x => x) psgis
adamc@38 486 end,
adamc@38 487 newline,
adamc@38 488 string "end"]
adamc@146 489 | SgnVar n => string ((#1 (E.lookupSgnNamed env n))
adamc@146 490 handle E.UnboundNamed _ => "UNBOUND")
adamc@45 491 | SgnFun (x, n, sgn, sgn') => box [string "functor",
adamc@45 492 space,
adamc@45 493 string "(",
adamc@45 494 string x,
adamc@45 495 space,
adamc@45 496 string ":",
adamc@45 497 space,
adamc@45 498 p_sgn env sgn,
adamc@45 499 string ")",
adamc@45 500 space,
adamc@45 501 string ":",
adamc@45 502 space,
adamc@45 503 p_sgn (E.pushStrNamed env x n sgn) sgn']
adamc@45 504 | SgnWhere (sgn, x, c) => box [p_sgn env sgn,
adamc@45 505 space,
adamc@45 506 string "where",
adamc@45 507 space,
adamc@45 508 string "con",
adamc@45 509 space,
adamc@45 510 string x,
adamc@45 511 space,
adamc@45 512 string "=",
adamc@45 513 space,
adamc@45 514 p_con env c]
adamc@64 515 | SgnProj (m1, ms, x) =>
adamc@64 516 let
adamc@64 517 val (m1x, sgn) = E.lookupStrNamed env m1
adamc@146 518 handle E.UnboundNamed _ => ("UNBOUND", (SgnConst [], loc))
adamc@64 519
adamc@64 520 val m1s = if !debug then
adamc@64 521 m1x ^ "__" ^ Int.toString m1
adamc@64 522 else
adamc@64 523 m1x
adamc@64 524 in
adamc@64 525 p_list_sep (string ".") string (m1x :: ms @ [x])
adamc@64 526 end
adamc@38 527
adamc@124 528 fun p_vali env (x, n, t, e) = box [p_named x n,
adamc@124 529 space,
adamc@124 530 string ":",
adamc@124 531 space,
adamc@124 532 p_con env t,
adamc@124 533 space,
adamc@124 534 string "=",
adamc@124 535 space,
adamc@124 536 p_exp env e]
adamc@124 537
adamc@124 538 fun p_decl env (dAll as (d, _) : decl) =
adamc@38 539 case d of
adamc@38 540 DCon (x, n, k, c) => box [string "con",
adamc@38 541 space,
adamc@38 542 p_named x n,
adamc@38 543 space,
adamc@38 544 string "::",
adamc@38 545 space,
adamc@38 546 p_kind k,
adamc@38 547 space,
adamc@38 548 string "=",
adamc@38 549 space,
adamc@38 550 p_con env c]
adamc@162 551 | DDatatype x => p_datatype env x
adamc@191 552 | DDatatypeImp (x, _, m1, ms, x', _, _) =>
adamc@162 553 let
adamc@162 554 val m1x = #1 (E.lookupStrNamed env m1)
adamc@162 555 handle E.UnboundNamed _ => "UNBOUND_STR_" ^ Int.toString m1
adamc@162 556 in
adamc@162 557 box [string "datatype",
adamc@162 558 space,
adamc@162 559 string x,
adamc@162 560 space,
adamc@162 561 string "=",
adamc@162 562 space,
adamc@162 563 string "datatype",
adamc@162 564 space,
adamc@162 565 p_list_sep (string ".") string (m1x :: ms @ [x'])]
adamc@162 566 end
adamc@124 567 | DVal vi => box [string "val",
adamc@124 568 space,
adamc@124 569 p_vali env vi]
adamc@124 570 | DValRec vis =>
adamc@124 571 let
adamc@124 572 val env = E.declBinds env dAll
adamc@124 573 in
adamc@124 574 box [string "val",
adamc@124 575 space,
adamc@124 576 string "rec",
adamc@124 577 space,
adamc@124 578 p_list_sep (box [newline, string "and", space]) (p_vali env) vis]
adamc@124 579 end
adamc@38 580
adamc@38 581 | DSgn (x, n, sgn) => box [string "signature",
adamc@38 582 space,
adamc@38 583 p_named x n,
adamc@38 584 space,
adamc@38 585 string "=",
adamc@38 586 space,
adamc@38 587 p_sgn env sgn]
adamc@38 588 | DStr (x, n, sgn, str) => box [string "structure",
adamc@38 589 space,
adamc@38 590 p_named x n,
adamc@38 591 space,
adamc@38 592 string ":",
adamc@38 593 space,
adamc@38 594 p_sgn env sgn,
adamc@38 595 space,
adamc@38 596 string "=",
adamc@38 597 space,
adamc@38 598 p_str env str]
adamc@48 599 | DFfiStr (x, n, sgn) => box [string "extern",
adamc@48 600 space,
adamc@48 601 string "structure",
adamc@48 602 space,
adamc@48 603 p_named x n,
adamc@48 604 space,
adamc@48 605 string ":",
adamc@48 606 space,
adamc@48 607 p_sgn env sgn]
adamc@109 608 | DExport (_, sgn, str) => box [string "export",
adamc@109 609 space,
adamc@109 610 p_str env str,
adamc@109 611 space,
adamc@109 612 string ":",
adamc@109 613 space,
adamc@109 614 p_sgn env sgn]
adamc@246 615 | DTable (_, x, n, c) => box [string "table",
adamc@246 616 space,
adamc@246 617 p_named x n,
adamc@246 618 space,
adamc@246 619 string ":",
adamc@246 620 space,
adamc@246 621 p_con env c]
adamc@338 622 | DSequence (_, x, n) => box [string "sequence",
adamc@338 623 space,
adamc@338 624 p_named x n]
adamc@271 625 | DDatabase s => box [string "database",
adamc@271 626 space,
adamc@271 627 string s]
adamc@460 628 | DCookie (_, x, n, c) => box [string "cookie",
adamc@460 629 space,
adamc@460 630 p_named x n,
adamc@460 631 space,
adamc@460 632 string ":",
adamc@460 633 space,
adamc@460 634 p_con env c]
adamc@38 635
adamc@38 636 and p_str env (str, _) =
adamc@38 637 case str of
adamc@38 638 StrConst ds => box [string "struct",
adamc@38 639 newline,
adamc@38 640 p_file env ds,
adamc@38 641 newline,
adamc@38 642 string "end"]
adamc@146 643 | StrVar n =>
adamc@146 644 let
adamc@146 645 val x = #1 (E.lookupStrNamed env n)
adamc@146 646 handle E.UnboundNamed _ => "UNBOUND"
adamc@146 647
adamc@146 648 val s = if !debug then
adamc@146 649 x ^ "__" ^ Int.toString n
adamc@146 650 else
adamc@146 651 x
adamc@146 652 in
adamc@146 653 string s
adamc@146 654 end
adamc@38 655 | StrProj (str, s) => box [p_str env str,
adamc@38 656 string ".",
adamc@38 657 string s]
adamc@45 658 | StrFun (x, n, sgn, sgn', str) =>
adamc@45 659 let
adamc@45 660 val env' = E.pushStrNamed env x n sgn
adamc@45 661 in
adamc@45 662 box [string "functor",
adamc@45 663 space,
adamc@45 664 string "(",
adamc@45 665 string x,
adamc@45 666 space,
adamc@45 667 string ":",
adamc@45 668 space,
adamc@45 669 p_sgn env sgn,
adamc@45 670 string ")",
adamc@45 671 space,
adamc@45 672 string ":",
adamc@45 673 space,
adamc@45 674 p_sgn env' sgn',
adamc@45 675 space,
adamc@45 676 string "=>",
adamc@45 677 space,
adamc@45 678 p_str env' str]
adamc@45 679 end
adamc@45 680 | StrApp (str1, str2) => box [p_str env str1,
adamc@45 681 string "(",
adamc@45 682 p_str env str2,
adamc@45 683 string ")"]
adamc@38 684
adamc@38 685 and p_file env file =
adamc@38 686 let
adamc@38 687 val (pds, _) = ListUtil.foldlMap (fn (d, env) =>
adamc@38 688 (p_decl env d,
adamc@38 689 E.declBinds env d))
adamc@38 690 env file
adamc@38 691 in
adamc@38 692 p_list_sep newline (fn x => x) pds
adamc@38 693 end
adamc@38 694
adamc@38 695 end