scc

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

commit 1537ee3c105f0ba568d4a3b40adf4021da40b7c9
parent fe8c715e58c377228207548a27aed474839feca5
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  4 Oct 2015 21:07:40 +0200

Move keyword initialization to more specific places

It is better if each module initializes the own keywords,
instead of having a function that initializes the keywords
for all the modules.

Diffstat:
Mcc1/cc1.h | 6+++++-
Mcc1/cpp.c | 18+++++++++++++++++-
Mcc1/lex.c | 39+++++++++++++++++++++++++++++++++++++++
Mcc1/main.c | 1-
Mcc1/symbol.c | 64----------------------------------------------------------------
5 files changed, 61 insertions(+), 67 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -46,6 +46,10 @@ struct limits { } min; }; +struct keyword { + char *str; + unsigned char token, value; +}; struct type { unsigned char op; /* type builder operator */ @@ -345,9 +349,9 @@ extern Symbol *nextsym(Symbol *sym, int ns); extern Symbol *install(int ns, Symbol *sym); extern Symbol *newsym(int ns); extern void pushctx(void), popctx(void); -extern void ikeywords(void); extern void killsym(Symbol *sym); extern Symbol *newlabel(void); +extern void keywords(struct keyword *key, int ns); /* stmt.c */ extern void compound(Symbol *lbreak, Symbol *lcont, Caselist *lswitch); diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -37,12 +37,27 @@ icpp(void) static char sdate[17], stime[14]; struct tm *tm; time_t t; - char **bp, *list[] = { + static char **bp, *list[] = { "__STDC__", "__STDC_HOSTED__", "__SCC__", NULL }; + static struct keyword keys[] = { + {"define", DEFINE, DEFINE}, + {"include", INCLUDE, INCLUDE}, + {"line", LINE, LINE}, + {"ifdef", IFDEF, IFDEF}, + {"if", IF, IF}, + {"elif", ELIF, ELIF}, + {"else", ELSE, ELSE}, + {"ifndef", IFNDEF, IFNDEF}, + {"endif", ENDIF, ENDIF}, + {"undef", UNDEF, UNDEF}, + {"pragma", PRAGMA, PRAGMA}, + {"error", ERROR, ERROR}, + {NULL, 0, 0} + }; t = time(NULL); tm = localtime(&t); @@ -57,6 +72,7 @@ icpp(void) for (bp = list; *bp; ++bp) defmacro(*bp)->u.s = "-1#1"; + keywords(keys, NS_CPPCLAUSES); } static void diff --git a/cc1/lex.c b/cc1/lex.c @@ -39,6 +39,44 @@ allocinput(char *fname, FILE *fp) void ilex(char *fname) { + static struct keyword keys[] = { + {"auto", SCLASS, AUTO}, + {"break", BREAK, BREAK}, + {"_Bool", TYPE, BOOL}, + {"case", CASE, CASE}, + {"char", TYPE, CHAR}, + {"const", TQUALIFIER, CONST}, + {"continue", CONTINUE, CONTINUE}, + {"default", DEFAULT, DEFAULT}, + {"do", DO, DO}, + {"double", TYPE, DOUBLE}, + {"else", ELSE, ELSE}, + {"enum", TYPE, ENUM}, + {"extern", SCLASS, EXTERN}, + {"float", TYPE, FLOAT}, + {"for", FOR, FOR}, + {"goto", GOTO, GOTO}, + {"if", IF, IF}, + {"inline", TQUALIFIER, INLINE}, + {"int", TYPE, INT}, + {"long", TYPE, LONG}, + {"register", SCLASS, REGISTER}, + {"restrict", TQUALIFIER, RESTRICT}, + {"return", RETURN, RETURN}, + {"short", TYPE, SHORT}, + {"signed", TYPE, SIGNED}, + {"sizeof", SIZEOF, SIZEOF}, + {"static", SCLASS, STATIC}, + {"struct", TYPE, STRUCT}, + {"switch", SWITCH, SWITCH}, + {"typedef", SCLASS, TYPEDEF}, + {"union", TYPE, UNION}, + {"unsigned", TYPE, UNSIGNED}, + {"void", TYPE, VOID}, + {"volatile", TQUALIFIER, VOLATILE}, + {"while", WHILE, WHILE}, + {NULL, 0, 0}, + }; FILE *fp; if (!fname) { @@ -52,6 +90,7 @@ ilex(char *fname) } allocinput(fname, fp); *input->begin = '\0'; + keywords(keys, NS_KEYWORD); } bool diff --git a/cc1/main.c b/cc1/main.c @@ -75,7 +75,6 @@ main(int argc, char *argv[]) usage(); icpp(); - ikeywords(); ilex(*argv); if (onlycpp) { diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -282,11 +282,6 @@ install(int ns, Symbol *sym) return linkhash(sym); } -struct keyword { - char *str; - unsigned char token, value; -}; - void keywords(struct keyword *key, int ns) { @@ -305,62 +300,3 @@ keywords(struct keyword *key, int ns) counterid = 0; head = NULL; } - -void -ikeywords(void) -{ - static struct keyword ckeywords[] = { - {"auto", SCLASS, AUTO}, - {"break", BREAK, BREAK}, - {"_Bool", TYPE, BOOL}, - {"case", CASE, CASE}, - {"char", TYPE, CHAR}, - {"const", TQUALIFIER, CONST}, - {"continue", CONTINUE, CONTINUE}, - {"default", DEFAULT, DEFAULT}, - {"do", DO, DO}, - {"double", TYPE, DOUBLE}, - {"else", ELSE, ELSE}, - {"enum", TYPE, ENUM}, - {"extern", SCLASS, EXTERN}, - {"float", TYPE, FLOAT}, - {"for", FOR, FOR}, - {"goto", GOTO, GOTO}, - {"if", IF, IF}, - {"inline", TQUALIFIER, INLINE}, - {"int", TYPE, INT}, - {"long", TYPE, LONG}, - {"register", SCLASS, REGISTER}, - {"restrict", TQUALIFIER, RESTRICT}, - {"return", RETURN, RETURN}, - {"short", TYPE, SHORT}, - {"signed", TYPE, SIGNED}, - {"sizeof", SIZEOF, SIZEOF}, - {"static", SCLASS, STATIC}, - {"struct", TYPE, STRUCT}, - {"switch", SWITCH, SWITCH}, - {"typedef", SCLASS, TYPEDEF}, - {"union", TYPE, UNION}, - {"unsigned", TYPE, UNSIGNED}, - {"void", TYPE, VOID}, - {"volatile", TQUALIFIER, VOLATILE}, - {"while", WHILE, WHILE}, - {NULL, 0, 0}, - }, cppclauses[] = { - {"define", DEFINE, DEFINE}, - {"include", INCLUDE, INCLUDE}, - {"line", LINE, LINE}, - {"ifdef", IFDEF, IFDEF}, - {"if", IF, IF}, - {"elif", ELIF, ELIF}, - {"else", ELSE, ELSE}, - {"ifndef", IFNDEF, IFNDEF}, - {"endif", ENDIF, ENDIF}, - {"undef", UNDEF, UNDEF}, - {"pragma", PRAGMA, PRAGMA}, - {"error", ERROR, ERROR}, - {NULL, 0, 0} - }; - keywords(ckeywords, NS_KEYWORD); - keywords(cppclauses, NS_CPPCLAUSES); -}