| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <ctype.h> |
|---|
| 15 | |
|---|
| 16 | #include "NewtPrint.h" |
|---|
| 17 | #include "NewtCore.h" |
|---|
| 18 | #include "NewtObj.h" |
|---|
| 19 | #include "NewtEnv.h" |
|---|
| 20 | #include "NewtIO.h" |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | static int32_t NewtGetPrintLength(void); |
|---|
| 26 | static int32_t NewtGetPrintDepth(void); |
|---|
| 27 | |
|---|
| 28 | static bool NewtSymbolIsPrint(char * str, int len); |
|---|
| 29 | static bool NewtStrIsPrint(char * str, int len); |
|---|
| 30 | static char * NewtCharToEscape(int c); |
|---|
| 31 | |
|---|
| 32 | static void NIOPrintIndent(newtStream_t * f, int32_t level); |
|---|
| 33 | static void NIOPrintEscapeStr(newtStream_t * f, char * str, int len); |
|---|
| 34 | static void NIOPrintRef(newtStream_t * f, newtRefArg r); |
|---|
| 35 | static void NIOPrintSpecial(newtStream_t * f, newtRefArg r); |
|---|
| 36 | static void NIOPrintInteger(newtStream_t * f, newtRefArg r); |
|---|
| 37 | static void NIOPrintReal(newtStream_t * f, newtRefArg r); |
|---|
| 38 | static void NIOPrintObjCharacter(newtStream_t * f, newtRefArg r); |
|---|
| 39 | static void NIOPrintObjNamedMP(newtStream_t * f, newtRefArg r); |
|---|
| 40 | static void NIOPrintObjMagicPointer(newtStream_t * f, newtRefArg r); |
|---|
| 41 | static void NIOPrintObjBinary(newtStream_t * f, newtRefArg r); |
|---|
| 42 | static void NIOPrintObjSymbol(newtStream_t * f, newtRefArg r); |
|---|
| 43 | static void NIOPrintObjString(newtStream_t * f, newtRefArg r); |
|---|
| 44 | static void NIOPrintObjArray(newtStream_t * f, newtRefArg r, int32_t depth, bool literal); |
|---|
| 45 | static void NIOPrintFnFrame(newtStream_t * f, newtRefArg r); |
|---|
| 46 | static void NIOPrintRegexFrame(newtStream_t * f, newtRefArg r); |
|---|
| 47 | static void NIOPrintObjFrame(newtStream_t * f, newtRefArg r, int32_t depth, bool literal); |
|---|
| 48 | static void NIOPrintLiteral(newtStream_t * f, newtRefArg r, bool * literalP); |
|---|
| 49 | static void NIOPrintObj2(newtStream_t * f, newtRefArg r, int32_t depth, bool literal); |
|---|
| 50 | |
|---|
| 51 | static void NIOPrintCharacter(newtStream_t * f, newtRefArg r); |
|---|
| 52 | static void NIOPrintSymbol(newtStream_t * f, newtRefArg r); |
|---|
| 53 | static void NIOPrintString(newtStream_t * f, newtRefArg r); |
|---|
| 54 | static void NIOPrintArray(newtStream_t * f, newtRefArg r); |
|---|
| 55 | static void NIOPrintObj(newtStream_t * f, newtRefArg r); |
|---|
| 56 | static void NIOPrint(newtStream_t * f, newtRefArg r); |
|---|
| 57 | |
|---|
| 58 | static void NIOInfo(newtStream_t * f, newtRefArg r); |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | #if 0 |
|---|
| 62 | #pragma mark - |
|---|
| 63 | #endif |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | int32_t NewtGetPrintLength(void) |
|---|
| 71 | { |
|---|
| 72 | newtRefVar n; |
|---|
| 73 | int32_t printLength = -1; |
|---|
| 74 | |
|---|
| 75 | n = NcGetGlobalVar(NSSYM0(printLength)); |
|---|
| 76 | |
|---|
| 77 | if (NewtRefIsInteger(n)) |
|---|
| 78 | printLength = NewtRefToInteger(n); |
|---|
| 79 | |
|---|
| 80 | if (printLength < 0) |
|---|
| 81 | printLength = 0x7fffffff; |
|---|
| 82 | |
|---|
| 83 | return printLength; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | int32_t NewtGetPrintDepth(void) |
|---|
| 94 | { |
|---|
| 95 | newtRefVar n; |
|---|
| 96 | int32_t depth = 3; |
|---|
| 97 | |
|---|
| 98 | n = NcGetGlobalVar(NSSYM0(printDepth)); |
|---|
| 99 | |
|---|
| 100 | if (NewtRefIsInteger(n)) |
|---|
| 101 | depth = NewtRefToInteger(n); |
|---|
| 102 | |
|---|
| 103 | return depth; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | #if 0 |
|---|
| 108 | #pragma mark - |
|---|
| 109 | #endif |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | bool NewtSymbolIsPrint(char * str, int len) |
|---|
| 121 | { |
|---|
| 122 | int c; |
|---|
| 123 | int i; |
|---|
| 124 | |
|---|
| 125 | if (len == 0) |
|---|
| 126 | return false; |
|---|
| 127 | |
|---|
| 128 | c = str[0]; |
|---|
| 129 | |
|---|
| 130 | if ('0' <= c && c <= '9') |
|---|
| 131 | return false; |
|---|
| 132 | |
|---|
| 133 | for (i = 0; i < len && str[i]; i++) |
|---|
| 134 | { |
|---|
| 135 | c = str[i]; |
|---|
| 136 | |
|---|
| 137 | if (c == '_') continue; |
|---|
| 138 | if ('a' <= c && c <= 'z') continue; |
|---|
| 139 | if ('A' <= c && c <= 'Z') continue; |
|---|
| 140 | if ('0' <= c && c <= '9') continue; |
|---|
| 141 | |
|---|
| 142 | return false; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | return true; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | bool NewtStrIsPrint(char * str, int len) |
|---|
| 160 | { |
|---|
| 161 | int i; |
|---|
| 162 | |
|---|
| 163 | for (i = 0; i < len && str[i]; i++) |
|---|
| 164 | { |
|---|
| 165 | if (str[i] == '"') |
|---|
| 166 | return false; |
|---|
| 167 | |
|---|
| 168 | if (! isprint(str[i])) |
|---|
| 169 | return false; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | return true; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | char * NewtCharToEscape(int c) |
|---|
| 185 | { |
|---|
| 186 | char * s = NULL; |
|---|
| 187 | |
|---|
| 188 | switch (c) |
|---|
| 189 | { |
|---|
| 190 | case '\n': |
|---|
| 191 | s = "\\n"; |
|---|
| 192 | break; |
|---|
| 193 | |
|---|
| 194 | case '\r': |
|---|
| 195 | s = "\\r"; |
|---|
| 196 | break; |
|---|
| 197 | |
|---|
| 198 | case '\t': |
|---|
| 199 | s = "\\t"; |
|---|
| 200 | break; |
|---|
| 201 | |
|---|
| 202 | case '"': |
|---|
| 203 | s = "\\\""; |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | return s; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | void NIOPrintIndent(newtStream_t * f, int32_t depth) |
|---|
| 217 | { |
|---|
| 218 | int32_t i, n = newt_env._indentDepth - depth; |
|---|
| 219 | NIOFputc('\n', f); |
|---|
| 220 | if (NEWT_INDENT>0) { |
|---|
| 221 | n *= NEWT_INDENT; |
|---|
| 222 | for (i=0; i<n; i++) { |
|---|
| 223 | NIOFputc('\t', f); |
|---|
| 224 | } |
|---|
| 225 | } else { |
|---|
| 226 | n *= -NEWT_INDENT; |
|---|
| 227 | for (i=0; i<n; i++) { |
|---|
| 228 | NIOFputc(' ', f); |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | void NIOPrintEscapeStr(newtStream_t * f, char * str, int len) |
|---|
| 247 | { |
|---|
| 248 | bool unicode = false; |
|---|
| 249 | char * s; |
|---|
| 250 | int c; |
|---|
| 251 | int i; |
|---|
| 252 | |
|---|
| 253 | for (i = 0; i < len; i++) |
|---|
| 254 | { |
|---|
| 255 | c = str[i]; |
|---|
| 256 | |
|---|
| 257 | s = NewtCharToEscape(c); |
|---|
| 258 | |
|---|
| 259 | if (s != NULL) |
|---|
| 260 | { |
|---|
| 261 | if (unicode) |
|---|
| 262 | { |
|---|
| 263 | NIOFputs("\\u", f); |
|---|
| 264 | unicode = false; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | NIOFputs(s, f); |
|---|
| 268 | } |
|---|
| 269 | else if (isprint(c)) |
|---|
| 270 | { |
|---|
| 271 | if (unicode) |
|---|
| 272 | { |
|---|
| 273 | NIOFputs("\\u", f); |
|---|
| 274 | unicode = false; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | NIOFputc(c, f); |
|---|
| 278 | } |
|---|
| 279 | else |
|---|
| 280 | { |
|---|
| 281 | if (! unicode) |
|---|
| 282 | { |
|---|
| 283 | NIOFputs("\\u", f); |
|---|
| 284 | unicode = true; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | NIOFprintf(f, "%02x%02x", c, str[++i]); |
|---|
| 288 | } |
|---|
| 289 | } |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | #if 0 |
|---|
| 294 | #pragma mark - |
|---|
| 295 | #endif |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | void NIOPrintRef(newtStream_t * f, newtRefArg r) |
|---|
| 308 | { |
|---|
| 309 | NIOFprintf(f, "#%08x", r); |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | |
|---|
| 324 | void NIOPrintSpecial(newtStream_t * f, newtRefArg r) |
|---|
| 325 | { |
|---|
| 326 | int n; |
|---|
| 327 | |
|---|
| 328 | n = NewtRefToSpecial(r); |
|---|
| 329 | NIOFprintf(f, "<Special, %04x>", n); |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | void NIOPrintInteger(newtStream_t * f, newtRefArg r) |
|---|
| 345 | { |
|---|
| 346 | int n; |
|---|
| 347 | |
|---|
| 348 | n = NewtRefToInteger(r); |
|---|
| 349 | NIOFprintf(f, "%d", n); |
|---|
| 350 | } |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | |
|---|
| 359 | |
|---|
| 360 | |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | |
|---|
| 364 | void NIOPrintReal(newtStream_t * f, newtRefArg r) |
|---|
| 365 | { |
|---|
| 366 | double n; |
|---|
| 367 | |
|---|
| 368 | n = NewtRefToReal(r); |
|---|
| 369 | NIOFprintf(f, "%f", n); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | |
|---|
| 378 | |
|---|
| 379 | |
|---|
| 380 | |
|---|
| 381 | |
|---|
| 382 | |
|---|
| 383 | |
|---|
| 384 | void NIOPrintObjCharacter(newtStream_t * f, newtRefArg r) |
|---|
| 385 | { |
|---|
| 386 | char * s; |
|---|
| 387 | int c; |
|---|
| 388 | |
|---|
| 389 | c = NewtRefToCharacter(r); |
|---|
| 390 | |
|---|
| 391 | NIOFputc('$', f); |
|---|
| 392 | |
|---|
| 393 | s = NewtCharToEscape(c); |
|---|
| 394 | |
|---|
| 395 | if (s != NULL) |
|---|
| 396 | NIOFputs(s, f); |
|---|
| 397 | else if (0xff00 & c) |
|---|
| 398 | NIOFprintf(f, "\\u%04x", c); |
|---|
| 399 | else if (isprint(c)) |
|---|
| 400 | NIOFputc(c, f); |
|---|
| 401 | else |
|---|
| 402 | NIOFprintf(f, "\\%02x", c); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | |
|---|
| 406 | #ifdef __NAMED_MAGIC_POINTER__ |
|---|
| 407 | |
|---|
| 408 | |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | |
|---|
| 413 | |
|---|
| 414 | |
|---|
| 415 | |
|---|
| 416 | |
|---|
| 417 | |
|---|
| 418 | |
|---|
| 419 | void NIOPrintObjNamedMP(newtStream_t * f, newtRefArg r) |
|---|
| 420 | { |
|---|
| 421 | newtRefVar sym; |
|---|
| 422 | |
|---|
| 423 | sym = NewtMPToSymbol(r); |
|---|
| 424 | |
|---|
| 425 | NIOFputc('@', f); |
|---|
| 426 | NIOPrintObjSymbol(f, sym); |
|---|
| 427 | } |
|---|
| 428 | |
|---|
| 429 | #endif |
|---|
| 430 | |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | |
|---|
| 437 | |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | |
|---|
| 441 | |
|---|
| 442 | |
|---|
| 443 | void NIOPrintObjMagicPointer(newtStream_t * f, newtRefArg r) |
|---|
| 444 | { |
|---|
| 445 | int32_t table; |
|---|
| 446 | int32_t index; |
|---|
| 447 | |
|---|
| 448 | table = NewtMPToTable(r); |
|---|
| 449 | index = NewtMPToIndex(r); |
|---|
| 450 | |
|---|
| 451 | NIOFprintf(f, "@%d", index); |
|---|
| 452 | } |
|---|
| 453 | |
|---|
| 454 | |
|---|
| 455 | |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | |
|---|
| 459 | |
|---|
| 460 | |
|---|
| 461 | |
|---|
| 462 | |
|---|
| 463 | |
|---|
| 464 | |
|---|
| 465 | |
|---|
| 466 | void NIOPrintObjBinary(newtStream_t * f, newtRefArg r) |
|---|
| 467 | { |
|---|
| 468 | newtRefVar klass; |
|---|
| 469 | int ptr; |
|---|
| 470 | int len; |
|---|
| 471 | |
|---|
| 472 | ptr = r; |
|---|
| 473 | len = NewtBinaryLength(r); |
|---|
| 474 | NIOFputs("<Binary, ", f); |
|---|
| 475 | |
|---|
| 476 | |
|---|
| 477 | klass = NcClassOf(r); |
|---|
| 478 | |
|---|
| 479 | if (NewtRefIsSymbol(klass)) |
|---|
| 480 | { |
|---|
| 481 | NIOFputs("class \"", f); |
|---|
| 482 | NIOPrintObj2(f, klass, 0, true); |
|---|
| 483 | NIOFputs("\", ", f); |
|---|
| 484 | } |
|---|
| 485 | |
|---|
| 486 | NIOFprintf(f, "length %d>", len); |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | |
|---|
| 490 | |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | |
|---|
| 494 | |
|---|
| 495 | |
|---|
| 496 | |
|---|
| 497 | |
|---|
| 498 | |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | void NIOPrintObjSymbol(newtStream_t * f, newtRefArg r) |
|---|
| 502 | { |
|---|
| 503 | newtSymDataRef sym; |
|---|
| 504 | int len; |
|---|
| 505 | |
|---|
| 506 | sym = NewtRefToSymbol(r); |
|---|
| 507 | len = NewtSymbolLength(r); |
|---|
| 508 | |
|---|
| 509 | if (NewtSymbolIsPrint(sym->name, len)) |
|---|
| 510 | { |
|---|
| 511 | NIOFputs(sym->name, f); |
|---|
| 512 | } |
|---|
| 513 | else |
|---|
| 514 | { |
|---|
| 515 | NIOFputc('|', f); |
|---|
| 516 | NIOPrintEscapeStr(f, sym->name, len); |
|---|
| 517 | NIOFputc('|', f); |
|---|
| 518 | } |
|---|
| 519 | } |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | |
|---|
| 532 | |
|---|
| 533 | void NIOPrintObjString(newtStream_t * f, newtRefArg r) |
|---|
| 534 | { |
|---|
| 535 | char * s; |
|---|
| 536 | int len; |
|---|
| 537 | |
|---|
| 538 | s = NewtRefToString(r); |
|---|
| 539 | len = NewtStringLength(r); |
|---|
| 540 | |
|---|
| 541 | if (NewtStrIsPrint(s, len)) |
|---|
| 542 | { |
|---|
| 543 | |
|---|
| 544 | NIOFputs("\"", f); |
|---|
| 545 | NIOFputs(s, f); |
|---|
| 546 | NIOFputs("\"", f); |
|---|
| 547 | } |
|---|
| 548 | else |
|---|
| 549 | { |
|---|
| 550 | NIOFputc('"', f); |
|---|
| 551 | NIOPrintEscapeStr(f, s, len); |
|---|
| 552 | NIOFputc('"', f); |
|---|
| 553 | } |
|---|
| 554 | } |
|---|
| 555 | |
|---|
| 556 | |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | |
|---|
| 560 | |
|---|
| 561 | |
|---|
| 562 | |
|---|
| 563 | |
|---|
| 564 | |
|---|
| 565 | |
|---|
| 566 | |
|---|
| 567 | |
|---|
| 568 | |
|---|
| 569 | |
|---|
| 570 | void NIOPrintObjArray(newtStream_t * f, newtRefArg r, int32_t depth, bool literal) |
|---|
| 571 | { |
|---|
| 572 | newtObjRef obj; |
|---|
| 573 | newtRef * slots; |
|---|
| 574 | newtRefVar klass; |
|---|
| 575 | uint32_t len; |
|---|
| 576 | uint32_t i; |
|---|
| 577 | |
|---|
| 578 | obj = NewtRefToPointer(r); |
|---|
| 579 | len = NewtObjSlotsLength(obj); |
|---|
| 580 | slots = NewtObjToSlots(obj); |
|---|
| 581 | |
|---|
| 582 | if (literal && NcClassOf(r) == NSSYM0(pathExpr)) |
|---|
| 583 | { |
|---|
| 584 | for (i = 0; i < len; i++) |
|---|
| 585 | { |
|---|
| 586 | if (0 < i) |
|---|
| 587 | NIOFputs(".", f); |
|---|
| 588 | |
|---|
| 589 | NIOPrintObj2(f, slots[i], 0, literal); |
|---|
| 590 | } |
|---|
| 591 | } |
|---|
| 592 | else |
|---|
| 593 | { |
|---|
| 594 | NIOFputs("[", f); |
|---|
| 595 | |
|---|
| 596 | klass = NcClassOf(r); |
|---|
| 597 | |
|---|
| 598 | if (NewtRefIsNotNIL(klass) && ! NewtRefEqual(klass, NSSYM0(array))) |
|---|
| 599 | { |
|---|
| 600 | if (NEWT_INDENT) NIOPrintIndent(f, depth-1); |
|---|
| 601 | NIOPrintObj2(f, klass, 0, true); |
|---|
| 602 | NIOFputs(": ", f); |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | if (depth < 0) |
|---|
| 606 | { |
|---|
| 607 | NIOPrintRef(f, r); |
|---|
| 608 | } |
|---|
| 609 | else |
|---|
| 610 | { |
|---|
| 611 | int32_t printLength; |
|---|
| 612 | |
|---|
| 613 | depth--; |
|---|
| 614 | printLength = NewtGetPrintLength(); |
|---|
| 615 | |
|---|
| 616 | for (i = 0; i < len; i++) |
|---|
| 617 | { |
|---|
| 618 | if (0 < i) |
|---|
| 619 | NIOFputs(", ", f); |
|---|
| 620 | |
|---|
| 621 | if (printLength <= i) |
|---|
| 622 | { |
|---|
| 623 | NIOFputs("...", f); |
|---|
| 624 | break; |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| 627 | if (NEWT_INDENT) NIOPrintIndent(f, depth); |
|---|
| 628 | NIOPrintObj2(f, slots[i], depth, literal); |
|---|
| 629 | } |
|---|
| 630 | depth++; |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | if (NEWT_INDENT) NIOPrintIndent(f, depth); |
|---|
| 634 | NIOFputs("]", f); |
|---|
| 635 | } |
|---|
| 636 | } |
|---|
| 637 | |
|---|
| 638 | |
|---|
| 639 | |
|---|
| 640 | |
|---|
| 641 | |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | void NIOPrintFnFrame(newtStream_t * f, newtRefArg r) |
|---|
| 651 | { |
|---|
| 652 | newtRefVar indefinite; |
|---|
| 653 | int32_t numArgs; |
|---|
| 654 | char * indefiniteStr = ""; |
|---|
| 655 | |
|---|
| 656 | numArgs = NewtRefToInteger(NcGetSlot(r, NSSYM0(numArgs))); |
|---|
| 657 | indefinite = NcGetSlot(r, NSSYM0(indefinite)); |
|---|
| 658 | |
|---|
| 659 | if (NewtRefIsNotNIL(indefinite)) |
|---|
| 660 | indefiniteStr = "..."; |
|---|
| 661 | |
|---|
| 662 | NIOFprintf(f, "<function, %d arg(s)%s #%08x>", numArgs, indefiniteStr, r); |
|---|
| 663 | } |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | void NIOPrintRegexFrame(newtStream_t * f, newtRefArg r) |
|---|
| 678 | { |
|---|
| 679 | newtRefVar pattern; |
|---|
| 680 | newtRefVar option; |
|---|
| 681 | |
|---|
| 682 | pattern = NcGetSlot(r, NSSYM0(pattern)); |
|---|
| 683 | option = NcGetSlot(r, NSSYM0(option)); |
|---|
| 684 | |
|---|
| 685 | |
|---|
| 686 | NIOFputs("/", f); |
|---|
| 687 | NIOFputs(NewtRefToString(pattern), f); |
|---|
| 688 | NIOFputs("/", f); |
|---|
| 689 | |
|---|
| 690 | if (NewtRefIsString(option)) |
|---|
| 691 | NIOFputs(NewtRefToString(option), f); |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | |
|---|
| 695 | |
|---|
| 696 | |
|---|
| 697 | |
|---|
| 698 | |
|---|
| 699 | |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | |
|---|
| 703 | |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | |
|---|
| 707 | |
|---|
| 708 | void NIOPrintObjFrame(newtStream_t * f, newtRefArg r, int32_t depth, bool literal) |
|---|
| 709 | { |
|---|
| 710 | newtObjRef obj; |
|---|
| 711 | newtRef * slots; |
|---|
| 712 | newtRefVar slot; |
|---|
| 713 | uint32_t index; |
|---|
| 714 | uint32_t len; |
|---|
| 715 | uint32_t i; |
|---|
| 716 | |
|---|
| 717 | if (NewtRefIsFunction(r) && ! NEWT_DUMPBC) |
|---|
| 718 | { |
|---|
| 719 | NIOPrintFnFrame(f, r); |
|---|
| 720 | return; |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | if (NewtRefIsRegex(r) && ! NEWT_DUMPBC) |
|---|
| 724 | { |
|---|
| 725 | NIOPrintRegexFrame(f, r); |
|---|
| 726 | return; |
|---|
| 727 | } |
|---|
| 728 | |
|---|
| 729 | obj = NewtRefToPointer(r); |
|---|
| 730 | len = NewtObjSlotsLength(obj); |
|---|
| 731 | slots = NewtObjToSlots(obj); |
|---|
| 732 | |
|---|
| 733 | NIOFputs("{", f); |
|---|
| 734 | |
|---|
| 735 | if (depth < 0) |
|---|
| 736 | { |
|---|
| 737 | NIOPrintRef(f, r); |
|---|
| 738 | } |
|---|
| 739 | else |
|---|
| 740 | { |
|---|
| 741 | int32_t printLength; |
|---|
| 742 | |
|---|
| 743 | depth--; |
|---|
| 744 | printLength = NewtGetPrintLength(); |
|---|
| 745 | |
|---|
| 746 | for (i = 0; i < len; i++) |
|---|
| 747 | { |
|---|
| 748 | if (0 < i) { |
|---|
| 749 | NIOFputs(", ", f); |
|---|
| 750 | } |
|---|
| 751 | |
|---|
| 752 | if (printLength <= i) |
|---|
| 753 | { |
|---|
| 754 | NIOFputs("...", f); |
|---|
| 755 | if (NEWT_INDENT) NIOPrintIndent(f, depth); |
|---|
| 756 | break; |
|---|
| 757 | } |
|---|
| 758 | if (NEWT_INDENT) NIOPrintIndent(f, depth); |
|---|
| 759 | |
|---|
| 760 | slot = NewtGetMapIndex(obj->as.map, i, &index); |
|---|
| 761 | if (slot == kNewtRefUnbind) break; |
|---|
| 762 | |
|---|
| 763 | NIOPrintObjSymbol(f, slot); |
|---|
| 764 | NIOFputs(": ", f); |
|---|
| 765 | NIOPrintObj2(f, slots[i], depth, literal); |
|---|
| 766 | |
|---|
| 767 | } |
|---|
| 768 | if (NEWT_INDENT) NIOPrintIndent(f, depth+1); |
|---|
| 769 | } |
|---|
| 770 | |
|---|
| 771 | NIOFputs("}", f); |
|---|
| 772 | } |
|---|
| 773 | |
|---|
| 774 | |
|---|
| 775 | |
|---|
| 776 | |
|---|
| 777 | |
|---|
| 778 | |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | |
|---|
| 782 | |
|---|
| 783 | |
|---|
| 784 | |
|---|
| 785 | |
|---|
| 786 | |
|---|
| 787 | void NIOPrintLiteral(newtStream_t * f, newtRefArg r, bool * literalP) |
|---|
| 788 | { |
|---|
| 789 | if (! *literalP && NewtRefIsLiteral(r)) |
|---|
| 790 | { |
|---|
| 791 | NIOFputc('\'', f); |
|---|
| 792 | *literalP = true; |
|---|
| 793 | } |
|---|
| 794 | } |
|---|
| 795 | |
|---|
| 796 | |
|---|
| 797 | |
|---|
| 798 | |
|---|
| 799 | |
|---|
| 800 | |
|---|
| 801 | |
|---|
| 802 | |
|---|
| 803 | |
|---|
| 804 | |
|---|
| 805 | |
|---|
| 806 | |
|---|
| 807 | |
|---|
| 808 | |
|---|
| 809 | |
|---|
| 810 | void NIOPrintObj2(newtStream_t * f, newtRefArg r, int32_t depth, bool literal) |
|---|
| 811 | { |
|---|
| 812 | switch (NewtGetRefType(r, true)) |
|---|
| 813 | { |
|---|
| 814 | case kNewtNil: |
|---|
| 815 | NIOFputs("NIL", f); |
|---|
| 816 | break; |
|---|
| 817 | |
|---|
| 818 | case kNewtTrue: |
|---|
| 819 | NIOFputs("TRUE", f); |
|---|
| 820 | break; |
|---|
| 821 | |
|---|
| 822 | case kNewtUnbind: |
|---|
| 823 | NIOFputs("#UNBIND", f); |
|---|
| 824 | break; |
|---|
| 825 | |
|---|
| 826 | case kNewtSpecial: |
|---|
| 827 | NIOPrintSpecial(f, r); |
|---|
| 828 | break; |
|---|
| 829 | |
|---|
| 830 | case kNewtInt30: |
|---|
| 831 | case kNewtInt32: |
|---|
| 832 | NIOPrintInteger(f, r); |
|---|
| 833 | break; |
|---|
| 834 | |
|---|
| 835 | case kNewtReal: |
|---|
| 836 | NIOPrintReal(f, r); |
|---|
| 837 | break; |
|---|
| 838 | |
|---|
| 839 | case kNewtCharacter: |
|---|
| 840 | NIOPrintObjCharacter(f, r); |
|---|
| 841 | break; |
|---|
| 842 | |
|---|
| 843 | case kNewtMagicPointer: |
|---|
| 844 | if (0 <= depth) |
|---|
| 845 | { |
|---|
| 846 | newtRefVar r2; |
|---|
| 847 | |
|---|
| 848 | r2 = NcResolveMagicPointer(r); |
|---|
| 849 | |
|---|
| 850 | if (! NewtRefIsMagicPointer(r2)) |
|---|
| 851 | { |
|---|
| 852 | NIOPrintObj2(f, r2, depth, literal); |
|---|
| 853 | break; |
|---|
| 854 | } |
|---|
| 855 | } |
|---|
| 856 | |
|---|
| 857 | #ifdef __NAMED_MAGIC_POINTER__ |
|---|
| 858 | if (NewtRefIsNamedMP(r)) |
|---|
| 859 | { |
|---|
| 860 | NIOPrintObjNamedMP(f, r); |
|---|
| 861 | break; |
|---|
| 862 | } |
|---|
| 863 | #endif |
|---|
| 864 | |
|---|
| 865 | NIOPrintObjMagicPointer(f, r); |
|---|
| 866 | break; |
|---|
| 867 | |
|---|
| 868 | case kNewtBinary: |
|---|
| 869 | NIOPrintObjBinary(f, r); |
|---|
| 870 | break; |
|---|
| 871 | |
|---|
| 872 | case kNewtArray: |
|---|
| 873 | NIOPrintLiteral(f, r, &literal); |
|---|
| 874 | NIOPrintObjArray(f, r, depth, literal); |
|---|
| 875 | break; |
|---|
| 876 | |
|---|
| 877 | case kNewtFrame: |
|---|
| 878 | NIOPrintLiteral(f, r, &literal); |
|---|
| 879 | NIOPrintObjFrame(f, r, depth, literal); |
|---|
| 880 | break; |
|---|
| 881 | |
|---|
| 882 | case kNewtSymbol: |
|---|
| 883 | NIOPrintLiteral(f, r, &literal); |
|---|
| 884 | NIOPrintObjSymbol(f, r); |
|---|
| 885 | break; |
|---|
| 886 | |
|---|
| 887 | case kNewtString: |
|---|
| 888 | NIOPrintObjString(f, r); |
|---|
| 889 | break; |
|---|
| 890 | |
|---|
| 891 | default: |
|---|
| 892 | NIOFputs("###UNKNOWN###", f); |
|---|
| 893 | break; |
|---|
| 894 | } |
|---|
| 895 | } |
|---|
| 896 | |
|---|
| 897 | |
|---|
| 898 | |
|---|
| 899 | |
|---|
| 900 | |
|---|
| 901 | |
|---|
| 902 | |
|---|
| 903 | |
|---|
| 904 | |
|---|
| 905 | |
|---|
| 906 | |
|---|
| 907 | |
|---|
| 908 | |
|---|
| 909 | void NIOPrintObj(newtStream_t * f, newtRefArg r) |
|---|
| 910 | { |
|---|
| 911 | int32_t depth = NewtGetPrintDepth(); |
|---|
| 912 | newt_env._indentDepth = depth; |
|---|
| 913 | NIOPrintObj2(f, r, depth, false); |
|---|
| 914 | } |
|---|
| 915 | |
|---|
| 916 | |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | |
|---|
| 920 | |
|---|
| 921 | |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | |
|---|
| 925 | |
|---|
| 926 | void NewtPrintObj(FILE * f, newtRefArg r) |
|---|
| 927 | { |
|---|
| 928 | newtStream_t stream; |
|---|
| 929 | |
|---|
| 930 | NIOSetFile(&stream, f); |
|---|
| 931 | NIOPrintObj(&stream, r); |
|---|
| 932 | } |
|---|
| 933 | |
|---|
| 934 | |
|---|
| 935 | |
|---|
| 936 | |
|---|
| 937 | |
|---|
| 938 | |
|---|
| 939 | |
|---|
| 940 | |
|---|
| 941 | |
|---|
| 942 | |
|---|
| 943 | |
|---|
| 944 | void NewtPrintObject(FILE * f, newtRefArg r) |
|---|
| 945 | { |
|---|
| 946 | newtStream_t stream; |
|---|
| 947 | |
|---|
| 948 | NIOSetFile(&stream, f); |
|---|
| 949 | |
|---|
| 950 | NIOPrintObj(&stream, r); |
|---|
| 951 | NIOFputs("\n", &stream); |
|---|
| 952 | } |
|---|
| 953 | |
|---|
| 954 | |
|---|
| 955 | |
|---|
| 956 | |
|---|
| 957 | |
|---|
| 958 | |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | |
|---|
| 963 | |
|---|
| 964 | |
|---|
| 965 | |
|---|
| 966 | void NIOPrintCharacter(newtStream_t * f, newtRefArg r) |
|---|
| 967 | { |
|---|
| 968 | NIOFputc(NewtRefToCharacter(r), f); |
|---|
| 969 | } |
|---|
| 970 | |
|---|
| 971 | |
|---|
| 972 | |
|---|
| 973 | |
|---|
| 974 | |
|---|
| 975 | |
|---|
| 976 | |
|---|
| 977 | |
|---|
| 978 | |
|---|
| 979 | |
|---|
| 980 | |
|---|
| 981 | |
|---|
| 982 | |
|---|
| 983 | void NIOPrintSymbol(newtStream_t * f, newtRefArg r) |
|---|
| 984 | { |
|---|
| 985 | newtSymDataRef sym; |
|---|
| 986 | |
|---|
| 987 | sym = NewtRefToSymbol(r); |
|---|
| 988 | NIOFputs(sym->name, f); |
|---|
| 989 | } |
|---|
| 990 | |
|---|
| 991 | |
|---|
| 992 | |
|---|
| 993 | |
|---|
| 994 | |
|---|
| 995 | |
|---|
| 996 | |
|---|
| 997 | |
|---|
| 998 | |
|---|
| 999 | |
|---|
| 1000 | |
|---|
| 1001 | |
|---|
| 1002 | |
|---|
| 1003 | void NIOPrintString(newtStream_t * f, newtRefArg r) |
|---|
| 1004 | { |
|---|
| 1005 | NIOFputs(NewtRefToString(r), f); |
|---|
| 1006 | } |
|---|
| 1007 | |
|---|
| 1008 | |
|---|
| 1009 | |
|---|
| 1010 | |
|---|
| 1011 | |
|---|
| 1012 | |
|---|
| 1013 | |
|---|
| 1014 | |
|---|
| 1015 | |
|---|
| 1016 | |
|---|
| 1017 | |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| 1020 | void NIOPrintArray(newtStream_t * f, newtRefArg r) |
|---|
| 1021 | { |
|---|
| 1022 | newtRef * slots; |
|---|
| 1023 | uint32_t len; |
|---|
| 1024 | uint32_t i; |
|---|
| 1025 | |
|---|
| 1026 | slots = NewtRefToSlots(r); |
|---|
| 1027 | len = NewtLength(r); |
|---|
| 1028 | |
|---|
| 1029 | for (i = 0; i < len; i++) |
|---|
| 1030 | { |
|---|
| 1031 | NIOPrint(f, slots[i]); |
|---|
| 1032 | } |
|---|
| 1033 | } |
|---|
| 1034 | |
|---|
| 1035 | |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | |
|---|
| 1039 | |
|---|
| 1040 | |
|---|
| 1041 | |
|---|
| 1042 | |
|---|
| 1043 | |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | |
|---|
| 1047 | void NIOPrint(newtStream_t * f, newtRefArg r) |
|---|
| 1048 | { |
|---|
| 1049 | switch (NewtGetRefType(r, true)) |
|---|
| 1050 | { |
|---|
| 1051 | case kNewtNil: |
|---|
| 1052 | case kNewtTrue: |
|---|
| 1053 | case kNewtUnbind: |
|---|
| 1054 | break; |
|---|
| 1055 | |
|---|
| 1056 | case kNewtSpecial: |
|---|
| 1057 | NIOPrintSpecial(f, r); |
|---|
| 1058 | break; |
|---|
| 1059 | |
|---|
| 1060 | case kNewtInt30: |
|---|
| 1061 | case kNewtInt32: |
|---|
| 1062 | NIOPrintInteger(f, r); |
|---|
| 1063 | break; |
|---|
| 1064 | |
|---|
| 1065 | case kNewtReal: |
|---|
| 1066 | NIOPrintReal(f, r); |
|---|
| 1067 | break; |
|---|
| 1068 | |
|---|
| 1069 | case kNewtCharacter: |
|---|
| 1070 | NIOPrintCharacter(f, r); |
|---|
| 1071 | break; |
|---|
| 1072 | |
|---|
| 1073 | case kNewtMagicPointer: |
|---|
| 1074 | { |
|---|
| 1075 | newtRefVar r2; |
|---|
| 1076 | |
|---|
| 1077 | r2 = NcResolveMagicPointer(r); |
|---|
| 1078 | |
|---|
| 1079 | if (! NewtRefIsMagicPointer(r2)) |
|---|
| 1080 | { |
|---|
| 1081 | NIOPrint(f, r2); |
|---|
| 1082 | } |
|---|
| 1083 | } |
|---|
| 1084 | break; |
|---|
| 1085 | |
|---|
| 1086 | case kNewtArray: |
|---|
| 1087 | NIOPrintArray(f, r); |
|---|
| 1088 | break; |
|---|
| 1089 | |
|---|
| 1090 | case kNewtBinary: |
|---|
| 1091 | case kNewtFrame: |
|---|
| 1092 | break; |
|---|
| 1093 | |
|---|
| 1094 | case kNewtSymbol: |
|---|
| 1095 | NIOPrintSymbol(f, r); |
|---|
| 1096 | break; |
|---|
| 1097 | |
|---|
| 1098 | case kNewtString: |
|---|
| 1099 | NIOPrintString(f, r); |
|---|
| 1100 | break; |
|---|
| 1101 | |
|---|
| 1102 | default: |
|---|
| 1103 | break |
|---|