annotate src/source_print.sml @ 77:a1026ae076ea

Another test of broad unification
author Adam Chlipala <adamc@hcoop.net>
date Sun, 29 Jun 2008 10:44:36 -0400
parents 6431b315a1e3
children b4f2a258e52c
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@18 47 | KWild => string "_"
adamc@1 48
adamc@1 49 and p_kind k = p_kind' false k
adamc@1 50
adamc@1 51 fun p_explicitness e =
adamc@1 52 case e of
adamc@1 53 Explicit => string "::"
adamc@1 54 | Implicit => string ":::"
adamc@1 55
adamc@1 56 fun p_con' par (c, _) =
adamc@1 57 case c of
adamc@1 58 CAnnot (c, k) => box [string "(",
adamc@1 59 p_con c,
adamc@1 60 space,
adamc@1 61 string "::",
adamc@1 62 space,
adamc@1 63 p_kind k,
adamc@1 64 string ")"]
adamc@1 65
adamc@1 66 | TFun (t1, t2) => parenIf par (box [p_con' true t1,
adamc@1 67 space,
adamc@1 68 string "->",
adamc@1 69 space,
adamc@1 70 p_con t2])
adamc@1 71 | TCFun (e, x, k, c) => parenIf par (box [string x,
adamc@1 72 space,
adamc@1 73 p_explicitness e,
adamc@1 74 space,
adamc@1 75 p_kind k,
adamc@1 76 space,
adamc@1 77 string "->",
adamc@1 78 space,
adamc@1 79 p_con c])
adamc@1 80 | TRecord (CRecord xcs, _) => box [string "{",
adamc@1 81 p_list (fn (x, c) =>
adamc@20 82 box [p_name x,
adamc@1 83 space,
adamc@1 84 string ":",
adamc@1 85 space,
adamc@1 86 p_con c]) xcs,
adamc@1 87 string "}"]
adamc@1 88 | TRecord c => box [string "$",
adamc@1 89 p_con' true c]
adamc@1 90
adamc@34 91 | CVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@1 92 | CApp (c1, c2) => parenIf par (box [p_con c1,
adamc@1 93 space,
adamc@1 94 p_con' true c2])
adamc@67 95 | CAbs (x, NONE, c) => parenIf par (box [string "fn",
adamc@67 96 space,
adamc@67 97 string x,
adamc@67 98 space,
adamc@67 99 string "=>",
adamc@67 100 space,
adamc@67 101 p_con c])
adamc@67 102 | CAbs (x, SOME k, c) => parenIf par (box [string "fn",
adamc@67 103 space,
adamc@67 104 string x,
adamc@67 105 space,
adamc@67 106 string "::",
adamc@67 107 space,
adamc@67 108 p_kind k,
adamc@67 109 space,
adamc@67 110 string "=>",
adamc@67 111 space,
adamc@67 112 p_con c])
adamc@1 113
adamc@1 114 | CName s => box [string "#", string s]
adamc@1 115
adamc@1 116 | CRecord xcs => box [string "[",
adamc@1 117 p_list (fn (x, c) =>
adamc@1 118 box [p_con x,
adamc@1 119 space,
adamc@1 120 string "=",
adamc@1 121 space,
adamc@1 122 p_con c]) xcs,
adamc@1 123 string "]"]
adamc@1 124 | CConcat (c1, c2) => parenIf par (box [p_con' true c1,
adamc@1 125 space,
adamc@1 126 string "++",
adamc@1 127 space,
adamc@1 128 p_con c2])
adamc@67 129 | CFold => string "fold"
adamc@18 130 | CWild k => box [string "(_",
adamc@18 131 space,
adamc@18 132 string "::",
adamc@18 133 space,
adamc@18 134 p_kind k]
adamc@1 135
adamc@1 136 and p_con c = p_con' false c
adamc@1 137
adamc@20 138 and p_name (all as (c, _)) =
adamc@20 139 case c of
adamc@20 140 CName s => string s
adamc@20 141 | _ => p_con all
adamc@20 142
adamc@8 143 fun p_exp' par (e, _) =
adamc@8 144 case e of
adamc@8 145 EAnnot (e, t) => box [string "(",
adamc@8 146 p_exp e,
adamc@8 147 space,
adamc@8 148 string ":",
adamc@8 149 space,
adamc@8 150 p_con t,
adamc@8 151 string ")"]
adamc@8 152
adamc@14 153 | EPrim p => Prim.p_t p
adamc@34 154 | EVar (ss, s) => p_list_sep (string ".") string (ss @ [s])
adamc@8 155 | EApp (e1, e2) => parenIf par (box [p_exp e1,
adamc@8 156 space,
adamc@8 157 p_exp' true e2])
adamc@8 158 | EAbs (x, NONE, e) => parenIf par (box [string "fn",
adamc@8 159 space,
adamc@8 160 string x,
adamc@8 161 space,
adamc@8 162 string "=>",
adamc@8 163 space,
adamc@8 164 p_exp e])
adamc@8 165 | EAbs (x, SOME t, e) => parenIf par (box [string "fn",
adamc@8 166 space,
adamc@8 167 string x,
adamc@8 168 space,
adamc@8 169 string ":",
adamc@8 170 space,
adamc@8 171 p_con t,
adamc@8 172 space,
adamc@8 173 string "=>",
adamc@8 174 space,
adamc@8 175 p_exp e])
adamc@8 176 | ECApp (e, c) => parenIf par (box [p_exp e,
adamc@8 177 space,
adamc@8 178 string "[",
adamc@8 179 p_con c,
adamc@8 180 string "]"])
adamc@8 181 | ECAbs (exp, x, k, e) => parenIf par (box [string "fn",
adamc@8 182 space,
adamc@8 183 string x,
adamc@8 184 space,
adamc@8 185 p_explicitness exp,
adamc@8 186 space,
adamc@8 187 p_kind k,
adamc@8 188 space,
adamc@8 189 string "=>",
adamc@8 190 space,
adamc@8 191 p_exp e])
adamc@8 192
adamc@12 193 | ERecord xes => box [string "{",
adamc@12 194 p_list (fn (x, e) =>
adamc@21 195 box [p_name x,
adamc@12 196 space,
adamc@12 197 string "=",
adamc@12 198 space,
adamc@12 199 p_exp e]) xes,
adamc@12 200 string "}"]
adamc@12 201 | EField (e, c) => box [p_exp' true e,
adamc@12 202 string ".",
adamc@12 203 p_con' true c]
adamc@71 204 | EFold => string "fold"
adamc@12 205
adamc@8 206 and p_exp e = p_exp' false e
adamc@8 207
adamc@30 208 fun p_sgn_item (sgi, _) =
adamc@30 209 case sgi of
adamc@30 210 SgiConAbs (x, k) => box [string "con",
adamc@30 211 space,
adamc@30 212 string x,
adamc@30 213 space,
adamc@30 214 string "::",
adamc@30 215 space,
adamc@30 216 p_kind k]
adamc@30 217 | SgiCon (x, NONE, c) => box [string "con",
adamc@30 218 space,
adamc@30 219 string x,
adamc@30 220 space,
adamc@30 221 string "=",
adamc@30 222 space,
adamc@30 223 p_con c]
adamc@30 224 | SgiCon (x, SOME k, c) => box [string "con",
adamc@30 225 space,
adamc@30 226 string x,
adamc@30 227 space,
adamc@30 228 string "::",
adamc@30 229 space,
adamc@30 230 p_kind k,
adamc@30 231 space,
adamc@30 232 string "=",
adamc@30 233 space,
adamc@30 234 p_con c]
adamc@30 235 | SgiVal (x, c) => box [string "val",
adamc@30 236 space,
adamc@30 237 string x,
adamc@30 238 space,
adamc@30 239 string ":",
adamc@30 240 space,
adamc@30 241 p_con c]
adamc@30 242 | SgiStr (x, sgn) => box [string "structure",
adamc@30 243 space,
adamc@30 244 string x,
adamc@30 245 space,
adamc@30 246 string ":",
adamc@30 247 space,
adamc@30 248 p_sgn sgn]
adamc@59 249 | SgiSgn (x, sgn) => box [string "signature",
adamc@59 250 space,
adamc@59 251 string x,
adamc@59 252 space,
adamc@59 253 string "=",
adamc@59 254 space,
adamc@59 255 p_sgn sgn]
adamc@58 256 | SgiInclude sgn => box [string "include",
adamc@58 257 space,
adamc@58 258 p_sgn sgn]
adamc@30 259
adamc@30 260 and p_sgn (sgn, _) =
adamc@30 261 case sgn of
adamc@30 262 SgnConst sgis => box [string "sig",
adamc@30 263 newline,
adamc@30 264 p_list_sep newline p_sgn_item sgis,
adamc@30 265 newline,
adamc@30 266 string "end"]
adamc@30 267 | SgnVar x => string x
adamc@40 268 | SgnFun (x, sgn, sgn') => box [string "functor",
adamc@40 269 space,
adamc@40 270 string "(",
adamc@40 271 string x,
adamc@40 272 space,
adamc@40 273 string ":",
adamc@40 274 p_sgn sgn,
adamc@40 275 string ")",
adamc@40 276 space,
adamc@40 277 string ":",
adamc@40 278 space,
adamc@40 279 p_sgn sgn']
adamc@42 280 | SgnWhere (sgn, x, c) => box [p_sgn sgn,
adamc@42 281 space,
adamc@42 282 string "where",
adamc@42 283 space,
adamc@42 284 string "con",
adamc@42 285 space,
adamc@42 286 string x,
adamc@42 287 space,
adamc@42 288 string "=",
adamc@42 289 space,
adamc@42 290 p_con c]
adamc@59 291 | SgnProj (m, ms, x) => p_list_sep (string ".") string (m :: ms @ [x])
adamc@59 292
adamc@42 293
adamc@1 294 fun p_decl ((d, _) : decl) =
adamc@1 295 case d of
adamc@1 296 DCon (x, NONE, c) => box [string "con",
adamc@1 297 space,
adamc@1 298 string x,
adamc@1 299 space,
adamc@1 300 string "=",
adamc@1 301 space,
adamc@1 302 p_con c]
adamc@1 303 | DCon (x, SOME k, c) => box [string "con",
adamc@1 304 space,
adamc@1 305 string x,
adamc@1 306 space,
adamc@1 307 string "::",
adamc@1 308 space,
adamc@1 309 p_kind k,
adamc@1 310 space,
adamc@1 311 string "=",
adamc@1 312 space,
adamc@1 313 p_con c]
adamc@8 314 | DVal (x, NONE, e) => box [string "val",
adamc@8 315 space,
adamc@8 316 string x,
adamc@8 317 space,
adamc@8 318 string "=",
adamc@8 319 space,
adamc@8 320 p_exp e]
adamc@8 321 | DVal (x, SOME t, e) => box [string "val",
adamc@8 322 space,
adamc@8 323 string x,
adamc@8 324 space,
adamc@8 325 string ":",
adamc@8 326 space,
adamc@8 327 p_con t,
adamc@8 328 space,
adamc@8 329 string "=",
adamc@8 330 space,
adamc@8 331 p_exp e]
adamc@1 332
adamc@30 333 | DSgn (x, sgn) => box [string "signature",
adamc@30 334 space,
adamc@30 335 string x,
adamc@30 336 space,
adamc@30 337 string "=",
adamc@30 338 space,
adamc@30 339 p_sgn sgn]
adamc@30 340 | DStr (x, NONE, str) => box [string "structure",
adamc@30 341 space,
adamc@30 342 string x,
adamc@30 343 space,
adamc@30 344 string "=",
adamc@30 345 space,
adamc@30 346 p_str str]
adamc@30 347 | DStr (x, SOME sgn, str) => box [string "structure",
adamc@30 348 space,
adamc@30 349 string x,
adamc@30 350 space,
adamc@30 351 string ":",
adamc@30 352 space,
adamc@30 353 p_sgn sgn,
adamc@30 354 space,
adamc@30 355 string "=",
adamc@30 356 space,
adamc@30 357 p_str str]
adamc@48 358 | DFfiStr (x, sgn) => box [string "extern",
adamc@48 359 space,
adamc@48 360 string "structure",
adamc@48 361 space,
adamc@48 362 string x,
adamc@48 363 space,
adamc@48 364 string ":",
adamc@48 365 space,
adamc@48 366 p_sgn sgn]
adamc@61 367 | DOpen (m, ms) => box [string "open",
adamc@61 368 space,
adamc@61 369 p_list_sep (string ".") string (m :: ms)]
adamc@30 370
adamc@30 371 and p_str (str, _) =
adamc@30 372 case str of
adamc@30 373 StrConst ds => box [string "struct",
adamc@30 374 newline,
adamc@30 375 p_list_sep newline p_decl ds,
adamc@30 376 newline,
adamc@30 377 string "end"]
adamc@30 378 | StrVar x => string x
adamc@34 379 | StrProj (str, x) => box [p_str str,
adamc@34 380 string ".",
adamc@34 381 string x]
adamc@40 382 | StrFun (x, sgn, NONE, str) => box [string "functor",
adamc@40 383 space,
adamc@40 384 string "(",
adamc@40 385 string x,
adamc@40 386 space,
adamc@40 387 string ":",
adamc@40 388 p_sgn sgn,
adamc@40 389 string ")",
adamc@40 390 space,
adamc@40 391 string "=>",
adamc@40 392 space,
adamc@40 393 p_str str]
adamc@40 394 | StrFun (x, sgn, SOME sgn', str) => box [string "functor",
adamc@40 395 space,
adamc@40 396 string "(",
adamc@40 397 string x,
adamc@40 398 space,
adamc@40 399 string ":",
adamc@40 400 p_sgn sgn,
adamc@40 401 string ")",
adamc@40 402 space,
adamc@40 403 string ":",
adamc@40 404 space,
adamc@40 405 p_sgn sgn',
adamc@40 406 space,
adamc@40 407 string "=>",
adamc@40 408 space,
adamc@40 409 p_str str]
adamc@44 410 | StrApp (str1, str2) => box [p_str str1,
adamc@44 411 string "(",
adamc@44 412 p_str str2,
adamc@44 413 string ")"]
adamc@30 414
adamc@1 415 val p_file = p_list_sep newline p_decl
adamc@1 416
adamc@1 417 end