annotate src/source_print.sml @ 171:c7a6e6dbc318

Elaborating some basic pattern matching
author Adam Chlipala <adamc@hcoop.net>
date Thu, 31 Jul 2008 10:06:27 -0400
parents a158f8c5aa55
children 8221b95cc24c
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@170 165 fun p_pat' par (p, _) =
adamc@170 166 case p of
adamc@170 167 PWild => string "_"
adamc@170 168 | PVar s => string s
adamc@170 169 | PCon (ms, x, NONE) => p_list_sep (string ".") string (ms @ [x])
adamc@170 170 | PCon (ms, x, SOME p) => parenIf par (box [p_list_sep (string ".") string (ms @ [x]),
adamc@170 171 space,
adamc@170 172 p_pat' true p])
adamc@170 173
adamc@170 174 val p_pat = p_pat' false
adamc@170 175
adamc@8 176 fun p_exp' par (e, _) =
adamc@8 177 case e of
adamc@8 178 EAnnot (e, t) => box [string "(",
adamc@8 179 p_exp e,
adamc@8 180 space,
adamc@8 181 string ":",
adamc@8 182 space,
adamc@8 183 p_con t,
adamc@8 184 string ")"]
adamc@8 185
adamc@14 186 | EPrim p => Prim.p_t p
adamc@34 187 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 188 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 189 space,
adamc@8 190 p_exp' true e2])
adamc@8 191 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 192 space,
adamc@8 193 string x,
adamc@8 194 space,
adamc@8 195 string "=>",
adamc@8 196 space,
adamc@8 197 p_exp e])
adamc@8 198 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 199 space,
adamc@8 200 string x,
adamc@8 201 space,
adamc@8 202 string ":",
adamc@8 203 space,
adamc@8 204 p_con t,
adamc@8 205 space,
adamc@8 206 string "=>",
adamc@8 207 space,
adamc@8 208 p_exp e])
adamc@8 209 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 210 space,
adamc@8 211 string "[",
adamc@8 212 p_con c,
adamc@8 213 string "]"])
adamc@8 214 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 215 space,
adamc@8 216 string x,
adamc@8 217 space,
adamc@8 218 p_explicitness exp,
adamc@8 219 space,
adamc@8 220 p_kind k,
adamc@8 221 space,
adamc@8 222 string "=>",
adamc@8 223 space,
adamc@8 224 p_exp e])
adamc@85 225 | EDisjoint (c1, c2, e) => parenIf par (box [p_con c1,
adamc@85 226 space,
adamc@85 227 string "~",
adamc@85 228 space,
adamc@85 229 p_con c2,
adamc@85 230 space,
adamc@85 231 string "=>",
adamc@85 232 space,
adamc@85 233 p_exp e])
adamc@8 234
adamc@12 235 | ERecord xes => box [string "{",
adamc@12 236 p_list (fn (x, e) =>
adamc@21 237 box [p_name x,
adamc@12 238 space,
adamc@12 239 string "=",
adamc@12 240 space,
adamc@12 241 p_exp e]) xes,
adamc@12 242 string "}"]
adamc@12 243 | EField (e, c) => box [p_exp' true e,
adamc@12 244 string ".",
adamc@12 245 p_con' true c]
adamc@149 246 | ECut (e, c) => parenIf par (box [p_exp' true e,
adamc@149 247 space,
adamc@149 248 string "--",
adamc@149 249 space,
adamc@149 250 p_con' true c])
adamc@71 251 | EFold => string "fold"
adamc@12 252
adamc@170 253 | ECase (e, pes) => parenIf par (box [string "case",
adamc@170 254 space,
adamc@171 255 p_exp e,
adamc@170 256 space,
adamc@170 257 string "of",
adamc@170 258 space,
adamc@170 259 p_list_sep (box [space, string "|", space])
adamc@170 260 (fn (p, e) => box [p_pat p,
adamc@170 261 space,
adamc@170 262 string "=>",
adamc@170 263 space,
adamc@170 264 p_exp e]) pes])
adamc@170 265
adamc@8 266 and p_exp e = p_exp' false e
adamc@8 267
adamc@156 268 fun p_datatype (x, cons) =
adamc@156 269 box [string "datatype",
adamc@156 270 space,
adamc@156 271 string x,
adamc@156 272 space,
adamc@156 273 string "=",
adamc@156 274 space,
adamc@156 275 p_list_sep (box [space, string "|", space])
adamc@156 276 (fn (x, NONE) => string x
adamc@156 277 | (x, SOME t) => box [string x, space, string "of", space, p_con t])
adamc@156 278 cons]
adamc@156 279
adamc@30 280 fun p_sgn_item (sgi, _) =
adamc@30 281 case sgi of
adamc@30 282 SgiConAbs (x, k) => box [string "con",
adamc@30 283 space,
adamc@30 284 string x,
adamc@30 285 space,
adamc@30 286 string "::",
adamc@30 287 space,
adamc@30 288 p_kind k]
adamc@30 289 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 290 space,
adamc@30 291 string x,
adamc@30 292 space,
adamc@30 293 string "=",
adamc@30 294 space,
adamc@30 295 p_con c]
adamc@30 296 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 297 space,
adamc@30 298 string x,
adamc@30 299 space,
adamc@30 300 string "::",
adamc@30 301 space,
adamc@30 302 p_kind k,
adamc@30 303 space,
adamc@30 304 string "=",
adamc@30 305 space,
adamc@30 306 p_con c]
adamc@156 307 | SgiDatatype x => p_datatype x
adamc@156 308 | SgiDatatypeImp (x, ms, x') =>
adamc@156 309 box [string "datatype",
adamc@156 310 space,
adamc@156 311 string x,
adamc@156 312 space,
adamc@156 313 string "=",
adamc@156 314 space,
adamc@156 315 string "datatype",
adamc@156 316 space,
adamc@156 317 p_list_sep (string ".") string (ms @ [x'])]
adamc@30 318 | SgiVal (x, c) => box [string "val",
adamc@30 319 space,
adamc@30 320 string x,
adamc@30 321 space,
adamc@30 322 string ":",
adamc@30 323 space,
adamc@30 324 p_con c]
adamc@30 325 | SgiStr (x, sgn) => box [string "structure",
adamc@30 326 space,
adamc@30 327 string x,
adamc@30 328 space,
adamc@30 329 string ":",
adamc@30 330 space,
adamc@30 331 p_sgn sgn]
adamc@59 332 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 333 space,
adamc@59 334 string x,
adamc@59 335 space,
adamc@59 336 string "=",
adamc@59 337 space,
adamc@59 338 p_sgn sgn]
adamc@58 339 | SgiInclude sgn => box [string "include",
adamc@58 340 space,
adamc@58 341 p_sgn sgn]
adamc@88 342 | SgiConstraint (c1, c2) => box [string "constraint",
adamc@88 343 space,
adamc@88 344 p_con c1,
adamc@88 345 space,
adamc@88 346 string "~",
adamc@88 347 space,
adamc@88 348 p_con c2]
adamc@30 349
adamc@30 350 and p_sgn (sgn, _) =
adamc@30 351 case sgn of
adamc@30 352 SgnConst sgis => box [string "sig",
adamc@30 353 newline,
adamc@30 354 p_list_sep newline p_sgn_item sgis,
adamc@30 355 newline,
adamc@30 356 string "end"]
adamc@30 357 | SgnVar x => string x
adamc@40 358 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 359 space,
adamc@40 360 string "(",
adamc@40 361 string x,
adamc@40 362 space,
adamc@40 363 string ":",
adamc@40 364 p_sgn sgn,
adamc@40 365 string ")",
adamc@40 366 space,
adamc@40 367 string ":",
adamc@40 368 space,
adamc@40 369 p_sgn sgn']
adamc@42 370 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 371 space,
adamc@42 372 string "where",
adamc@42 373 space,
adamc@42 374 string "con",
adamc@42 375 space,
adamc@42 376 string x,
adamc@42 377 space,
adamc@42 378 string "=",
adamc@42 379 space,
adamc@42 380 p_con c]
adamc@59 381 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 382
adamc@42 383
adamc@123 384 fun p_vali (x, co, e) =
adamc@123 385 case co of
adamc@123 386 NONE => box [string x,
adamc@123 387 space,
adamc@123 388 string "=",
adamc@123 389 space,
adamc@123 390 p_exp e]
adamc@123 391 | SOME t => box [string x,
adamc@123 392 space,
adamc@123 393 string ":",
adamc@123 394 space,
adamc@123 395 p_con t,
adamc@123 396 space,
adamc@123 397 string "=",
adamc@123 398 space,
adamc@123 399 p_exp e]
adamc@123 400
adamc@1 401 fun p_decl ((d, _) : decl) =
adamc@1 402 case d of
adamc@1 403 DCon (x, NONE, c) => box [string "con",
adamc@1 404 space,
adamc@1 405 string x,
adamc@1 406 space,
adamc@1 407 string "=",
adamc@1 408 space,
adamc@1 409 p_con c]
adamc@1 410 | DCon (x, SOME k, c) => box [string "con",
adamc@1 411 space,
adamc@1 412 string x,
adamc@1 413 space,
adamc@1 414 string "::",
adamc@1 415 space,
adamc@1 416 p_kind k,
adamc@1 417 space,
adamc@1 418 string "=",
adamc@1 419 space,
adamc@1 420 p_con c]
adamc@156 421 | DDatatype x => p_datatype x
adamc@156 422 | DDatatypeImp (x, ms, x') =>
adamc@156 423 box [string "datatype",
adamc@156 424 space,
adamc@156 425 string x,
adamc@156 426 space,
adamc@156 427 string "=",
adamc@156 428 space,
adamc@156 429 string "datatype",
adamc@156 430 space,
adamc@156 431 p_list_sep (string ".") string (ms @ [x'])]
adamc@123 432 | DVal vi => box [string "val",
adamc@123 433 space,
adamc@123 434 p_vali vi]
adamc@123 435 | DValRec vis => box [string "val",
adamc@123 436 space,
adamc@123 437 string "rec",
adamc@123 438 space,
adamc@123 439 p_list_sep (box [newline, string "and", space]) p_vali vis]
adamc@1 440
adamc@30 441 | DSgn (x, sgn) => box [string "signature",
adamc@30 442 space,
adamc@30 443 string x,
adamc@30 444 space,
adamc@30 445 string "=",
adamc@30 446 space,
adamc@30 447 p_sgn sgn]
adamc@30 448 | DStr (x, NONE, str) => box [string "structure",
adamc@30 449 space,
adamc@30 450 string x,
adamc@30 451 space,
adamc@30 452 string "=",
adamc@30 453 space,
adamc@30 454 p_str str]
adamc@30 455 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 456 space,
adamc@30 457 string x,
adamc@30 458 space,
adamc@30 459 string ":",
adamc@30 460 space,
adamc@30 461 p_sgn sgn,
adamc@30 462 space,
adamc@30 463 string "=",
adamc@30 464 space,
adamc@30 465 p_str str]
adamc@48 466 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 467 space,
adamc@48 468 string "structure",
adamc@48 469 space,
adamc@48 470 string x,
adamc@48 471 space,
adamc@48 472 string ":",
adamc@48 473 space,
adamc@48 474 p_sgn sgn]
adamc@61 475 | DOpen (m, ms) => box [string "open",
adamc@61 476 space,
adamc@61 477 p_list_sep (string ".") string (m :: ms)]
adamc@88 478 | DConstraint (c1, c2) => box [string "constraint",
adamc@88 479 space,
adamc@88 480 p_con c1,
adamc@88 481 space,
adamc@88 482 string "~",
adamc@88 483 space,
adamc@88 484 p_con c2]
adamc@88 485 | DOpenConstraints (m, ms) => box [string "open",
adamc@88 486 space,
adamc@88 487 string "constraints",
adamc@88 488 space,
adamc@88 489 p_list_sep (string ".") string (m :: ms)]
adamc@30 490
adamc@109 491 | DExport str => box [string "export",
adamc@109 492 space,
adamc@109 493 p_str str]
adamc@100 494
adamc@30 495 and p_str (str, _) =
adamc@30 496 case str of
adamc@30 497 StrConst ds => box [string "struct",
adamc@30 498 newline,
adamc@30 499 p_list_sep newline p_decl ds,
adamc@30 500 newline,
adamc@30 501 string "end"]
adamc@30 502 | StrVar x => string x
adamc@34 503 | StrProj (str, x) => box [p_str str,
adamc@34 504 string ".",
adamc@34 505 string x]
adamc@40 506 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 507 space,
adamc@40 508 string "(",
adamc@40 509 string x,
adamc@40 510 space,
adamc@40 511 string ":",
adamc@40 512 p_sgn sgn,
adamc@40 513 string ")",
adamc@40 514 space,
adamc@40 515 string "=>",
adamc@40 516 space,
adamc@40 517 p_str str]
adamc@40 518 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 519 space,
adamc@40 520 string "(",
adamc@40 521 string x,
adamc@40 522 space,
adamc@40 523 string ":",
adamc@40 524 p_sgn sgn,
adamc@40 525 string ")",
adamc@40 526 space,
adamc@40 527 string ":",
adamc@40 528 space,
adamc@40 529 p_sgn sgn',
adamc@40 530 space,
adamc@40 531 string "=>",
adamc@40 532 space,
adamc@40 533 p_str str]
adamc@44 534 | StrApp (str1, str2) => box [p_str str1,
adamc@44 535 string "(",
adamc@44 536 p_str str2,
adamc@44 537 string ")"]
adamc@30 538
adamc@1 539 val p_file = p_list_sep newline p_decl
adamc@1 540
adamc@1 541 end