scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 9ec827fe1ceba3de3209e235fcb8a65382baf4bb
parent ab7033ad9cf899c83e4911881cb11026103e7fd4
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 28 Mar 2014 17:00:27 +0100

Remove all the references to struct node

I do not like how the code was begining to seem, so I am going to try
another thing.

Diffstat:
Mexpr.c | 134++++++-------------------------------------------------------------------------
Mstmt.c | 4++--
2 files changed, 12 insertions(+), 126 deletions(-)

diff --git a/expr.c b/expr.c @@ -6,34 +6,7 @@ #include "tokens.h" #include "symbol.h" -struct node *expr(void); - -enum { - OSYM = 1, OARY, OPTR, OADD, -}; - -struct node { - uint8_t op; - struct ctype *type; - - struct { - bool constant : 1; - } f; - union { - struct symbol *sym; - } u; - struct node *left, *right; -}; - -struct node * -newnode(uint8_t op, struct ctype *tp) -{ - struct node *np = xcalloc(1, sizeof(*np)); - - np->op = op; - np->type = tp; - return np; -} +void expr(void); static struct node * primary(void) @@ -44,8 +17,6 @@ primary(void) case IDEN: if (yylval.sym == NULL) error("'%s' undeclared", yytext); - np = newnode(OSYM, yylval.sym->type); - np->u.sym = yylval.sym; next(); break; case CONSTANT: @@ -54,7 +25,7 @@ primary(void) break; case '(': next(); - np = expr(); + expr(); expect(')'); break; default: @@ -63,112 +34,28 @@ primary(void) return np; } -static struct node * -int2ptr(struct node *np) -{ -} -static struct node * -ptr2vec(struct node *np) -{ - struct ctype *tp = np->type; - struct node *p; - - tp = mktype(UNQUAL(tp)->type, ARY, NULL, 0); - p = newnode(OPTR, tp); - p->left = np; - return p; -} - -static struct node * -ary(struct node *np1) +static void +ary(void) { - struct node *np2, *naux; - struct ctype *tp; - uint8_t t1, t2, taux; - - /* should be for arrays: A2 A1 RI #1 *R ` */ - /* should be for pointers: A2 @ A1 RI #1 *R ` */ - np2 = expr(); - expect(']'); - t1 = BTYPE(np1->type); - t2 = BTYPE(np2->type); - - if (!isaddr(t1)) { - taux = t1, t1 = t2, t2 = taux; - naux = np1, np1 = np2, np2 = naux; - } - if (!isaddr(t1)) - error("expected array or pointer"); - if (isptr(t1)) - np1 = ptr2vec(np1); - if (!isarith(t2)) - error("array subscript is not an integer"); - - tp = np1->type; - tp = UNQUAL(tp); - naux = newnode(OADD, tp); - naux->left = np1; - naux->right = int2ptr(np2); - return naux; } -static struct node * +static void postfix(void) { - struct node *np; - - np = primary(); + primary(); for (;;) { switch (yytoken) { - case '[': next(); np = ary(np); break; - default: return np; + case '[': next(); ary(); break; + default: return; } } } -struct node * +void expr(void) { - register struct node *np; - do - np = postfix(); + postfix(); while (yytoken == ','); - - return np; } - -static void -evalnode(struct node *np) -{ - if (!np) - return; - - switch (np->op) { - case OSYM: - emitsym(np->u.sym); - break; - case OARY: - evalnode(np->left); - evalnode(np->right); - fputs("\t'", stdout); - break; - case OPTR: - evalnode(np->left); - fputs("\t@", stdout); - break; - case OADD: - evalnode(np->left); - evalnode(np->right); - fputs("\t+", stdout); - break; - } -} - -void -eval(struct node *np) -{ - evalnode(np); - putchar('\n'); -} -\ No newline at end of file diff --git a/stmt.c b/stmt.c @@ -8,7 +8,6 @@ void compound(void) { - extern struct node *expr(void); extern void decl(void); expect('{'); @@ -18,7 +17,8 @@ compound(void) decl(); break; default: - eval(expr()); + expr(); + /* TODO: Evaluate the expression here */ } expect(';'); }