annotate src/source_print.sml @ 167:2be573fec9a6

Unurlifying a datatype; longjmp-based error signaling mechanism
author Adam Chlipala <adamc@hcoop.net>
date Tue, 29 Jul 2008 15:25:42 -0400
parents 34ccd7d2bea8
children a158f8c5aa55
rev   line source
adamc@1 1 (* Copyright (c) 2008, Adam Chlipala
adamc@1 2 * All rights reserved.
adamc@1 3 *
adamc@1 4 * Redistribution and use in source and binary forms, with or without
adamc@1 5 * modification, are permitted provided that the following conditions are met:
adamc@1 6 *
adamc@1 7 * - Redistributions of source code must retain the above copyright notice,
adamc@1 8 * this list of conditions and the following disclaimer.
adamc@1 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@1 10 * this list of conditions and the following disclaimer in the documentation
adamc@1 11 * and/or other materials provided with the distribution.
adamc@1 12 * - The names of contributors may not be used to endorse or promote products
adamc@1 13 * derived from this software without specific prior written permission.
adamc@1 14 *
adamc@1 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@1 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@1 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@1 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@1 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@1 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@1 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@1 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@1 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@1 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@1 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@1 26 *)
adamc@1 27
adamc@1 28 (* Pretty-printing Laconic/Web *)
adamc@1 29
adamc@4 30 structure SourcePrint :> SOURCE_PRINT = struct
adamc@1 31
adamc@1 32 open Print.PD
adamc@1 33 open Print
adamc@1 34
adamc@4 35 open Source
adamc@1 36
adamc@1 37 fun p_kind' par (k, _) =
adamc@1 38 case k of
adamc@1 39 KType => string "Type"
adamc@1 40 | KArrow (k1, k2) => parenIf par (box [p_kind' true k1,
adamc@1 41 space,
adamc@1 42 string "->",
adamc@1 43 space,
adamc@1 44 p_kind k2])
adamc@1 45 | KName => string "Name"
adamc@1 46 | KRecord k => box [string "{", p_kind k, string "}"]
adamc@82 47 | KUnit => string "Unit"
adamc@18 48 | KWild => string "_"
adamc@1 49
adamc@1 50 and p_kind k = p_kind' false k
adamc@1 51
adamc@1 52 fun p_explicitness e =
adamc@1 53 case e of
adamc@1 54 Explicit => string "::"
adamc@1 55 | Implicit => string ":::"
adamc@1 56
adamc@1 57 fun p_con' par (c, _) =
adamc@1 58 case c of
adamc@1 59 CAnnot (c, k) => box [string "(",
adamc@1 60 p_con c,
adamc@1 61 space,
adamc@1 62 string "::",
adamc@1 63 space,
adamc@1 64 p_kind k,
adamc@1 65 string ")"]
adamc@1 66
adamc@1 67 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
adamc@1 68 space,
adamc@1 69 string "->",
adamc@1 70 space,
adamc@1 71 p_con t2])
adamc@1 72 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@1 73 space,
adamc@1 74 p_explicitness e,
adamc@1 75 space,
adamc@1 76 p_kind k,
adamc@1 77 space,
adamc@1 78 string "->",
adamc@1 79 space,
adamc@1 80 p_con c])
adamc@85 81 | TDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@85 82 space,
adamc@85 83 string "~",
adamc@85 84 space,
adamc@85 85 p_con c2,
adamc@85 86 space,
adamc@85 87 string "->",
adamc@85 88 space,
adamc@85 89 p_con c3])
adamc@1 90 | TRecord (CRecord xcs, _) => box [string "{",
adamc@1 91 p_list (fn (x, c) =>
adamc@20 92 box [p_name x,
adamc@1 93 space,
adamc@1 94 string ":",
adamc@1 95 space,
adamc@1 96 p_con c]) xcs,
adamc@1 97 string "}"]
adamc@1 98 | TRecord c => box [string "$",
adamc@1 99 p_con' true c]
adamc@1 100
adamc@34 101 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@1 102 | CApp (c1, c2) => parenIf par (box [p_con c1,
adamc@1 103 space,
adamc@1 104 p_con' true c2])
adamc@67 105 | CAbs (x, NONE, c) => parenIf par (box [string "fn",
adamc@67 106 space,
adamc@67 107 string x,
adamc@67 108 space,
adamc@67 109 string "=>",
adamc@67 110 space,
adamc@67 111 p_con c])
adamc@67 112 | CAbs (x, SOME k, c) => parenIf par (box [string "fn",
adamc@67 113 space,
adamc@67 114 string x,
adamc@67 115 space,
adamc@67 116 string "::",
adamc@67 117 space,
adamc@67 118 p_kind k,
adamc@67 119 space,
adamc@67 120 string "=>",
adamc@67 121 space,
adamc@67 122 p_con c])
adamc@84 123 | CDisjoint (c1, c2, c3) => parenIf par (box [p_con c1,
adamc@84 124 space,
adamc@84 125 string "~",
adamc@84 126 space,
adamc@84 127 p_con c2,
adamc@84 128 space,
adamc@84 129 string "=>",
adamc@84 130 space,
adamc@84 131 p_con c3])
adamc@1 132
adamc@1 133 | CName s => box [string "#", string s]
adamc@1 134
adamc@1 135 | CRecord xcs => box [string "[",
adamc@1 136 p_list (fn (x, c) =>
adamc@1 137 box [p_con x,
adamc@1 138 space,
adamc@1 139 string "=",
adamc@1 140 space,
adamc@1 141 p_con c]) xcs,
adamc@1 142 string "]"]
adamc@1 143 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
adamc@1 144 space,
adamc@1 145 string "++",
adamc@1 146 space,
adamc@1 147 p_con c2])
adamc@67 148 | CFold => string "fold"
adamc@82 149
adamc@82 150 | CUnit => string "()"
adamc@82 151
adamc@18 152 | CWild k => box [string "(_",
adamc@18 153 space,
adamc@18 154 string "::",
adamc@18 155 space,
adamc@18 156 p_kind k]
adamc@1 157
adamc@1 158 and p_con c = p_con' false c
adamc@1 159
adamc@20 160 and p_name (all as (c, _)) =
adamc@20 161 case c of
adamc@20 162 CName s => string s
adamc@20 163 | _ => p_con all
adamc@20 164
adamc@8 165 fun p_exp' par (e, _) =
adamc@8 166 case e of
adamc@8 167 EAnnot (e, t) => box [string "(",
adamc@8 168 p_exp e,
adamc@8 169 space,
adamc@8 170 string ":",
adamc@8 171 space,
adamc@8 172 p_con t,
adamc@8 173 string ")"]
adamc@8 174
adamc@14 175 | EPrim p => Prim.p_t p
adamc@34 176 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 177 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 178 space,
adamc@8 179 p_exp' true e2])
adamc@8 180 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 181 space,
adamc@8 182 string x,
adamc@8 183 space,
adamc@8 184 string "=>",
adamc@8 185 space,
adamc@8 186 p_exp e])
adamc@8 187 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 188 space,
adamc@8 189 string x,
adamc@8 190 space,
adamc@8 191 string ":",
adamc@8 192 space,
adamc@8 193 p_con t,
adamc@8 194 space,
adamc@8 195 string "=>",
adamc@8 196 space,
adamc@8 197 p_exp e])
adamc@8 198 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 199 space,
adamc@8 200 string "[",
adamc@8 201 p_con c,
adamc@8 202 string "]"])
adamc@8 203 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 204 space,
adamc@8 205 string x,
adamc@8 206 space,
adamc@8 207 p_explicitness exp,
adamc@8 208 space,
adamc@8 209 p_kind k,
adamc@8 210 space,
adamc@8 211 string "=>",
adamc@8 212 space,
adamc@8 213 p_exp e])
adamc@85 214 | EDisjoint (c1, c2, e) => parenIf par (box [p_con c1,
adamc@85 215 space,
adamc@85 216 string "~",
adamc@85 217 space,
adamc@85 218 p_con c2,
adamc@85 219 space,
adamc@85 220 string "=>",
adamc@85 221 space,
adamc@85 222 p_exp e])
adamc@8 223
adamc@12 224 | ERecord xes => box [string "{",
adamc@12 225 p_list (fn (x, e) =>
adamc@21 226 box [p_name x,
adamc@12 227 space,
adamc@12 228 string "=",
adamc@12 229 space,
adamc@12 230 p_exp e]) xes,
adamc@12 231 string "}"]
adamc@12 232 | EField (e, c) => box [p_exp' true e,
adamc@12 233 string ".",
adamc@12 234 p_con' true c]
adamc@149 235 | ECut (e, c) => parenIf par (box [p_exp' true e,
adamc@149 236 space,
adamc@149 237 string "--",
adamc@149 238 space,
adamc@149 239 p_con' true c])
adamc@71 240 | EFold => string "fold"
adamc@12 241
adamc@8 242 and p_exp e = p_exp' false e
adamc@8 243
adamc@156 244 fun p_datatype (x, cons) =
adamc@156 245 box [string "datatype",
adamc@156 246 space,
adamc@156 247 string x,
adamc@156 248 space,
adamc@156 249 string "=",
adamc@156 250 space,
adamc@156 251 p_list_sep (box [space, string "|", space])
adamc@156 252 (fn (x, NONE) => string x
adamc@156 253 | (x, SOME t) => box [string x, space, string "of", space, p_con t])
adamc@156 254 cons]
adamc@156 255
adamc@30 256 fun p_sgn_item (sgi, _) =
adamc@30 257 case sgi of
adamc@30 258 SgiConAbs (x, k) => box [string "con",
adamc@30 259 space,
adamc@30 260 string x,
adamc@30 261 space,
adamc@30 262 string "::",
adamc@30 263 space,
adamc@30 264 p_kind k]
adamc@30 265 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 266 space,
adamc@30 267 string x,
adamc@30 268 space,
adamc@30 269 string "=",
adamc@30 270 space,
adamc@30 271 p_con c]
adamc@30 272 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 273 space,
adamc@30 274 string x,
adamc@30 275 space,
adamc@30 276 string "::",
adamc@30 277 space,
adamc@30 278 p_kind k,
adamc@30 279 space,
adamc@30 280 string "=",
adamc@30 281 space,
adamc@30 282 p_con c]
adamc@156 283 | SgiDatatype x => p_datatype x
adamc@156 284 | SgiDatatypeImp (x, ms, x') =>
adamc@156 285 box [string "datatype",
adamc@156 286 space,
adamc@156 287 string x,
adamc@156 288 space,
adamc@156 289 string "=",
adamc@156 290 space,
adamc@156 291 string "datatype",
adamc@156 292 space,
adamc@156 293 p_list_sep (string ".") string (ms @ [x'])]
adamc@30 294 | SgiVal (x, c) => box [string "val",
adamc@30 295 space,
adamc@30 296 string x,
adamc@30 297 space,
adamc@30 298 string ":",
adamc@30 299 space,
adamc@30 300 p_con c]
adamc@30 301 | SgiStr (x, sgn) => box [string "structure",
adamc@30 302 space,
adamc@30 303 string x,
adamc@30 304 space,
adamc@30 305 string ":",
adamc@30 306 space,
adamc@30 307 p_sgn sgn]
adamc@59 308 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 309 space,
adamc@59 310 string x,
adamc@59 311 space,
adamc@59 312 string "=",
adamc@59 313 space,
adamc@59 314 p_sgn sgn]
adamc@58 315 | SgiInclude sgn => box [string "include",
adamc@58 316 space,
adamc@58 317 p_sgn sgn]
adamc@88 318 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 319 space,
adamc@88 320 p_con c1,
adamc@88 321 space,
adamc@88 322 string "~",
adamc@88 323 space,
adamc@88 324 p_con c2]
adamc@30 325
adamc@30 326 and p_sgn (sgn, _) =
adamc@30 327 case sgn of
adamc@30 328 SgnConst sgis => box [string "sig",
adamc@30 329 newline,
adamc@30 330 p_list_sep newline p_sgn_item sgis,
adamc@30 331 newline,
adamc@30 332 string "end"]
adamc@30 333 | SgnVar x => string x
adamc@40 334 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 335 space,
adamc@40 336 string "(",
adamc@40 337 string x,
adamc@40 338 space,
adamc@40 339 string ":",
adamc@40 340 p_sgn sgn,
adamc@40 341 string ")",
adamc@40 342 space,
adamc@40 343 string ":",
adamc@40 344 space,
adamc@40 345 p_sgn sgn']
adamc@42 346 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 347 space,
adamc@42 348 string "where",
adamc@42 349 space,
adamc@42 350 string "con",
adamc@42 351 space,
adamc@42 352 string x,
adamc@42 353 space,
adamc@42 354 string "=",
adamc@42 355 space,
adamc@42 356 p_con c]
adamc@59 357 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 358
adamc@42 359
adamc@123 360 fun p_vali (x, co, e) =
adamc@123 361 case co of
adamc@123 362 NONE => box [string x,
adamc@123 363 space,
adamc@123 364 string "=",
adamc@123 365 space,
adamc@123 366 p_exp e]
adamc@123 367 | SOME t => box [string x,
adamc@123 368 space,
adamc@123 369 string ":",
adamc@123 370 space,
adamc@123 371 p_con t,
adamc@123 372 space,
adamc@123 373 string "=",
adamc@123 374 space,
adamc@123 375 p_exp e]
adamc@123 376
adamc@1 377 fun p_decl ((d, _) : decl) =
adamc@1 378 case d of
adamc@1 379 DCon (x, NONE, c) => box [string "con",
adamc@1 380 space,
adamc@1 381 string x,
adamc@1 382 space,
adamc@1 383 string "=",
adamc@1 384 space,
adamc@1 385 p_con c]
adamc@1 386 | DCon (x, SOME k, c) => box [string "con",
adamc@1 387 space,
adamc@1 388 string x,
adamc@1 389 space,
adamc@1 390 string "::",
adamc@1 391 space,
adamc@1 392 p_kind k,
adamc@1 393 space,
adamc@1 394 string "=",
adamc@1 395 space,
adamc@1 396 p_con c]
adamc@156 397 | DDatatype x => p_datatype x
adamc@156 398 | DDatatypeImp (x, ms, x') =>
adamc@156 399 box [string "datatype",
adamc@156 400 space,
adamc@156 401 string x,
adamc@156 402 space,
adamc@156 403 string "=",
adamc@156 404 space,
adamc@156 405 string "datatype",
adamc@156 406 space,
adamc@156 407 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 408 | DVal vi => box [string "val",
adamc@123 409 space,
adamc@123 410 p_vali vi]
adamc@123 411 | DValRec vis => box [string "val",
adamc@123 412 space,
adamc@123 413 string "rec",
adamc@123 414 space,
adamc@123 415 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 416
adamc@30 417 | DSgn (x, sgn) => box [string "signature",
adamc@30 418 space,
adamc@30 419 string x,
adamc@30 420 space,
adamc@30 421 string "=",
adamc@30 422 space,
adamc@30 423 p_sgn sgn]
adamc@30 424 | DStr (x, NONE, str) => box [string "structure",
adamc@30 425 space,
adamc@30 426 string x,
adamc@30 427 space,
adamc@30 428 string "=",
adamc@30 429 space,
adamc@30 430 p_str str]
adamc@30 431 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 432 space,
adamc@30 433 string x,
adamc@30 434 space,
adamc@30 435 string ":",
adamc@30 436 space,
adamc@30 437 p_sgn sgn,
adamc@30 438 space,
adamc@30 439 string "=",
adamc@30 440 space,
adamc@30 441 p_str str]
adamc@48 442 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 443 space,
adamc@48 444 string "structure",
adamc@48 445 space,
adamc@48 446 string x,
adamc@48 447 space,
adamc@48 448 string ":",
adamc@48 449 space,
adamc@48 450 p_sgn sgn]
adamc@61 451 | DOpen (m, ms) => box [string "open",
adamc@61 452 space,
adamc@61 453 p_list_sep (string ".") string (m :: ms)]
adamc@88 454 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 455 space,
adamc@88 456 p_con c1,
adamc@88 457 space,
adamc@88 458 string "~",
adamc@88 459 space,
adamc@88 460 p_con c2]
adamc@88 461 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 462 space,
adamc@88 463 string "constraints",
adamc@88 464 space,
adamc@88 465 p_list_sep (string ".") string (m :: ms)]
adamc@30 466
adamc@109 467 | DExport str => box [string "export",
adamc@109 468 space,
adamc@109 469 p_str str]
adamc@100 470
adamc@30 471 and p_str (str, _) =
adamc@30 472 case str of
adamc@30 473 StrConst ds => box [string "struct",
adamc@30 474 newline,
adamc@30 475 p_list_sep newline p_decl ds,
adamc@30 476 newline,
adamc@30 477 string "end"]
adamc@30 478 | StrVar x => string x
adamc@34 479 | StrProj (str, x) => box [p_str str,
adamc@34 480 string ".",
adamc@34 481 string x]
adamc@40 482 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 483 space,
adamc@40 484 string "(",
adamc@40 485 string x,
adamc@40 486 space,
adamc@40 487 string ":",
adamc@40 488 p_sgn sgn,
adamc@40 489 string ")",
adamc@40 490 space,
adamc@40 491 string "=>",
adamc@40 492 space,
adamc@40 493 p_str str]
adamc@40 494 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 495 space,
adamc@40 496 string "(",
adamc@40 497 string x,
adamc@40 498 space,
adamc@40 499 string ":",
adamc@40 500 p_sgn sgn,
adamc@40 501 string ")",
adamc@40 502 space,
adamc@40 503 string ":",
adamc@40 504 space,
adamc@40 505 p_sgn sgn',
adamc@40 506 space,
adamc@40 507 string "=>",
adamc@40 508 space,
adamc@40 509 p_str str]
adamc@44 510 | StrApp (str1, str2) => box [p_str str1,
adamc@44 511 string "(",
adamc@44 512 p_str str2,
adamc@44 513 string ")"]
adamc@30 514
adamc@1 515 val p_file = p_list_sep newline p_decl
adamc@1 516
adamc@1 517 end