| File: | src/abpretty.c |
| Warning: | line 1074, column 11 Although the value stored to 'f' is used in the enclosing expression, the value is never actually read from 'f' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /***************************************************************************** |
| 2 | * |
| 3 | * abpretty.c: Pretty print abstract syntax to produce inputable Aldor code |
| 4 | * |
| 5 | * Copyright (c) 1990-2007 Aldor Software Organization Ltd (Aldor.org). |
| 6 | * |
| 7 | ****************************************************************************/ |
| 8 | |
| 9 | #include "abpretty.h" |
| 10 | #include "axlobs.h" |
| 11 | #include "comsg.h" |
| 12 | #include "doc.h" |
| 13 | #include "lib.h" |
| 14 | #include "spesym.h" |
| 15 | #include "strops.h" |
| 16 | #include "syme.h" |
| 17 | #include "tform.h" |
| 18 | |
| 19 | #define CONTINUED"..." "..." |
| 20 | #define NCONTINUED(sizeof("...") - 1) (sizeof("...") - 1) |
| 21 | #define MAX_NEWLINES2 2 |
| 22 | #define LINE_BLOCK_SIZE4 4 |
| 23 | #define MAX_PREC255 255 |
| 24 | #define INDENTATION" " " " |
| 25 | #define HALFINDENTATION" " " " |
| 26 | |
| 27 | |
| 28 | localstatic Bool abPPClipped0(Buffer, AbSyn, long *pmaxchars); |
| 29 | localstatic Bool abPPPutc(Buffer, int, long *pmaxchars); |
| 30 | localstatic Bool abPPPuts(Buffer, CString, long *pmaxchars); |
| 31 | |
| 32 | localstatic Bool abPPSingleNewLine(Buffer buf, long *pmaxchars); |
| 33 | localstatic Bool abPPIndent(Buffer buf, long *pmaxchars); |
| 34 | localstatic Bool abPPParens(int prec, Buffer buf, AbSyn ab, long *pmaxchars); |
| 35 | |
| 36 | localstatic Bool abPPApply(Buffer, AbSyn, long *pmaxchars); |
| 37 | localstatic Bool abPPIterators(Buffer, int itc, AbSyn *itv, long *pmaxchars); |
| 38 | localstatic Bool abPPLambda(Buffer, AbSyn, long *pmaxchars, Bool packed, AbSyn); |
| 39 | localstatic Bool abPPSequence(Buffer, AbSyn, long *pmaxchars); |
| 40 | |
| 41 | localstatic Bool abPPIsElidedLambda (AbSyn); |
| 42 | localstatic Bool abPPElidedLambda (Buffer, AbSyn, long *); |
| 43 | localstatic Bool abPPIsDefine (AbSyn); |
| 44 | |
| 45 | localstatic Bool symeClipped0(Buffer, Syme, long *pmaxchars); |
| 46 | |
| 47 | localstatic int getPrecedence(AbSyn ab); |
| 48 | localstatic Bool isLeftAssociative(AbSyn ab); |
| 49 | |
| 50 | static int sequenceLevel; |
| 51 | static Bool usePiles; |
| 52 | |
| 53 | |
| 54 | /***************************************************************************** |
| 55 | * |
| 56 | * :: External entry points |
| 57 | * |
| 58 | ****************************************************************************/ |
| 59 | |
| 60 | /* |
| 61 | * Abstract syntax functions. |
| 62 | */ |
| 63 | String |
| 64 | abPretty(AbSyn ab) |
| 65 | { |
| 66 | return abPrettyClippedIn(ab, ABPP_UNCLIPPED(200000L), ABPP_NOINDENT(-1)); |
| 67 | } |
| 68 | |
| 69 | String |
| 70 | abPrettyClippedIn(AbSyn ab, long maxchars, int ind) |
| 71 | { |
| 72 | /* ind = number of tabs stops of indentation */ |
| 73 | |
| 74 | Buffer buf; |
| 75 | Bool hasContinuedRoom = (maxchars >= NCONTINUED(sizeof("...") - 1)); |
| 76 | |
| 77 | sequenceLevel = ind; |
| 78 | usePiles = true1; |
| 79 | |
| 80 | buf = bufNew(); |
| 81 | |
| 82 | if (hasContinuedRoom) |
| 83 | maxchars -= NCONTINUED(sizeof("...") - 1); |
| 84 | |
| 85 | if (!abPPClipped0(buf, ab, &maxchars) && hasContinuedRoom) |
| 86 | bufPuts(buf, CONTINUED"..."); |
| 87 | |
| 88 | return bufLiberate(buf); |
| 89 | } |
| 90 | |
| 91 | int |
| 92 | abPrettyPrint(FILE * fout, AbSyn ab) |
| 93 | { |
| 94 | return abPrettyPrintClippedIn(fout, ab, ABPP_UNCLIPPED(200000L), ABPP_NOINDENT(-1)); |
| 95 | } |
| 96 | |
| 97 | int |
| 98 | abPrettyPrintClippedIn(FILE * fout, AbSyn ab, long maxchars, int ind) |
| 99 | { |
| 100 | String s = abPrettyClippedIn(ab, maxchars, ind); |
| 101 | int cc = fprintf(fout, "%s", s); |
| 102 | strFree(s); |
| 103 | return cc; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Type form functions. |
| 108 | */ |
| 109 | |
| 110 | String |
| 111 | tfPretty(TForm tf) |
| 112 | { |
| 113 | return tfPrettyClippedIn(tf, ABPP_UNCLIPPED(200000L), ABPP_NOINDENT(-1)); |
| 114 | } |
| 115 | |
| 116 | String |
| 117 | tfPrettyClippedIn(TForm tf, long maxchars, int ind) |
| 118 | { |
| 119 | String s; |
| 120 | assert(tf)do { if (!(tf)) _do_assert(("tf"),"abpretty.c",120); } while ( 0); |
| 121 | |
| 122 | if (tfIsUnknown(tf)(((tf)->tag) == TF_Unknown)) |
| 123 | s = strCopy("-- Unknown type --"); |
| 124 | else if (tfIsNone(tf)((((tf)->tag) == TF_Multiple) && tfMultiArgc(tf) == 0)) |
| 125 | s = strCopy("(0 types)"); |
| 126 | else |
| 127 | s = abPrettyClippedIn(tfToAbSynPretty(tf), maxchars, ind); |
| 128 | |
| 129 | return s; |
| 130 | } |
| 131 | |
| 132 | int |
| 133 | tfPrettyPrint(FILE *fout, TForm tf) |
| 134 | { |
| 135 | return tfPrettyPrintClippedIn(fout, tf, ABPP_UNCLIPPED(200000L), ABPP_NOINDENT(-1)); |
| 136 | } |
| 137 | |
| 138 | int |
| 139 | tfPrettyPrintClippedIn(FILE *fout, TForm tf, long maxchars, int ind) |
| 140 | { |
| 141 | String s = tfPrettyClippedIn(tf, maxchars, ind); |
| 142 | int cc = fprintf(fout, "%s", s); |
| 143 | strFree(s); |
| 144 | return cc; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * Symbol expression functions. |
| 149 | */ |
| 150 | |
| 151 | String |
| 152 | symePretty(Syme syme) |
| 153 | { |
| 154 | assert(syme)do { if (!(syme)) _do_assert(("syme"),"abpretty.c",154); } while (0); |
| 155 | return symePrettyClippedIn(syme, ABPP_UNCLIPPED(200000L), ABPP_NOINDENT(-1)); |
| 156 | } |
| 157 | |
| 158 | String |
| 159 | symePrettyClippedIn(Syme syme, long maxchars, int ind) |
| 160 | { |
| 161 | /* ind = number of tabs stops of indentation */ |
| 162 | |
| 163 | Buffer buf; |
| 164 | Bool hasContinuedRoom = (maxchars >= NCONTINUED(sizeof("...") - 1)); |
| 165 | |
| 166 | assert(syme)do { if (!(syme)) _do_assert(("syme"),"abpretty.c",166); } while (0); |
| 167 | sequenceLevel = ind; |
| 168 | usePiles = true1; |
| 169 | |
| 170 | buf = bufNew(); |
| 171 | |
| 172 | if (hasContinuedRoom) |
| 173 | maxchars -= NCONTINUED(sizeof("...") - 1); |
| 174 | |
| 175 | if (!symeClipped0(buf, syme, &maxchars) && hasContinuedRoom) |
| 176 | bufPuts(buf, CONTINUED"..."); |
| 177 | |
| 178 | return bufLiberate(buf); |
| 179 | } |
| 180 | |
| 181 | /***************************************************************************** |
| 182 | * |
| 183 | * :: The Guts |
| 184 | * |
| 185 | ****************************************************************************/ |
| 186 | |
| 187 | /* |
| 188 | * Return whether the expression fit. |
| 189 | */ |
| 190 | |
| 191 | localstatic Bool |
| 192 | abPPClipped0(Buffer buf, AbSyn ab, long *pmaxchars) |
| 193 | { |
| 194 | Bool f; /* Fits? */ |
| 195 | int i; |
| 196 | AbSynTag tag; |
| 197 | String str; |
| 198 | |
| 199 | assert(ab != 0)do { if (!(ab != 0)) _do_assert(("ab != 0"),"abpretty.c",199) ; } while (0); |
| 200 | |
| 201 | f = *pmaxchars > 0; |
| 202 | |
| 203 | if (! f) |
| 204 | return f; |
| 205 | |
| 206 | tag = abTag(ab)((ab)->abHdr.tag); |
| 207 | |
| 208 | switch (tag) { |
| 209 | case AB_Nothing: |
| 210 | f = f && abPPPuts(buf, "()", pmaxchars); |
| 211 | break; |
| 212 | case AB_Label: |
| 213 | f = f && abPPClipped0(buf, ab->abLabel.expr , pmaxchars); |
| 214 | break; |
| 215 | case AB_Paren: |
| 216 | f = f && abPPClipped0(buf, ab->abParen.expr , pmaxchars); |
| 217 | break; |
| 218 | |
| 219 | case AB_Blank: |
| 220 | case AB_IdSy: |
| 221 | case AB_Id: |
| 222 | f = f && abPPPuts(buf, symString(abLeafSym(ab))((((ab)->abGen.data.sym))->str), pmaxchars); |
| 223 | break; |
| 224 | |
| 225 | case AB_LitInteger: |
| 226 | case AB_LitFloat: |
| 227 | f = f && abPPPuts(buf, abLeafStr(ab)((ab)->abGen.data.str), pmaxchars); |
| 228 | break; |
| 229 | |
| 230 | case AB_LitString: |
| 231 | { |
| 232 | f = f && abPPPutc(buf, '"', pmaxchars) |
| 233 | && abPPPuts(buf, ab->abLitString.str, pmaxchars) |
| 234 | && abPPPutc(buf, '"', pmaxchars); |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | case AB_Apply: |
| 239 | f = f && abPPApply(buf, ab, pmaxchars); |
| 240 | break; |
| 241 | |
| 242 | case AB_Lambda: |
| 243 | case AB_PLambda: |
| 244 | f = f && abPPLambda(buf, ab, pmaxchars, |
| 245 | tag == AB_PLambda, NULL((void*)0)); |
| 246 | break; |
| 247 | |
| 248 | case AB_Sequence: |
| 249 | f = f && abPPSequence(buf, ab, pmaxchars); |
| 250 | break; |
| 251 | |
| 252 | case AB_Documented: |
| 253 | f = f && abPPClipped0(buf, ab->abDocumented.doc , pmaxchars) |
| 254 | && abPPClipped0(buf, ab->abDocumented.expr, pmaxchars); |
| 255 | break; |
| 256 | |
| 257 | case AB_DocText: |
| 258 | { |
| 259 | char *d = docString(ab->abDocText.doc)((ab->abDocText.doc)->corpus); |
| 260 | |
| 261 | f = f && abPPIndent(buf, pmaxchars) |
| 262 | && abPPPuts(buf, "++", pmaxchars); |
| 263 | |
| 264 | while (d && *d) { |
| 265 | if (*d == '\n') { |
| 266 | if (*(++d)) |
| 267 | f = f && abPPIndent(buf, pmaxchars) |
| 268 | && abPPPuts(buf, "++", pmaxchars); |
| 269 | } |
| 270 | else { |
| 271 | f = f && abPPPutc(buf, *d, pmaxchars); |
| 272 | ++d; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | f = f && abPPIndent(buf, pmaxchars); |
| 277 | } |
| 278 | break; |
| 279 | |
| 280 | case AB_Collect: |
| 281 | f = f && abPPClipped0(buf, ab->abCollect.body, pmaxchars) |
| 282 | && abPPPutc(buf, ' ', pmaxchars) |
| 283 | && abPPIterators(buf, abCollectIterc(ab)(((ab)->abHdr.argc)-1), ab->abCollect.iterv, |
| 284 | pmaxchars); |
| 285 | break; |
| 286 | |
| 287 | case AB_Repeat: |
| 288 | { |
| 289 | Bool ind; |
| 290 | String repeat; |
| 291 | int itc = abRepeatIterc(ab)(((ab)->abHdr.argc)-1); |
| 292 | |
| 293 | repeat = (itc > 0) ? " repeat" : "repeat"; |
| 294 | |
| 295 | f = f && abPPIterators(buf, itc, ab->abRepeat.iterv, pmaxchars) |
| 296 | && abPPPuts(buf, repeat, pmaxchars); |
| 297 | |
| 298 | ind = (abTag(ab->abRepeat.body)((ab->abRepeat.body)->abHdr.tag) != AB_Sequence); |
| 299 | |
| 300 | if (ind) { |
| 301 | sequenceLevel++; |
| 302 | f = f && abPPIndent(buf, pmaxchars); |
| 303 | } |
| 304 | |
| 305 | f = f && abPPClipped0(buf, ab->abRepeat.body, pmaxchars); |
| 306 | |
| 307 | if (ind) |
| 308 | sequenceLevel--; |
| 309 | |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | case AB_For: |
| 314 | f = f && abPPPuts(buf, "for ", pmaxchars) |
| 315 | && abPPClipped0(buf, ab->abFor.lhs, pmaxchars) |
| 316 | && abPPPuts(buf, " in ", pmaxchars) |
| 317 | && abPPClipped0(buf, ab->abFor.whole, pmaxchars); |
| 318 | if (!abIsNothing(ab->abFor.test)((ab->abFor.test)->abHdr.tag == (AB_Nothing))) { |
| 319 | Bool saveUsePiles = usePiles; |
| 320 | AbSyn test = ab->abFor.test; |
| 321 | |
| 322 | usePiles = false((int) 0); |
| 323 | f = f && abPPPuts(buf, " | ", pmaxchars) |
| 324 | && abPPClipped0(buf, test, pmaxchars); |
| 325 | usePiles = saveUsePiles; |
| 326 | } |
| 327 | break; |
| 328 | |
| 329 | case AB_Test: |
| 330 | f = f && abPPClipped0(buf, ab->abTest.cond, pmaxchars); |
| 331 | break; |
| 332 | |
| 333 | case AB_While: |
| 334 | { |
| 335 | Bool saveUsePiles = usePiles; |
| 336 | |
| 337 | usePiles = false((int) 0); |
| 338 | f = f && abPPPuts(buf, "while ", pmaxchars) |
| 339 | && abPPClipped0(buf, ab->abWhile.test, pmaxchars); |
| 340 | usePiles = saveUsePiles; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | case AB_If: |
| 345 | f = f && abPPIndent(buf, pmaxchars) |
| 346 | && abPPPuts(buf, "if (", pmaxchars) |
| 347 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars) |
| 348 | && abPPPuts(buf, ") then ", pmaxchars) |
| 349 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars); |
| 350 | if (abIsNotNothing(abArgv(ab)[2])!((((ab)->abGen.data.argv)[2])->abHdr.tag == (AB_Nothing ))) |
| 351 | f = f && abPPIndent(buf, pmaxchars) |
| 352 | && abPPPuts(buf, "else ", pmaxchars) |
| 353 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[2], pmaxchars); |
| 354 | break; |
| 355 | |
| 356 | case AB_Define: |
| 357 | if (abPPIsDefine(ab->abDefine.lhs)) |
| 358 | f = f && abPPClipped0(buf, ab->abDefine.lhs, pmaxchars); |
| 359 | else if (abPPIsElidedLambda(ab)) |
| 360 | f = f && abPPElidedLambda(buf, ab, pmaxchars); |
| 361 | else { |
| 362 | if (! abIsAnyLambda(ab->abDefine.rhs)(((ab->abDefine.rhs)->abHdr.tag == (AB_Lambda)) || ((ab ->abDefine.rhs)->abHdr.tag == (AB_PLambda)))) { |
| 363 | /* |
| 364 | * '==' is right associative. |
| 365 | */ |
| 366 | |
| 367 | AbSyn lhs = ab->abDefine.lhs; |
| 368 | Bool isWith = |
| 369 | abHasTag(lhs, AB_Declare)((lhs)->abHdr.tag == (AB_Declare)) && |
| 370 | abHasTag(lhs->abDeclare.type, AB_With)((lhs->abDeclare.type)->abHdr.tag == (AB_With)); |
| 371 | |
| 372 | if (isWith) |
| 373 | f = f && abPPIndent(buf, pmaxchars); |
| 374 | |
| 375 | if (!isLeftAssociative(lhs)) |
| 376 | f = f && abPPParens(MAX_PREC255, buf, |
| 377 | lhs, pmaxchars); |
| 378 | else |
| 379 | f = f && abPPClipped0(buf, lhs, |
| 380 | pmaxchars); |
| 381 | |
| 382 | if (isWith) |
| 383 | f = f && abPPIndent(buf, pmaxchars) |
| 384 | && abPPPuts(buf, HALFINDENTATION" ", |
| 385 | pmaxchars); |
| 386 | else |
| 387 | f = f && abPPPutc(buf, ' ', pmaxchars); |
| 388 | |
| 389 | f = f && abPPPuts(buf, "== ", pmaxchars); |
| 390 | } |
| 391 | f = f && abPPClipped0(buf, ab->abDefine.rhs, pmaxchars); |
| 392 | } |
| 393 | break; |
| 394 | |
| 395 | case AB_Assign: |
| 396 | case AB_Qualify: |
| 397 | { |
| 398 | /* |
| 399 | * These fellows are right associative. |
| 400 | */ |
| 401 | String op; |
| 402 | |
| 403 | op = (tag == AB_Assign) ? " := " : "$"; |
| 404 | |
| 405 | if (!isLeftAssociative(abArgv(ab)((ab)->abGen.data.argv)[0])) |
| 406 | f = f && abPPParens(MAX_PREC255, buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars); |
| 407 | else |
| 408 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars); |
| 409 | f = f && abPPPuts(buf, op, pmaxchars) |
| 410 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars); |
| 411 | break; |
| 412 | } |
| 413 | |
| 414 | case AB_Declare: |
| 415 | { |
| 416 | if (abIsNotNothing(abArgv(ab)[0])!((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Nothing ))) { |
| 417 | if (abIsTheId(abArgv(ab)[0], ssymBracket)(((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Id)) && ((((ab)->abGen.data.argv)[0])->abId.sym)==(ssymBracket ))) |
| 418 | f = f && abPPPuts(buf, symString(ssymBracket)((ssymBracket)->str), pmaxchars); |
| 419 | else if (abIsTheId(abArgv(ab)[0], ssymBrace)(((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Id)) && ((((ab)->abGen.data.argv)[0])->abId.sym)==(ssymBrace))) |
| 420 | f = f && abPPPuts(buf, symString(ssymBrace)((ssymBrace)->str), pmaxchars); |
| 421 | else |
| 422 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars); |
| 423 | f = f && abPPPuts(buf, ": ", pmaxchars); |
| 424 | } |
| 425 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars); |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | case AB_Comma: |
| 430 | for (i = 0; f && i < ab->abHdr.argc; i++) |
| 431 | if (abIsNotNothing(abArgv(ab)[i])!((((ab)->abGen.data.argv)[i])->abHdr.tag == (AB_Nothing ))) { |
| 432 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[i], pmaxchars); |
| 433 | if (i < ab->abHdr.argc - 1) |
| 434 | f = f && abPPPuts(buf, ", ", pmaxchars); |
| 435 | } |
| 436 | break; |
| 437 | |
| 438 | case AB_Export: |
| 439 | { |
| 440 | Bool seq = abHasTag(abArgv(ab)[0], AB_Sequence)((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Sequence )); |
| 441 | TokenTag tTag = abInfo(tag)abInfoTable[(tag) - AB_START].tokenTag; |
| 442 | |
| 443 | str = keyString(tTag); |
| 444 | |
| 445 | f = f && abPPPuts(buf, str, pmaxchars); |
| 446 | |
| 447 | if (abIsNotNothing(abArgv(ab)[0])!((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Nothing ))) { |
| 448 | if (! seq) |
| 449 | f = f && abPPPutc(buf, ' ', pmaxchars); |
| 450 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars); |
| 451 | } |
| 452 | |
| 453 | if (abIsNotNothing(abArgv(ab)[1])!((((ab)->abGen.data.argv)[1])->abHdr.tag == (AB_Nothing ))) { |
| 454 | if (seq) |
| 455 | f = f && abPPIndent(buf, pmaxchars) |
| 456 | && abPPPuts(buf, HALFINDENTATION" ", pmaxchars); |
| 457 | else |
| 458 | f = f && abPPPutc(buf, ' ', pmaxchars); |
| 459 | f = f && abPPPuts(buf, "to ", pmaxchars) |
| 460 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars); |
| 461 | } |
| 462 | } |
| 463 | break; |
| 464 | |
| 465 | case AB_Extend: |
| 466 | { |
| 467 | Bool seq = abHasTag(abArgv(ab)[0], AB_Sequence)((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Sequence )); |
| 468 | TokenTag tTag = abInfo(tag)abInfoTable[(tag) - AB_START].tokenTag; |
| 469 | |
| 470 | str = keyString(tTag); |
| 471 | |
| 472 | f = f && abPPPuts(buf, str, pmaxchars); |
| 473 | |
| 474 | if (abIsNotNothing(abArgv(ab)[0])!((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Nothing ))) { |
| 475 | if (! seq) |
| 476 | f = f && abPPPutc(buf, ' ', pmaxchars); |
| 477 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars); |
| 478 | } |
| 479 | } |
| 480 | break; |
| 481 | |
| 482 | case AB_Import: |
| 483 | case AB_Inline: |
| 484 | { |
| 485 | AbSyn what = abArgv(ab)((ab)->abGen.data.argv)[0], origin = abArgv(ab)((ab)->abGen.data.argv)[1]; |
| 486 | |
| 487 | TokenTag tTag = abInfo(tag)abInfoTable[(tag) - AB_START].tokenTag; |
| 488 | |
| 489 | f = f && abPPPuts(buf, keyString(tTag), pmaxchars); |
| 490 | f = f && abPPPuts(buf, " ", pmaxchars); |
| 491 | |
| 492 | if (abIsNotNothing(what)!((what)->abHdr.tag == (AB_Nothing))) { |
| 493 | if (abHasTag(what, AB_With)((what)->abHdr.tag == (AB_With))) { |
| 494 | if (! abHasTag(what->abWith.within, AB_Sequence)((what->abWith.within)->abHdr.tag == (AB_Sequence))) |
| 495 | f = f && abPPIndent(buf, pmaxchars) |
| 496 | && abPPPuts(buf, INDENTATION" ", pmaxchars); |
| 497 | f = f && abPPClipped0(buf, what->abWith.within, pmaxchars); |
| 498 | } |
| 499 | else { |
| 500 | if (! abHasTag(what, AB_Sequence)((what)->abHdr.tag == (AB_Sequence))) |
| 501 | f = f && abPPIndent(buf, pmaxchars) |
| 502 | && abPPPuts(buf, INDENTATION" ", pmaxchars); |
| 503 | f = f && abPPClipped0(buf, what, pmaxchars); |
| 504 | } |
| 505 | f = f && abPPIndent(buf, pmaxchars); |
| 506 | }; |
| 507 | if (abIsNotNothing(origin)!((origin)->abHdr.tag == (AB_Nothing))) |
| 508 | f = f && abPPPuts(buf, "from ", pmaxchars) |
| 509 | && abPPClipped0(buf, origin, pmaxchars); |
| 510 | |
| 511 | } |
| 512 | break; |
| 513 | |
| 514 | case AB_Builtin: |
| 515 | { |
| 516 | f = f && abPPPuts(buf, "import ", pmaxchars); |
| 517 | |
| 518 | if (! abHasTag(ab->abBuiltin.what, AB_Sequence)((ab->abBuiltin.what)->abHdr.tag == (AB_Sequence))) |
| 519 | f = f && abPPIndent(buf, pmaxchars) |
| 520 | && abPPPuts(buf, INDENTATION" ", pmaxchars); |
| 521 | f = f && abPPClipped0(buf, ab->abBuiltin.what, pmaxchars); |
| 522 | f = f && abPPIndent(buf, pmaxchars); |
| 523 | f = f && abPPPuts(buf, "from Builtin", pmaxchars); |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case AB_ForeignImport: |
| 528 | { |
| 529 | f = f && abPPPuts(buf, "import ", pmaxchars); |
| 530 | |
| 531 | if (! abHasTag(ab->abForeignImport.what, AB_Sequence)((ab->abForeignImport.what)->abHdr.tag == (AB_Sequence) )) |
| 532 | f = f && abPPIndent(buf, pmaxchars) |
| 533 | && abPPPuts(buf, INDENTATION" ", pmaxchars); |
| 534 | |
| 535 | f = f && abPPClipped0(buf, ab->abForeignImport.what, pmaxchars); |
| 536 | |
| 537 | f = f && abPPIndent(buf, pmaxchars); |
| 538 | f = f && abPPPuts(buf, "from Foreign(", pmaxchars); |
| 539 | if (abIsNotNothing(ab->abForeignImport.origin)!((ab->abForeignImport.origin)->abHdr.tag == (AB_Nothing ))) |
| 540 | f = f && abPPClipped0(buf, ab->abForeignImport.origin, pmaxchars); |
| 541 | f = f && abPPPuts(buf, ")", pmaxchars); |
| 542 | } |
| 543 | break; |
| 544 | |
| 545 | case AB_Where: |
| 546 | { |
| 547 | AbSyn context = ab->abWhere.context, expr = ab->abWhere.expr; |
| 548 | |
| 549 | f = f && abPPClipped0(buf, expr, pmaxchars); |
| 550 | if (context && abIsNotNothing(context)!((context)->abHdr.tag == (AB_Nothing))) { |
| 551 | f = f && abPPPuts(buf, " where", pmaxchars); |
| 552 | sequenceLevel++; |
| 553 | if (! abHasTag(context, AB_Sequence)((context)->abHdr.tag == (AB_Sequence))) |
| 554 | f = f && abPPIndent(buf, pmaxchars); |
| 555 | f = f && abPPClipped0(buf, context, pmaxchars); |
| 556 | sequenceLevel--; |
| 557 | } |
| 558 | |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | case AB_Add: |
| 563 | { |
| 564 | AbSyn base = ab->abAdd.base; |
| 565 | AbSyn capsule = ab->abAdd.capsule; |
| 566 | |
| 567 | if (abIsNotNothing(base)!((base)->abHdr.tag == (AB_Nothing))) |
| 568 | f = f && abPPClipped0(buf, base, pmaxchars); |
| 569 | f = f && abPPPuts(buf, " add ", pmaxchars); |
| 570 | if (abIsNotNothing(capsule)!((capsule)->abHdr.tag == (AB_Nothing))) { |
| 571 | sequenceLevel++; |
| 572 | if (! abHasTag(capsule, AB_Sequence)((capsule)->abHdr.tag == (AB_Sequence))) |
| 573 | f = f && abPPIndent(buf, pmaxchars); |
| 574 | } |
| 575 | f = f && abPPClipped0(buf, capsule, pmaxchars); |
| 576 | if (abIsNotNothing(capsule)!((capsule)->abHdr.tag == (AB_Nothing))) |
| 577 | sequenceLevel--; |
| 578 | f = f && abPPIndent(buf, pmaxchars); |
| 579 | break; |
| 580 | } |
| 581 | |
| 582 | case AB_With: |
| 583 | { |
| 584 | AbSyn base = ab->abWith.base; |
| 585 | AbSyn within = ab->abWith.within; |
| 586 | if (abIsNotNothing(base)!((base)->abHdr.tag == (AB_Nothing))) |
| 587 | f = f && abPPClipped0(buf, base, pmaxchars); |
| 588 | f = f && abPPPuts(buf, " with ", pmaxchars); |
| 589 | if (abIsNotNothing(within)!((within)->abHdr.tag == (AB_Nothing))) |
| 590 | f = f && abPPClipped0(buf, within, pmaxchars); |
| 591 | break; |
| 592 | } |
| 593 | case AB_Never: |
| 594 | if (abUse(ab)((ab)->abHdr.use) == AB_Use_Elided) |
| 595 | f = f && abPPPuts(buf, "..", pmaxchars); |
| 596 | else |
| 597 | f = f && abPPPuts(buf, "never", pmaxchars); |
| 598 | break; |
| 599 | /* |
| 600 | * Remaining cases are supposed to fall through to default. |
| 601 | * Skip a line before these forms if they are followed by a |
| 602 | * sequence. |
| 603 | */ |
| 604 | |
| 605 | case AB_Default: |
| 606 | if (abIsNothing(abArgv(ab)[0])((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Nothing ))) |
| 607 | break; |
| 608 | /* Fall through */ |
| 609 | default: |
| 610 | { |
| 611 | TokenTag tTag = abInfo(abTag(ab))abInfoTable[(((ab)->abHdr.tag)) - AB_START].tokenTag; |
| 612 | UByte mInf = (tTag == TK_LIMIT) ? 0 : tokInfo(tTag)(tokInfoTable[(tTag)-TK_START]).isMaybeInfix; |
| 613 | |
| 614 | if (tTag == TK_LIMIT) |
| 615 | str = abInfo(tag)abInfoTable[(tag) - AB_START].str; /* !! Can do better */ |
| 616 | else |
| 617 | str = keyString(tTag); |
| 618 | |
| 619 | if (mInf && (abArgc(ab)((ab)->abHdr.argc) == 2)) { |
| 620 | if (abHasTag(abArgv(ab)[0], AB_Comma)((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Comma))) |
| 621 | f = f && abPPPutc(buf, '(', pmaxchars) |
| 622 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars) |
| 623 | && abPPPuts(buf, ") ", pmaxchars); |
| 624 | else if (abIsNotNothing(abArgv(ab)[0])!((((ab)->abGen.data.argv)[0])->abHdr.tag == (AB_Nothing ))) |
| 625 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars) |
| 626 | && abPPPuts(buf, " ", pmaxchars); |
| 627 | |
| 628 | f = f && abPPPuts(buf, str, pmaxchars); |
| 629 | |
| 630 | if (abHasTag(abArgv(ab)[1], AB_Comma)((((ab)->abGen.data.argv)[1])->abHdr.tag == (AB_Comma))) |
| 631 | f = f && abPPPuts(buf, " (", pmaxchars) |
| 632 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars) |
| 633 | && abPPPutc(buf, ')', pmaxchars); |
| 634 | else if (abIsNotNothing(abArgv(ab)[1])!((((ab)->abGen.data.argv)[1])->abHdr.tag == (AB_Nothing ))) |
| 635 | f = f && abPPPutc(buf, ' ', pmaxchars) |
| 636 | && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars); |
| 637 | } |
| 638 | else { |
| 639 | f = f && abPPPuts(buf, str, pmaxchars) |
| 640 | && abPPPutc(buf, ' ', pmaxchars); |
| 641 | |
| 642 | for (i = 0; f && i < ab->abHdr.argc; i++) { |
| 643 | if (abIsNothing(abArgv(ab)[i])((((ab)->abGen.data.argv)[i])->abHdr.tag == (AB_Nothing ))) |
| 644 | continue; |
| 645 | |
| 646 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[i], pmaxchars); |
| 647 | |
| 648 | if (tag == AB_For) |
| 649 | if (i < ab->abHdr.argc - 1) |
| 650 | f = f && abPPPutc(buf, ' ', pmaxchars); |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | break; |
| 655 | } |
| 656 | |
| 657 | return f; |
| 658 | } |
| 659 | |
| 660 | localstatic Bool |
| 661 | abPPSingleNewLine(Buffer buf, long *pmaxchars) |
| 662 | { |
| 663 | return abPPPutc(buf, '\n', pmaxchars); |
| 664 | } |
| 665 | |
| 666 | localstatic Bool |
| 667 | abPPPutc(Buffer buf, int c, long *pmaxchars) |
| 668 | { |
| 669 | if (*pmaxchars >= 1) { |
| 670 | /* Character fits */ |
| 671 | bufAdd1(buf, (char) c); |
| 672 | bufAdd1(buf, char0((char) 0)); |
| 673 | bufBack1(buf); |
| 674 | *pmaxchars -= 1; |
| 675 | return true1; |
| 676 | } |
| 677 | |
| 678 | /* Doesn't fit at all */ |
| 679 | return false((int) 0); |
| 680 | } |
| 681 | |
| 682 | localstatic Bool |
| 683 | abPPPuts(Buffer buf, CString s, long *pmaxchars) |
| 684 | { |
| 685 | int n = strLength(s); |
| 686 | |
| 687 | if (*pmaxchars >= n) { |
| 688 | /* String fits completely */ |
| 689 | |
| 690 | bufPuts(buf, s); |
| 691 | *pmaxchars -= n; |
| 692 | return true1; |
| 693 | } |
| 694 | else if (*pmaxchars > 0) { |
| 695 | /* Fits partially */ |
| 696 | bufAddn(buf, s, *pmaxchars); |
| 697 | bufPuts(buf, ""); |
| 698 | *pmaxchars = 0; |
| 699 | return false((int) 0); |
| 700 | } |
| 701 | else { |
| 702 | /* Doesn't fit at all */ |
| 703 | return false((int) 0); |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | localstatic Bool |
| 708 | abPPIndent(Buffer buf, long *pmaxchars) |
| 709 | { |
| 710 | Bool f = (*pmaxchars > 0); |
| 711 | int i = sequenceLevel; |
| 712 | |
| 713 | if (!f) |
| 714 | return f; |
| 715 | |
| 716 | f = f && abPPSingleNewLine(buf, pmaxchars); |
| 717 | |
| 718 | while (f && (i > 0)) { |
| 719 | f = f && abPPPuts(buf, INDENTATION" ", pmaxchars); |
| 720 | i--; |
| 721 | } |
| 722 | |
| 723 | return f; |
| 724 | } |
| 725 | |
| 726 | localstatic Bool |
| 727 | abPPParens(int prec, Buffer buf, AbSyn ab, long *pmaxchars) |
| 728 | { |
| 729 | /* |
| 730 | * prec is the precedence if ab is an argument to something if prec = |
| 731 | * MAX_PREC, always add parens. |
| 732 | */ |
| 733 | |
| 734 | Bool f = true1, useParens; |
| 735 | |
| 736 | useParens = ((prec == MAX_PREC255) || (getPrecedence(ab) < prec)) ? true1 : false((int) 0); |
| 737 | |
| 738 | if (useParens) |
| 739 | f = f && abPPPutc(buf, '(', pmaxchars); |
| 740 | |
| 741 | f = f && abPPClipped0(buf, ab, pmaxchars); |
| 742 | |
| 743 | if (useParens) |
| 744 | f = f && abPPPutc(buf, ')', pmaxchars); |
| 745 | |
| 746 | return f; |
| 747 | } |
| 748 | |
| 749 | localstatic Bool |
| 750 | abPPApply(Buffer buf, AbSyn ab, long *pmaxchars) |
| 751 | { |
| 752 | /* |
| 753 | * Handle applications, taking into account precedence and |
| 754 | * associativity. |
| 755 | */ |
| 756 | |
| 757 | Bool f = true1; |
| 758 | String str; |
| 759 | AbSyn op = abApplyOp(ab)((ab)->abApply.op); |
| 760 | TokenTag tTag = TK_LIMIT; |
| 761 | UByte mInf = 0; |
| 762 | int i, p; |
| 763 | Bool bracket = abIsTheId(op, ssymBracket)(((op)->abHdr.tag == (AB_Id)) && ((op)->abId.sym )==(ssymBracket)); |
| 764 | Bool brace = abIsTheId(op, ssymBrace )(((op)->abHdr.tag == (AB_Id)) && ((op)->abId.sym )==(ssymBrace)); |
| 765 | |
| 766 | |
| 767 | if (abHasTag(op,AB_Id)((op)->abHdr.tag == (AB_Id))) { |
| 768 | str = symString(op->abId.sym)((op->abId.sym)->str); |
| 769 | tTag = keyTag(str); |
| 770 | mInf = (tTag == TK_LIMIT) ? (UByte) 0 : tokInfo(tTag)(tokInfoTable[(tTag)-TK_START]).isMaybeInfix; |
| 771 | } |
| 772 | |
| 773 | if (bracket || brace) { |
| 774 | f = f && abPPPutc(buf, (bracket ? '[' : '{'), pmaxchars); |
| 775 | |
| 776 | for (i = 1; f && i < ab->abHdr.argc; i++) { |
| 777 | f = f && abPPClipped0(buf, |
| 778 | abArgv(ab)((ab)->abGen.data.argv)[i], pmaxchars); |
| 779 | if (i < ab->abHdr.argc - 1) |
| 780 | f = f && abPPPuts(buf, ", ", pmaxchars); |
| 781 | } |
| 782 | |
| 783 | f = f && abPPPutc(buf, (bracket ? ']' : '}'), pmaxchars); |
| 784 | } |
| 785 | else if ((tTag == KW_Minus) && (abApplyArgc(ab)(((ab)->abHdr.argc)-1) == 1)) { |
| 786 | /* prefix '-': has higher precedence than binary '-' */ |
| 787 | |
| 788 | p = 1 + getPrecedence(op); |
| 789 | f = f && abPPClipped0(buf, op, pmaxchars) |
| 790 | && abPPPutc(buf, ' ', pmaxchars) |
| 791 | && abPPParens(p, buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars); |
| 792 | } |
| 793 | else if (mInf && (abApplyArgc(ab)(((ab)->abHdr.argc)-1) == 2)) { |
| 794 | int q; |
| 795 | p = getPrecedence(op); |
| 796 | |
| 797 | q = (abIsAnyMap(ab)(((((ab)->abHdr.tag == (AB_Apply)) && (((((ab)-> abApply.op))->abHdr.tag == (AB_Id)) && ((((ab)-> abApply.op))->abId.sym)==(ssymArrow))) && (((ab)-> abHdr.argc)-1) == 2) || ((((ab)->abHdr.tag == (AB_Apply)) && (((((ab)->abApply.op))->abHdr.tag == (AB_Id)) && ((((ab)->abApply.op))->abId.sym)==(ssymPackedArrow))) && (((ab)->abHdr.argc)-1) == 2)) && abHasTag(abArgv(ab)[1], AB_Declare)((((ab)->abGen.data.argv)[1])->abHdr.tag == (AB_Declare ))) |
| 798 | ? MAX_PREC255 |
| 799 | : p; |
| 800 | |
| 801 | f = f && abPPParens(q, buf, abArgv(ab)((ab)->abGen.data.argv)[1], pmaxchars) |
| 802 | && abPPPutc(buf, ' ', pmaxchars) |
| 803 | && abPPClipped0(buf, op, pmaxchars) |
| 804 | && abPPPutc(buf, ' ', pmaxchars) |
| 805 | && abPPParens(p, buf, abArgv(ab)((ab)->abGen.data.argv)[2], pmaxchars); |
| 806 | } |
| 807 | else { |
| 808 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[0], pmaxchars); |
| 809 | f = f && abPPPutc(buf, '(', pmaxchars); |
| 810 | |
| 811 | for (i = 1; f && i < ab->abHdr.argc; i++) { |
| 812 | f = f && abPPClipped0(buf, |
| 813 | abArgv(ab)((ab)->abGen.data.argv)[i], pmaxchars); |
| 814 | if (i < ab->abHdr.argc - 1) |
| 815 | f = f && abPPPuts(buf, ", ", pmaxchars); |
| 816 | } |
| 817 | f = f && abPPPutc(buf, ')', pmaxchars); |
| 818 | } |
| 819 | |
| 820 | return f; |
| 821 | } |
| 822 | |
| 823 | localstatic Bool |
| 824 | abPPIsDefine(AbSyn ab) |
| 825 | { |
| 826 | if (abTag(ab)((ab)->abHdr.tag) != AB_Declare) |
| 827 | return false((int) 0); |
| 828 | if (abTag(ab->abDeclare.type)((ab->abDeclare.type)->abHdr.tag) != AB_Define) |
| 829 | return false((int) 0); |
| 830 | return true1; |
| 831 | } |
| 832 | |
| 833 | localstatic Bool |
| 834 | abPPIsElidedLambda(AbSyn ab) |
| 835 | { |
| 836 | if (!abIsAnyLambda(ab->abDefine.rhs)(((ab->abDefine.rhs)->abHdr.tag == (AB_Lambda)) || ((ab ->abDefine.rhs)->abHdr.tag == (AB_PLambda)))) |
| 837 | return false((int) 0); |
| 838 | if (abUse(ab->abDefine.rhs->abLambda.body)((ab->abDefine.rhs->abLambda.body)->abHdr.use) != AB_Use_Elided) |
| 839 | return false((int) 0); |
| 840 | if (abTag(ab->abDefine.lhs)((ab->abDefine.lhs)->abHdr.tag) != AB_Declare) |
| 841 | return false((int) 0); |
| 842 | |
| 843 | return true1; |
| 844 | } |
| 845 | |
| 846 | localstatic Bool |
| 847 | abPPElidedLambda(Buffer buf, AbSyn ab, long *pmaxchars) |
| 848 | { |
| 849 | AbSyn lambda = ab->abDefine.rhs; |
| 850 | AbSyn decl = ab->abDefine.lhs; |
| 851 | Bool f = true1; |
| 852 | |
| 853 | assert(abTag(ab) == AB_Define)do { if (!(((ab)->abHdr.tag) == AB_Define)) _do_assert(("abTag(ab) == AB_Define" ),"abpretty.c",853); } while (0); |
| 854 | assert(abTag(decl) == AB_Declare)do { if (!(((decl)->abHdr.tag) == AB_Declare)) _do_assert( ("abTag(decl) == AB_Declare"),"abpretty.c",854); } while (0); |
| 855 | assert(abTag(decl->abDeclare.id) == AB_Id)do { if (!(((decl->abDeclare.id)->abHdr.tag) == AB_Id)) _do_assert(("abTag(decl->abDeclare.id) == AB_Id"),"abpretty.c" ,855); } while (0); |
| 856 | f = f && abPPLambda(buf, ab->abDefine.rhs, pmaxchars, |
| 857 | abTag(lambda)((lambda)->abHdr.tag) == AB_PLambda, |
| 858 | decl->abDeclare.id); |
| 859 | return f; |
| 860 | |
| 861 | } |
| 862 | |
| 863 | localstatic Bool |
| 864 | abPPLambda(Buffer buf, AbSyn ab, long *pmaxchars, Bool packed, |
| 865 | AbSyn label) |
| 866 | { |
| 867 | /* |
| 868 | * Handle lambda bodies. |
| 869 | */ |
| 870 | |
| 871 | AbSyn body = ab->abLambda.body, param = ab->abLambda.param; |
| 872 | TokenTag tTag; |
| 873 | String op, str; |
| 874 | UByte mInf = 0; |
| 875 | Bool f = true1; |
| 876 | |
| 877 | if (!label && abHasTag(body,AB_Label)((body)->abHdr.tag == (AB_Label))) { |
| 878 | label = body->abLabel.label; |
| 879 | |
| 880 | assert(abHasTag(label, AB_Id))do { if (!(((label)->abHdr.tag == (AB_Id)))) _do_assert(("abHasTag(label, AB_Id)" ),"abpretty.c",880); } while (0); |
| 881 | body = body->abLabel.expr; |
| 882 | } |
| 883 | if (label) { |
| 884 | str = symString(label->abId.sym)((label->abId.sym)->str); |
| 885 | |
| 886 | tTag = keyTag(str); |
| 887 | |
| 888 | mInf = (tTag == TK_LIMIT) ? (UByte) 0 : tokInfo(tTag)(tokInfoTable[(tTag)-TK_START]).isMaybeInfix; |
| 889 | |
| 890 | op = " == "; |
| 891 | } |
| 892 | else if (packed) { |
| 893 | str = ""; |
| 894 | op = " +->* "; |
| 895 | } |
| 896 | else { |
| 897 | str = ""; |
| 898 | op = " +-> "; |
| 899 | } |
| 900 | |
| 901 | if (mInf && abIsNotNothing(ab->abLambda.rtype)!((ab->abLambda.rtype)->abHdr.tag == (AB_Nothing))) |
| 902 | f = f && abPPPutc(buf, '(', pmaxchars); |
| 903 | |
| 904 | if (! abHasTag(param, AB_Comma)((param)->abHdr.tag == (AB_Comma))) |
| 905 | comsgError(param, ALDOR_E_ChkBadParams91); |
| 906 | |
| 907 | if (mInf && (abArgc(param)((param)->abHdr.argc) == 2)) { |
| 908 | int p = getPrecedence(label); |
| 909 | |
| 910 | f = f && abPPParens(p, buf, abArgv(param)((param)->abGen.data.argv)[0], pmaxchars) |
| 911 | && abPPPutc(buf, ' ', pmaxchars) |
| 912 | && abPPPuts(buf, str, pmaxchars) |
| 913 | && abPPPutc(buf, ' ', pmaxchars) |
| 914 | && abPPParens(p, buf, abArgv(param)((param)->abGen.data.argv)[1], pmaxchars); |
| 915 | } |
| 916 | else { |
| 917 | Bool saveUsePiles = usePiles; |
| 918 | Bool brace = label && abIsTheId(label,ssymBrace)(((label)->abHdr.tag == (AB_Id)) && ((label)->abId .sym)==(ssymBrace)); |
| 919 | Bool bracket = label && abIsTheId(label,ssymBracket)(((label)->abHdr.tag == (AB_Id)) && ((label)->abId .sym)==(ssymBracket)); |
| 920 | |
| 921 | usePiles = false((int) 0); |
| 922 | |
| 923 | if (bracket || brace) |
| 924 | f = f && abPPPutc(buf, bracket ? '[' : '{', pmaxchars) |
| 925 | && abPPClipped0(buf, param, pmaxchars) |
| 926 | && abPPPutc(buf, bracket ? ']' : '}', pmaxchars); |
| 927 | else |
| 928 | f = f && abPPPuts(buf, str, pmaxchars) |
| 929 | && abPPParens(MAX_PREC255, buf, param, pmaxchars); |
| 930 | |
| 931 | usePiles = saveUsePiles; |
| 932 | } |
| 933 | |
| 934 | if (abIsNotNothing(ab->abLambda.rtype)!((ab->abLambda.rtype)->abHdr.tag == (AB_Nothing))) { |
| 935 | Bool isWith = abHasTag(ab->abLambda.rtype, AB_With)((ab->abLambda.rtype)->abHdr.tag == (AB_With)); |
| 936 | |
| 937 | if (mInf) |
| 938 | f = f && abPPPuts(buf, ")", pmaxchars); |
| 939 | f = f && abPPPuts(buf, ": ", pmaxchars) |
| 940 | && abPPClipped0(buf, ab->abLambda.rtype, pmaxchars); |
| 941 | if (isWith) { |
| 942 | f = f && abPPIndent(buf, pmaxchars) |
| 943 | && abPPPuts(buf, HALFINDENTATION" ", pmaxchars); |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | f = f && abPPPuts(buf, op, pmaxchars) |
| 948 | && abPPClipped0(buf, body, pmaxchars); |
| 949 | |
| 950 | return f; |
| 951 | } |
| 952 | |
| 953 | localstatic Bool |
| 954 | abPPIterators(Buffer buf, int iterc, AbSyn *iterv, long *pmaxchars) |
| 955 | { |
| 956 | int i; |
| 957 | Bool f = true1; |
| 958 | for (i = 0; i < iterc; i++) { |
| 959 | if (i > 0) |
| 960 | f = f && abPPPutc(buf, ' ', pmaxchars); |
| 961 | f = f && abPPClipped0(buf, iterv[i], pmaxchars); |
| 962 | } |
| 963 | return f; |
| 964 | } |
| 965 | |
| 966 | localstatic Bool |
| 967 | abPPSequence(Buffer buf, AbSyn ab, long *pmaxchars) |
| 968 | { |
| 969 | /* |
| 970 | * Handle sequences, only producing piles is usePiles == true. |
| 971 | */ |
| 972 | |
| 973 | int i; |
| 974 | Bool f = true1; |
| 975 | |
| 976 | if (!usePiles) |
| 977 | f = f && abPPPutc(buf, '(', pmaxchars); |
| 978 | |
| 979 | sequenceLevel++; |
| 980 | |
| 981 | for (i = 0; f && i < ab->abHdr.argc; i++) { |
| 982 | Bool seqInSeq = false((int) 0); |
| 983 | |
| 984 | if (abIsNothing(abArgv(ab)[i])((((ab)->abGen.data.argv)[i])->abHdr.tag == (AB_Nothing ))) |
| 985 | continue; |
| 986 | |
| 987 | if (usePiles) { |
| 988 | f = f && abPPIndent(buf, pmaxchars); |
| 989 | |
| 990 | /* handle sequences within sequences */ |
| 991 | |
| 992 | if (abHasTag(abArgv(ab)[i], AB_Sequence)((((ab)->abGen.data.argv)[i])->abHdr.tag == (AB_Sequence ))) { |
| 993 | seqInSeq = true1; |
| 994 | f = f && abPPPutc(buf, '{', pmaxchars); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | f = f && abPPClipped0(buf, abArgv(ab)((ab)->abGen.data.argv)[i], pmaxchars); |
| 999 | |
| 1000 | if (usePiles && seqInSeq) |
| 1001 | f = f && abPPIndent(buf, pmaxchars) |
| 1002 | && abPPPuts(buf, HALFINDENTATION" ", pmaxchars) |
| 1003 | && abPPPutc(buf, '}', pmaxchars); |
| 1004 | if (!usePiles && (i != ab->abHdr.argc - 1)) |
| 1005 | f = f && abPPPuts(buf, "; ", pmaxchars); |
| 1006 | } |
| 1007 | |
| 1008 | sequenceLevel--; |
| 1009 | |
| 1010 | if (!usePiles) |
| 1011 | f = f && abPPPutc(buf, ')', pmaxchars); |
| 1012 | |
| 1013 | return f; |
| 1014 | } |
| 1015 | |
| 1016 | localstatic int |
| 1017 | getPrecedence(AbSyn ab) |
| 1018 | { |
| 1019 | /* |
| 1020 | * Get operator precedence. See token.c for table. |
| 1021 | */ |
| 1022 | |
| 1023 | if (!ab) |
| 1024 | return 0; |
| 1025 | |
| 1026 | if (abHasTag(ab, AB_Id)((ab)->abHdr.tag == (AB_Id))) { |
| 1027 | TokenTag tTag = keyTag(symString(ab->abId.sym)((ab->abId.sym)->str)); |
| 1028 | |
| 1029 | if (tTag != TK_LIMIT) |
| 1030 | return tokInfo(tTag)(tokInfoTable[(tTag)-TK_START]).precedence; |
| 1031 | } |
| 1032 | else if (abHasTag(ab, AB_Apply)((ab)->abHdr.tag == (AB_Apply))) |
| 1033 | return getPrecedence(abApplyOp(ab)((ab)->abApply.op)); |
| 1034 | |
| 1035 | return tokInfo(abInfo(abTag(ab)).tokenTag)(tokInfoTable[(abInfoTable[(((ab)->abHdr.tag)) - AB_START] .tokenTag)-TK_START]).precedence; |
| 1036 | } |
| 1037 | |
| 1038 | localstatic Bool |
| 1039 | isLeftAssociative(AbSyn ab) |
| 1040 | { |
| 1041 | /* |
| 1042 | * Return true if operator ab is left associative, false otherwise. |
| 1043 | */ |
| 1044 | |
| 1045 | if (!ab) |
| 1046 | return true1; |
| 1047 | |
| 1048 | if (abHasTag(ab, AB_Id)((ab)->abHdr.tag == (AB_Id))) { |
| 1049 | TokenTag tTag = keyTag(symString(ab->abId.sym)((ab->abId.sym)->str)); |
| 1050 | |
| 1051 | if (tTag != TK_LIMIT) |
| 1052 | return tokInfo(tTag)(tokInfoTable[(tTag)-TK_START]).isLeftAssoc; |
| 1053 | } |
| 1054 | else if (abHasTag(ab, AB_Apply)((ab)->abHdr.tag == (AB_Apply))) |
| 1055 | return isLeftAssociative(abApplyOp(ab)((ab)->abApply.op)); |
| 1056 | |
| 1057 | return tokInfo(abInfo(abTag(ab)).tokenTag)(tokInfoTable[(abInfoTable[(((ab)->abHdr.tag)) - AB_START] .tokenTag)-TK_START]).isLeftAssoc; |
| 1058 | } |
| 1059 | |
| 1060 | localstatic Bool |
| 1061 | symeClipped0(Buffer buf, Syme syme, long *pmaxchars) |
| 1062 | { |
| 1063 | Bool f; /* Fits? */ |
| 1064 | CString str; |
| 1065 | |
| 1066 | assert(syme != 0)do { if (!(syme != 0)) _do_assert(("syme != 0"),"abpretty.c", 1066); } while (0); |
| 1067 | |
| 1068 | f = *pmaxchars > 0; |
| 1069 | |
| 1070 | if (! f) |
| 1071 | return f; |
| 1072 | |
| 1073 | if (!syme) |
| 1074 | return (f = f && abPPPuts(buf, "{* NULL *}", pmaxchars)); |
Although the value stored to 'f' is used in the enclosing expression, the value is never actually read from 'f' | |
| 1075 | |
| 1076 | str = symeInfo[symeKind(syme)((((syme)->kind == SYME_Trigger ? libGetAllSymes((syme)-> lib) : ((void*)0)), (syme))->kind)].str + sizeof("SYME_") - 1; |
| 1077 | f = f && abPPPuts(buf, str, pmaxchars); |
| 1078 | f = f && abPPPuts(buf, " ", pmaxchars); |
| 1079 | f = f && abPPPuts(buf, symeString(syme)((((syme)->id))->str), pmaxchars); |
| 1080 | |
| 1081 | #ifdef PrintCircle |
| 1082 | if (symeMark(syme)((SefoMark) ((((((syme)->kind == SYME_Trigger ? libGetAllSymes ((syme)->lib) : ((void*)0)), (syme))->locmask) & (1 << (SYFI_Mark))) ? ((syme)->fieldv)[symeIndex(syme, SYFI_Mark)] : (symeFieldInfo[SYFI_Mark].def))) == sstSerial) |
| 1083 | return f = f && abPPPuts(buf, " *}", pmaxchars); |
| 1084 | symeSetMark(syme, sstSerial)(symeSetFieldVal = ((AInt) (sstSerial)), (((((syme)->kind == SYME_Trigger ? libGetAllSymes((syme)->lib) : ((void*)0)), (syme))->locmask) & (1 << (SYFI_Mark))) ? (((syme )->fieldv)[symeIndex(syme,SYFI_Mark)] = (symeSetFieldVal)) : !((syme)->full) && symeSetFieldVal == (symeFieldInfo [SYFI_Mark].def) ? symeSetFieldVal : symeSetFieldFn(syme,SYFI_Mark ,symeSetFieldVal)); |
| 1085 | #endif |
| 1086 | |
| 1087 | f = f && abPPPuts(buf, " : ", pmaxchars); |
| 1088 | f = f && abPPPuts(buf, tfPretty(symeType(syme)), pmaxchars); |
| 1089 | |
| 1090 | if (symeIsImport(syme)(((((syme)->kind == SYME_Trigger ? libGetAllSymes((syme)-> lib) : ((void*)0)), (syme))->kind) == SYME_Import)) { |
| 1091 | f = f && abPPPuts(buf, " from ", pmaxchars); |
| 1092 | f = f && abPPPuts(buf, tfPretty(symeExporter(syme)), |
| 1093 | pmaxchars); |
| 1094 | } |
| 1095 | |
| 1096 | return f; |
| 1097 | } |