ref: 6c5d52ea0865cb623eead43535739b008b526928
parent: aaeec1e16228d8a144d022621786639961912313
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sat Oct 5 18:02:19 EDT 2013
Simplify prtree Due to the new representation of the tree, it is easiar run over a tree, so we can remove the nchild field in the representation table. It is also no needed the prtree_helper function, because each increment of indent is always followed of a decrement, so we can use only a function because when prtree returns indent holds the same value it had in the beginning.
--- a/tree.c
+++ b/tree.c
@@ -1,8 +1,6 @@
#include <assert.h>
-#include <stddef.h>
#include <stdio.h>
-#include <stdint.h>
#include "cc.h"
#include "syntax.h"
@@ -24,9 +22,6 @@
};
-static unsigned char indent; /* used for pretty printing the tree*/
-
-
struct node *
nodesym(struct symbol *sym)
{@@ -69,109 +64,97 @@
return p->tree;
}
-static void
-prtree_helper(register struct node *np)
+void
+prtree(register struct node *np)
{- static struct optab {- unsigned char nchild;
- const char *txt;
- } *bp, optab [] = {- [OCALL] = {2, "()"},- [OARY] = {2, "[]"},- [OFIELD] = {2, "."},- [OPTR] = {2, "->"},- [OPOSTINC] = {2, ".++"},- [OPOSTDEC] = {2, ".--"},- [OPREINC] = {2, "++."},- [OPREDEC] = {2, "--."},- [OADDR] = {2, "&."},- [OINDIR] = {2, "[*]"},- [OMINUS] = {2, "-."},- [OPLUS] = {2, "+."},- [OCPL] = {2, "~"},- [ONEG] = {2, "!"},- [OMUL] = {2, "*"},- [ODIV] = {2, "/"},- [OMOD] = {2, "%"},- [OADD] = {2, "+"},- [OSUB] = {2, "-"},- [OSHL] = {2, "<<"},- [OSHR] = {2, ">>"},- [OLT] = {2, "<"},- [OGT] = {2, ">"},- [OGE] = {2, ">="},- [OLE] = {2, "<="},- [OEQ] = {2, "=="},- [ONE] = {2, "!="},- [OBAND] = {2, "&"},- [OBXOR] = {2, "^"},- [OBOR] = {2, "|"},- [OAND] = {2, "&&"},- [OOR] = {2, "||"},- [OTERN] = {2, "?"},- [OASSIGN] = {2, "="},- [OA_MUL] = {2, "*="},- [OA_DIV] = {2, "/="},- [OA_MOD] = {2, "%="},- [OA_ADD] = {2, "+="},- [OA_SUB] = {2, "-="},- [OA_SHL] = {2, "<<="},- [OA_SHR] = {2, ">>="},- [OA_AND] = {2, "&="},- [OA_XOR] = {2, "^="},- [OA_OR] = {2, "|="},- [OSYM] = {0, "sym"},- [OCOMP] = {2, "comp"},- [OSWITCH] = {2, "switch"},- [OIF] = {2, "if"},- [OFOR] = {2, "for"},- [OFEXP] = {2, "efor"},- [ODO] = {2, "do"},- [OWHILE] = {2, "while"},- [OLABEL] = {2, "label"},- [OGOTO] = {2, "goto"},- [OBREAK] = {2, "break"},- [OCONT] = {2, "cont"},- [ORETURN] = {2, "return"},- [OCASE] = {2, "case"},- [ODEFAULT] = {2, "default"},- [OFTN] = {2, "function"},- [ODEF] = {2, "def"},- [O2EXP] = { 2, ":"}+ static unsigned char indent;
+ register unsigned char i;
+ static char *optab[] = {+ [OCALL] = "()",
+ [OARY] = "[]",
+ [OFIELD] = ".",
+ [OPTR] = "->",
+ [OPOSTINC] = ".++",
+ [OPOSTDEC] = ".--",
+ [OPREINC] = "++.",
+ [OPREDEC] = "--.",
+ [OADDR] = "&.",
+ [OINDIR] = "[*]",
+ [OMINUS] = "-.",
+ [OPLUS] = "+.",
+ [OCPL] = "~",
+ [ONEG] = "!",
+ [OMUL] = "*",
+ [ODIV] = "/",
+ [OMOD] = "%",
+ [OADD] = "+",
+ [OSUB] = "-",
+ [OSHL] = "<<",
+ [OSHR] = ">>",
+ [OLT] = "<",
+ [OGT] = ">",
+ [OGE] = ">=",
+ [OLE] = "<=",
+ [OEQ] = "==",
+ [ONE] = "!=",
+ [OBAND] = "&",
+ [OBXOR] = "^",
+ [OBOR] = "|",
+ [OAND] = "&&",
+ [OOR] = "||",
+ [OTERN] = "?",
+ [OASSIGN] = "=",
+ [OA_MUL] = "*=",
+ [OA_DIV] = "/=",
+ [OA_MOD] = "%=",
+ [OA_ADD] = "+=",
+ [OA_SUB] = "-=",
+ [OA_SHL] = "<<=",
+ [OA_SHR] = ">>=",
+ [OA_AND] = "&=",
+ [OA_XOR] = "^=",
+ [OA_OR] = "|=",
+ [OSYM] = "sym",
+ [OCOMP] = "comp",
+ [OSWITCH] = "switch",
+ [OIF] = "if",
+ [OFOR] = "for",
+ [OFEXP] = "efor",
+ [ODO] = "do",
+ [OWHILE] = "while",
+ [OLABEL] = "label",
+ [OGOTO] = "goto",
+ [OBREAK] = "break",
+ [OCONT] = "cont",
+ [ORETURN] = "return",
+ [OCASE] = "case",
+ [ODEFAULT] = "default",
+ [OFTN] = "function",
+ [O2EXP] = ":",
+ [ODEF] = "def"
};
+
if (!np) { fputs(" nil", stdout);return;
}
- assert(np->op < ARRAY_SIZE(optab));
- bp = &optab[np->op];
- if (bp->nchild) {- register unsigned char i;
- putchar('\n');- for (i = indent; i != 0; --i)
- putchar(' ');- printf("(%s", bp->txt);- indent += 2;
- }
- switch (bp->nchild) {- case 0: {- register struct symbol *sym = ((struct nodesym *) np)->sym;
- putchar(' ');- fputs((sym->name) ? sym->name : ".", stdout);
+ if (np->op == OSYM) {+ const char *s = ((struct nodesym *) np)->sym->name;
+
+ printf(" %s", s ? s : ".");return;
}
- case 2:
- prtree_helper(((struct node_op2 *) np)->left);
- prtree_helper(((struct node_op2 *) np)->right);
- break;
- }
+
+ putchar('\n');+ for (i = indent; i != 0; --i)
+ putchar(' ');+
+ indent += 2;
+ assert(np->op < ARRAY_SIZE(optab));
+ printf("(%s", optab[np->op]);+ prtree(((struct node_op2 *)np)->right);
+ prtree(((struct node_op2 *)np)->left);
putchar(')');indent -= 2;
}
-void
-prtree(register struct node *np)
-{- indent = 0;
- prtree_helper(np);
-}
--
⑨