diff --git a/chibicc.h b/chibicc.h index 1bcd350011a3235fda2851e30471f5b2b2947a7a..17494b80b5d4f0f705dc976cbe155558667b90ce 100644 --- a/chibicc.h +++ b/chibicc.h @@ -69,11 +69,18 @@ void warn_tok(Token *tok, char *fmt, ...); bool equal(Token *tok, char *op); Token *skip(Token *tok, char *op); bool consume(Token **rest, Token *tok, char *str); +void convert_keywords(Token *tok); Token *tokenize_file(char *filename); #define unreachable() \ error("internal error at %s:%d", __FILE__, __LINE__) +// +// preprocess.c +// + +Token *preprocess(Token *tok); + // // parse.c // diff --git a/main.c b/main.c index fc526461c98d378a09b0911d20d73132aa9e9cfd..d04556b7aa94cce9f7bf26674e3a70359ac68058 100644 --- a/main.c +++ b/main.c @@ -183,6 +183,7 @@ static void run_cc1(int argc, char **argv, char *input, char *output) { static void cc1(void) { // Tokenize and parse. Token *tok = tokenize_file(base_file); + tok = preprocess(tok); Var *prog = parse(tok); // Traverse the AST to emit assembly. diff --git a/preprocess.c b/preprocess.c new file mode 100644 index 0000000000000000000000000000000000000000..e26e4d9e9fc0b7553c239613108fd3be88f9799a --- /dev/null +++ b/preprocess.c @@ -0,0 +1,7 @@ +#include "chibicc.h" + +// Entry point function of the preprocessor. +Token *preprocess(Token *tok) { + convert_keywords(tok); + return tok; +} diff --git a/tokenize.c b/tokenize.c index 70bcbd681df7cec2134ce1c7ddf23cc1184838c0..81c6f7d1773c4a6482e615191eb1df9921045e08 100644 --- a/tokenize.c +++ b/tokenize.c @@ -332,7 +332,7 @@ static Token *read_number(Token *cur, char *start) { return tok; } -static void convert_keywords(Token *tok) { +void convert_keywords(Token *tok) { for (Token *t = tok; t->kind != TK_EOF; t = t->next) if (is_keyword(t)) t->kind = TK_RESERVED; @@ -449,7 +449,6 @@ static Token *tokenize(char *filename, char *p) { new_token(TK_EOF, cur, p, 0); add_line_numbers(head.next); - convert_keywords(head.next); return head.next; }