Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* List a node on a file */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include "PROTO.h" |
| 6 | #include "token.h" |
| 7 | #include "node.h" |
| 8 | |
| 9 | static int level, atbol; |
| 10 | |
| 11 | static void |
| 12 | list1node(fp, n) |
| 13 | FILE *fp; |
| 14 | node *n; |
| 15 | { |
| 16 | if (n == 0) |
| 17 | return; |
| 18 | if (ISNONTERMINAL(TYPE(n))) { |
| 19 | int i; |
| 20 | for (i = 0; i < NCH(n); i++) |
| 21 | list1node(fp, CHILD(n, i)); |
| 22 | } |
| 23 | else if (ISTERMINAL(TYPE(n))) { |
| 24 | switch (TYPE(n)) { |
| 25 | case INDENT: |
| 26 | ++level; |
| 27 | break; |
| 28 | case DEDENT: |
| 29 | --level; |
| 30 | break; |
| 31 | default: |
| 32 | if (atbol) { |
| 33 | int i; |
| 34 | for (i = 0; i < level; ++i) |
| 35 | fprintf(fp, "\t"); |
| 36 | atbol = 0; |
| 37 | } |
| 38 | if (TYPE(n) == NEWLINE) { |
| 39 | if (STR(n) != NULL) |
| 40 | fprintf(fp, "%s", STR(n)); |
| 41 | fprintf(fp, "\n"); |
| 42 | atbol = 1; |
| 43 | } |
| 44 | else |
| 45 | fprintf(fp, "%s ", STR(n)); |
| 46 | break; |
| 47 | } |
| 48 | } |
| 49 | else |
| 50 | fprintf(fp, "? "); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | listnode(fp, n) |
| 55 | FILE *fp; |
| 56 | node *n; |
| 57 | { |
| 58 | level = 0; |
| 59 | atbol = 1; |
| 60 | list1node(fp, n); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | listtree(n) |
| 65 | node *n; |
| 66 | { |
| 67 | listnode(stdout, n); |
| 68 | } |