From: Wolfgang (Blub) Bumiller Date: Fri, 2 Nov 2012 17:28:54 +0000 (+0100) Subject: Finishing the preprocessing flag for the lexer, added preprocess.c to test it X-Git-Tag: 0.1~53 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=a3791b3f51ee63cb9f04d608f972830b96c298f7;p=xonotic%2Fgmqcc.git Finishing the preprocessing flag for the lexer, added preprocess.c to test it --- diff --git a/Makefile b/Makefile index a51aed3..660ec84 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ OBJ = \ code.o \ ast.o \ ir.o \ + preprocess.o \ error.o OBJ_A = test/ast-test.o OBJ_I = test/ir-test.o diff --git a/lexer.c b/lexer.c index eb215f4..4d184d0 100644 --- a/lexer.c +++ b/lexer.c @@ -350,6 +350,7 @@ printf( "line one\n" static int lex_skipwhite(lex_file *lex) { int ch = 0; + bool haswhite = false; do { @@ -359,7 +360,7 @@ static int lex_skipwhite(lex_file *lex) if (ch == '\n') { /* end-of-line */ /* see if there was whitespace first */ - if (lex->tok.value_count) { + if (haswhite) { /* (lex->tok.value_count) { */ lex_ungetch(lex, ch); if (!lex_endtoken(lex)) return TOKEN_FATAL; @@ -368,19 +369,19 @@ static int lex_skipwhite(lex_file *lex) /* otherwise return EOL */ return TOKEN_EOL; } + haswhite = true; if (!lex_tokench(lex, ch)) return TOKEN_FATAL; } ch = lex_getch(lex); } - if (lex->flags.preprocessing && !lex_tokench(lex, ch)) - return TOKEN_FATAL; if (ch == '/') { ch = lex_getch(lex); if (ch == '/') { /* one line comment */ + haswhite = true; ch = lex_getch(lex); if (lex->flags.preprocessing) { @@ -407,6 +408,7 @@ static int lex_skipwhite(lex_file *lex) if (ch == '*') { /* multiline comment */ + haswhite = true; if (lex->flags.preprocessing) { if (!lex_tokench(lex, ' ') || !lex_tokench(lex, ' ')) @@ -448,6 +450,12 @@ static int lex_skipwhite(lex_file *lex) } } while (ch != EOF && isspace(ch)); + if (haswhite) { + if (!lex_endtoken(lex)) + return TOKEN_FATAL; + lex_ungetch(lex, ch); + return TOKEN_WHITE; + } return ch; } @@ -675,7 +683,7 @@ int lex_do(lex_file *lex) lex->tok.ctx.line = lex->sline; lex->tok.ctx.file = lex->name; - if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || TOKEN_FATAL)) { + if (lex->flags.preprocessing && (ch == TOKEN_WHITE || ch == TOKEN_EOL || ch == TOKEN_FATAL)) { return (lex->tok.ttype = ch); } diff --git a/main.c b/main.c index a65a735..3fb25ca 100644 --- a/main.c +++ b/main.c @@ -460,6 +460,14 @@ int main(int argc, char **argv) { (items_data[itr].type == TYPE_SRC ? "progs.src" : ("unknown")))))); + if (opts_pp_only) { + if (!preprocess(items_data[itr].filename)) { + retval = 1; + goto cleanup; + } + continue; + } + if (!parser_compile(items_data[itr].filename)) { retval = 1; goto cleanup; diff --git a/preprocess.c b/preprocess.c new file mode 100644 index 0000000..adf5e5e --- /dev/null +++ b/preprocess.c @@ -0,0 +1,26 @@ +#include "gmqcc.h" +#include "lexer.h" + +bool preprocess(const char *filename) +{ + int tok; + lex_file *lex = lex_open(filename); + lex->flags.preprocessing = true; + + do { + tok = lex_do(lex); + if (tok == TOKEN_EOL) + printf("EOL"); + else if (tok >= TOKEN_START && tok <= TOKEN_FATAL) + printf("%s: ", _tokennames[tok - TOKEN_START]); + else + printf("TOKEN: '%c'", tok); + if (tok == TOKEN_WHITE) + printf(">>%s<<\n", lex->tok.value); + else + printf("\n"); + } while (tok < TOKEN_EOF); + + lex_close(lex); + return true; +}