/* printtree1 - recursively print tree p */ static void printtree1(Tree p, int fd, int lev) { FILE *f = fd == 1 ? stdout : stderr; int i; static char blanks[] = " "; if (p == 0 || *printed(i = nodeid(p))) return; fprint(f, "#%d%S%S", i, blanks, i < 10 ? 2 : i < 100 ? 1 : 0, blanks, lev); fprint(f, "%s %t", opname(p->op), p->type); *printed(i) = 1; for (i = 0; i < NELEMS(p->kids); i++) if (p->kids[i]) fprint(f, " #%d", nodeid(p->kids[i])); if (p->op == FIELD && p->u.field) fprint(f, " %s %d..%d", p->u.field->name, fieldsize(p->u.field) + fieldright(p->u.field), fieldright(p->u.field)); else if (generic(p->op) == CNST) fprint(f, " %s", vtoa(p->type, p->u.v)); else if (p->u.sym) fprint(f, " %s", p->u.sym->name); if (p->node) fprint(f, " node=%p", p->node); fprint(f, "\n"); for (i = 0; i < NELEMS(p->kids); i++) printtree1(p->kids[i], fd, lev + 1); }
void printtree1(node* root, int level) { if (root == 0) ; else if (root->node_type == ID) { printf("%*s", 2*level, ""); printf("%s\n", symtable[root->leaf_value].lexptr); } else if (root->node_type == NUM) { printf("%*s", 2*level, ""); printf("%d\n", root->leaf_value); } else if (root->node_type == ';') { printf("%*s", 2*level, ""); printtree1(root->args[0], level + 1); printf("%*s", 2*level, ""); printf(";\n"); printtree1(root->args[1], level); } else if (root->node_type == MOD) { printf("%*s", 2*level, ""); printf("mod\n"); printtree1(root->args[0], level + 1); printtree1(root->args[1], level + 1); } else if (root->node_type == DIV) { printf("%*s", 2*level, ""); printf("div\n"); printtree1(root->args[0], level + 1); printtree1(root->args[1], level + 1); } else if (root->node_type == IF) { printf("%*s", 2*level, ""); printf("if\n"); printtree1(root->args[0], level + 1); printtree1(root->args[1], level + 1); printtree1(root->args[2], level + 2); } else if (root->node_type == WHILE) { printf("%*s", 2*level, ""); printf("while\n"); printtree1(root->args[0], level + 1); printtree1(root->args[1], level); } else if (root->node_type == PRINT) { printf("%*s", 2*level, ""); printf("print\n"); printtree1(root->args[0], level + 1); printtree1(root->args[1], level + 1); } else if (root->node_type == READ) { printf("%*s", 2*level, ""); printf("read\n"); printtree1(root->args[0], level + 1); printtree1(root->args[1], level + 1); } else { //if (root->node_type == '+') { printf("%*s", 2*level, ""); printf("%c\n", root->node_type); printtree1(root->args[0], level + 1); printtree1(root->args[1], level + 1); } }
/* printtree - print tree p on fd */ void printtree(Tree p, int fd) { (void)printed(0); printtree1(p, fd, 1); }
void treeprint(node* root) { printf("Syntax tree:\n"); printtree1(root, 0); }