annotate src/expl_print.sml @ 623:588b9d16b00a

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