From 1c48d0fe728c263bbe946a5d3adebaa29323ddff Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 21 Jul 2025 18:39:48 +0900 Subject: feat: partially implement #ifdef, #else and #endif directives --- preprocess.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'preprocess.c') diff --git a/preprocess.c b/preprocess.c index a8b3b91..205e2bd 100644 --- a/preprocess.c +++ b/preprocess.c @@ -37,6 +37,7 @@ struct Preprocessor { int n_pp_tokens; PpDefines* pp_defines; int include_depth; + int skip_pp_tokens; }; typedef struct Preprocessor Preprocessor; @@ -83,6 +84,11 @@ int find_pp_define(Preprocessor* pp, String* name) { return -1; } +int skip_pp_tokens(Preprocessor* pp) { + // TODO: support nested #if + return pp->skip_pp_tokens; +} + void pp_tokenize_all(Preprocessor* pp) { char* buf; int ch; @@ -416,7 +422,49 @@ void process_pp_directives(Preprocessor* pp) { pp->n_pp_tokens += n_include_pp_tokens; } + } else if (tok2->kind == PpTokenKind_identifier && string_equals_cstr(&tok2->raw, "ifdef")) { + ++tok2; + while (tok2->kind != PpTokenKind_eof && tok2->kind == PpTokenKind_whitespace) + ++tok2; + if (tok2->kind == PpTokenKind_identifier) { + // Process #ifdef directive. + PpToken* name = tok2; + ++tok2; + + pp->skip_pp_tokens = find_pp_define(pp, &name->raw) == -1; + } + // Remove #ifdef directive. + while (tok != tok2 + 1) { + tok->kind = PpTokenKind_whitespace; + tok->raw.len = 0; + tok->raw.data = NULL; + ++tok; + } + } else if (tok2->kind == PpTokenKind_identifier && string_equals_cstr(&tok2->raw, "endif")) { + ++tok2; + pp->skip_pp_tokens = 0; + // Remove #endif directive. + while (tok != tok2 + 1) { + tok->kind = PpTokenKind_whitespace; + tok->raw.len = 0; + tok->raw.data = NULL; + ++tok; + } + } else if (tok2->kind == PpTokenKind_identifier && string_equals_cstr(&tok2->raw, "else")) { + ++tok2; + pp->skip_pp_tokens = 1 - pp->skip_pp_tokens; + // Remove #else directive. + while (tok != tok2 + 1) { + tok->kind = PpTokenKind_whitespace; + tok->raw.len = 0; + tok->raw.data = NULL; + ++tok; + } } + } else if (skip_pp_tokens(pp)) { + tok->kind = PpTokenKind_whitespace; + tok->raw.len = 0; + tok->raw.data = NULL; } else if (tok->kind == PpTokenKind_identifier) { int pp_define_idx = find_pp_define(pp, &tok->raw); if (pp_define_idx != -1) { -- cgit v1.2.3-70-g09d2