ref: 8b5ea303454aa1f5ea386833c337a39ad7ab0996
parent: 3c8406c0b92af281c68ffcf7595fed2ac98a095a
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Mar 18 19:59:08 EDT 2014
Remove the nodesym function and create union value.
--- a/expr.c
+++ b/expr.c
@@ -18,14 +18,14 @@
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 @@
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 @@
} 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 @@
np = bit_and();
while (yytoken == '^') {
next();
- np = node(OBXOR, np, bit_and());
+ np = node(OBXOR, np, bit_and());
}
return np;
}
--- a/flow.c
+++ b/flow.c
@@ -41,7 +41,7 @@
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 @@
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 *
--- a/symbol.h
+++ b/symbol.h
@@ -46,6 +46,14 @@
struct funpar *next;
};
+union value {
+ char c;
+ int i;
+ struct symbol *sym;
+ uint8_t ns;
+ short offset;
+};
+
struct symbol {
char *name;
struct ctype *type;
@@ -52,12 +60,7 @@
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;
};
--- a/syntax.h
+++ b/syntax.h
@@ -28,7 +28,6 @@
*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
--- 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);
}