scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 8b5ea303454aa1f5ea386833c337a39ad7ab0996
parent 3c8406c0b92af281c68ffcf7595fed2ac98a095a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue, 18 Mar 2014 23:59:08 +0100

Remove the nodesym function and create union value.

Diffstat:
expr.c | 12++++++------
flow.c | 5++---
symbol.h | 15+++++++++------
syntax.h | 1-
tree.c | 35+++++++----------------------------
5 files changed, 24 insertions(+), 44 deletions(-)

diff --git a/expr.c b/expr.c @@ -18,14 +18,14 @@ primary(void) switch (yytoken) { case IDEN: - if ((sym = lookup(yytext, NS_IDEN)) == NULL) + if (yylval.sym == NULL) error("'%s' undeclared", yytext); + /* TODO: Do something */ next(); - np = nodesym(sym); break; case CONSTANT: next(); - np = nodesym(sym); + /* TODO: do something */ break; case '(': next(); @@ -69,7 +69,7 @@ postfix(void) expect_iden: next(); expect(IDEN); - np2 = nodesym(lookup(yytext, NS_IDEN)); + /* TODO: Do something interesting */ node_2_childs: np1 = node(op, np1, np2); continue; @@ -103,7 +103,7 @@ unary(void) } else { unary(); } - return nodesym(NULL); + return NULL; /* TODO: return something interesting here */ case INC: op = OPREINC; goto call_unary; case DEC: op = OPREDEC; goto call_unary; case '&': op = OADDR; goto call_cast; @@ -256,7 +256,7 @@ bit_xor(void) np = bit_and(); while (yytoken == '^') { next(); - np = node(OBXOR, np, bit_and()); + np = node(OBXOR, np, bit_and()); } return np; } diff --git a/flow.c b/flow.c @@ -41,7 +41,7 @@ Goto(void) expect(GOTO); expect(IDEN); sym = lookup(yytext, NS_LABEL); - np = node(OGOTO, nodesym(sym), NULL); + /* TODO: create the jump */ expect(';'); return np; @@ -137,12 +137,11 @@ Switch(void) static struct node * label(void) { - register struct symbol *sym = lookup(yytext, NS_LABEL); /* TODO: detect repeated labels */ /* TODO: install in symbol table */ next(), next(); /* skip IDEN and ':' */ - return node(OLABEL, nodesym(sym), stmt()); + /* TODO: Do something */ } static struct node * diff --git a/symbol.h b/symbol.h @@ -46,18 +46,21 @@ struct funpar { struct funpar *next; }; +union value { + char c; + int i; + struct symbol *sym; + uint8_t ns; + short offset; +}; + struct symbol { char *name; struct ctype *type; uint8_t ctx; uint8_t token; uint8_t ns; - union { - char c; - int i; - uint8_t ns; - short offset; - } u; + union value u; struct symbol *next; struct symbol *hash; }; diff --git a/syntax.h b/syntax.h @@ -28,7 +28,6 @@ extern struct node *expr(void), *extdecl(void), *decl(void), *typename(void), *function(void); extern struct node *node(unsigned char op, struct node *l, struct node *r); -extern struct node *nodesym(struct symbol *sym); extern bool walk(register struct node *np, bool (*fun)(struct node *)); #endif diff --git a/tree.c b/tree.c @@ -9,50 +9,29 @@ struct node { unsigned char op; -}; - -struct node_op2 { - struct node base; + union value u; + struct ctype *type; struct node *left; struct node *right; }; -struct nodesym { - struct node base; - struct symbol *sym; -}; - - -struct node * -nodesym(struct symbol *sym) -{ - register struct nodesym *np = xmalloc(sizeof(*np)); - - np->base.op = OSYM; - np->sym = sym; - return (struct node *) np; -} - struct node * node(unsigned char op, struct node *l, struct node *r) { - register struct node_op2 *np = xmalloc(sizeof(*np)); + register struct node *np = xmalloc(sizeof(*np)); - np->base.op = op; + np->op = op; np->left = l; np->right = r; - return (struct node *) np; + return np; } bool walk(register struct node *np, bool (*fun)(struct node *)) { - struct node_op2 *p; - - if (!np || np->op == OSYM) + if (!np) return 1; - p = (struct node_op2 *) np; - return (*fun)(np) && walk(p->left, fun) && walk(p->right, fun); + return (*fun)(np) && walk(np->left, fun) && walk(np->right, fun); }