ref: 39b7bd0fe07005f621890f2a7601d3e38d367e7a
parent: 321ff05de46a7eaf967e73364dd02fa70c08518a
author: Roberto E. Vargas Caballero <[email protected]>
date: Sun Feb 12 06:57:15 EST 2012
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.
--- 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 @@
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,10 +236,13 @@
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);
@@ -239,13 +249,13 @@
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;
}
}
--- 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 {
--- a/symbol.c
+++ b/symbol.c
@@ -23,17 +23,20 @@
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};
+
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,9 +54,10 @@
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;
+ }
}
}
@@ -60,15 +64,17 @@
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 @@
}
return NULL;
}
+
--- a/symbol.h
+++ b/symbol.h
@@ -14,7 +14,7 @@
struct symhash;
-extern struct symhash siden, sgoto, sstruct;
+extern struct symhash *siden, *sgoto, *sstruct;
#endif
--- 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 */