Finishing the preprocessing flag for the lexer, added preprocess.c to test it

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-11-02 18:28:54 +01:00
parent a195e296a9
commit a3791b3f51
4 changed files with 47 additions and 4 deletions

View file

@ -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

16
lexer.c
View file

@ -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);
}

8
main.c
View file

@ -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;

26
preprocess.c Normal file
View file

@ -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;
}