scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 7525bdb75be193b54dce97849fe81e5df378584e
parent 0c7104c6a979801a9ad3c164dce3812974dc1601
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed, 18 Jan 2017 11:16:06 +0100

[cc1] Add type field to input structure

The type is implicit in several of the other fields
of the sructure but it is generating several problems,
because guessing continuosly the type is not easy always.
The number of Input structures is going to be small, so
the space wasted is totally insignificant and the
quality of the code can be improved a lot.

Diffstat:
Mcc1/cc1.h | 11++++++++++-
Mcc1/lex.c | 15+++++++++++----
2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -23,6 +23,14 @@ enum typeprops { TK_R = 1 << 5, /* this is a K&R-function */ }; +enum inputtype { + IMACRO = 1 << 0, /* macro expansion type */ + IFILE = 1 << 1, /* input file type */ + ISTDIN = 1 << 2, /* stdin type */ + IEOF = 1 << 3, /* EOF mark */ + ITYPE = IMACRO | IFILE | ISTDIN, +}; + /* data type letters */ enum ns { L_INT8 = 'C', @@ -343,12 +351,13 @@ struct yystype { }; struct input { + char flags; + unsigned short nline; char *fname; FILE *fp; Symbol *hide; char *line, *begin, *p; struct input *next; - unsigned short nline; }; /* error.c */ diff --git a/cc1/lex.c b/cc1/lex.c @@ -72,7 +72,7 @@ int addinput(char *fname, Symbol *hide, char *buffer) { FILE *fp; - unsigned nline = 0; + unsigned type, nline = 0; Input *ip; if (hide) { @@ -83,14 +83,17 @@ addinput(char *fname, Symbol *hide, char *buffer) if (hide->hide == UCHAR_MAX) die("Too many macro expansions"); ++hide->hide; + type = IMACRO; } else if (fname) { /* a new file */ if ((fp = fopen(fname, "r")) == NULL) return 0; + type = IFILE; } else { /* reading from stdin */ fp = stdin; fname = "<stdin>"; + type = ISTDIN; } ip = xmalloc(sizeof(*ip)); @@ -106,6 +109,7 @@ addinput(char *fname, Symbol *hide, char *buffer) ip->fp = fp; ip->hide = hide; ip->nline = nline; + ip->flags = type; input = ip; return 1; @@ -117,23 +121,26 @@ delinput(void) Input *ip = input; Symbol *hide = ip->hide; - if (ip->fp) { + switch (ip->flags & ITYPE) { + case IFILE: if (fclose(ip->fp)) die("error: failed to read from input file '%s'", ip->fname); if (!ip->next) eof = 1; - } - if (hide) { + break; + case IMACRO: --hide->hide; /* * If the symbol is not declared then it was * an expansion due to a #if directive with * a non declared symbol (expanded to 0), * thus we have to kill the symbol + * TODO: review this comment and code */ if ((hide->flags & SDECLARED) == 0) killsym(hide); + break; } if (eof) return;