scc

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

commit 39b7bd0fe07005f621890f2a7601d3e38d367e7a
parent 321ff05de46a7eaf967e73364dd02fa70c08518a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 12 Feb 2012 11:57:15 +0100

Added insert into table symbol

When a declaration finds a ',' or a ';' we can insert it into the symbol
table, because we recognize the input. We use an alloca instead of a
malloc due to the block form of programs.

Diffstat:
decl.c | 26++++++++++++++++++--------
lex.c | 1-
symbol.c | 25++++++++++++++++---------
symbol.h | 2+-
tokens.h | 4++++
5 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/decl.c b/decl.c @@ -1,17 +1,21 @@ +#include <alloca.h> #include <assert.h> #include <stddef.h> +#include <string.h> #include "cc.h" #include "tokens.h" +#include "symbol.h" #include "types.h" + /* ANSI C says minimum maximum for indirection level is 12 */ #define PTRLEVEL_MAX 12 char parser_out_home; - - +static unsigned char symhash; +static char symname[TOKSIZ_MAX + 1]; static unsigned char stack[30]; static unsigned char *stackp = stack; @@ -36,7 +40,10 @@ void dirdcl(void) gettok(); } else if (yytoken == IDENTIFIER) { gettok(); - /* here we are!!! */; + strcpy(symname, yytext); + symhash = yyhash; + } else { + error("expected '(' or identifier before of '%s'", yytext); } for (;;) { @@ -229,23 +236,26 @@ duplicated: void declaration(void) { struct type *t; + struct symbol *sym; +repeat: t = specifier(); for (; ; gettok()) { + /* TODO: put here savepoint for error recovering */ decl(); if (yytoken != ',' && yytoken != ';') error("unexpected", yytext); while (!empty()) t = mktype(t, pop()); ptype(t); + sym = alloca(sizeof(*sym)); + pushsym(siden, sym, symhash); if (yytoken == ',') - /* add variable */; - else if (yytoken == ';') { - /* end of sentence */; - return; - } + continue; + else if (yytoken == ';') + goto repeat; } } diff --git a/lex.c b/lex.c @@ -8,7 +8,6 @@ #include "tokens.h" -#define TOKSIZ_MAX 21 #define NR_KWD_HASH 32 static struct keyword { diff --git a/symbol.c b/symbol.c @@ -23,7 +23,10 @@ struct symctx { static struct symctx global_ctx; static struct symctx *ctxp = &global_ctx; -struct symhash siden, sgoto, sstruct; +struct symhash *siden = (struct symhash *) {0}; +struct symhash *sgoto = (struct symhash *) {0}; + +struct symhash *sstruct = (struct symhash *) {0}; @@ -31,9 +34,9 @@ struct symhash siden, sgoto, sstruct; void new_ctx(struct symctx *ctx) { - ctx->siden = siden.top; - ctx->sstruct = sstruct.top; - ctx->sgoto = sgoto.top; + ctx->siden = siden->top; + ctx->sstruct = sstruct->top; + ctx->sgoto = sgoto->top; ctx->next = ctxp; ctxp = ctx; } @@ -51,24 +54,27 @@ static void del_hash_ctx(struct symhash *h, struct symbol *const top) lim = h->buf + NR_SYM_HASH; for (bp = h->buf; bp < lim; bp++) { register struct symbol *aux; - for (aux = *bp; aux < top; *bp = aux = aux->next) + for (aux = *bp; aux < top; *bp = aux = aux->next) { if (aux == h->top) h->top = aux; + } } } void del_ctx(void) { - del_hash_ctx(&siden, ctxp->siden); - del_hash_ctx(&sstruct, ctxp->sstruct); - del_hash_ctx(&sgoto, ctxp->sgoto); /* TODO: correct handling in goto */ + del_hash_ctx(siden, ctxp->siden); + del_hash_ctx(sstruct, ctxp->sstruct); + del_hash_ctx(sgoto, ctxp->sgoto); /* TODO: correct handling in goto */ } -struct symbol *pushsym(struct symhash *h, struct symbol *sym, unsigned char hash) +struct symbol *pushsym(struct symhash *h, + struct symbol *sym, unsigned char hash) + { static unsigned char key; @@ -91,3 +97,4 @@ struct symbol *findsym(struct symhash *h, char *s, unsigned char hash) } return NULL; } + diff --git a/symbol.h b/symbol.h @@ -14,7 +14,7 @@ struct symbol { struct symhash; -extern struct symhash siden, sgoto, sstruct; +extern struct symhash *siden, *sgoto, *sstruct; #endif diff --git a/tokens.h b/tokens.h @@ -1,6 +1,10 @@ #ifndef TOKENS_H #define TOKENS_H + + +#define TOKSIZ_MAX 21 + /* Don't change this codification because program used it!!! */ enum { /* types */