scc

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

commit 06276ba4738710b02df4c81803655ea777ed52c9
parent 432127aad4a212d5d02dd74d9c7340ef68a85804
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 23 May 2015 20:36:55 +0200

Add #else clause

This implementation needs more testing, and it need a bit of
rewriting, but at this point we have the preprocessor finished.

Diffstat:
Mcc1/cpp.c | 32+++++++++++++++++++++++++++-----
1 file changed, 27 insertions(+), 5 deletions(-)

diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -418,20 +418,19 @@ ifclause(char *s, int isdef) memcpy(yytext, s, len); yytext[len] = '\0'; cleanup(endp); - curif = numif++; + ++numif; - if (iffalse != 0) { + if (iffalse == 0) { sym = lookup(NS_CPP); if ((sym->flags & ISDEFINED) != 0 == isdef) return 1; } - ++iffalse; - while (curif != numif) { + curif = iffalse++; + while (curif != iffalse) { if (!moreinput()) error("found EOF while ..."); } - --iffalse; return 1; } @@ -451,12 +450,34 @@ ifndef(char *s) static bool endif(char *s) { + cleanup(s); if (numif == 0) error("#endif without #if"); --numif; return iffalse == 0; } +static bool +elseclause(char *s) +{ + unsigned curif; + + cleanup(s); + if (numif == 0) + error("#else without #if"); + + if (iffalse == 0) { + curif = iffalse++; + while (curif != iffalse) { + if (!moreinput()) + error("found EOF while ..."); + } + } + --iffalse; + + return iffalse != 0; +} + bool preprocessor(char *p) { @@ -471,6 +492,7 @@ preprocessor(char *p) "ifdef", ifdef, "ifndef", ifndef, "endif", endif, + "else", elseclause, "line", line, "pragma", pragma, "error", usererr,