Skip to content

Add C23 bool, true, and false keywords (fixes #250) - #255

Open
MaxwellCalkin wants to merge 1 commit into
anthropics:mainfrom
MaxwellCalkin:fix/c23-bool-true-false-keywords
Open

Add C23 bool, true, and false keywords (fixes #250)#255
MaxwellCalkin wants to merge 1 commit into
anthropics:mainfrom
MaxwellCalkin:fix/c23-bool-true-false-keywords

Conversation

@MaxwellCalkin

@MaxwellCalkin MaxwellCalkin commented Mar 9, 2026

Copy link
Copy Markdown

Summary

Fixes #250 — CCC fails to recognize bool, true, and false in C23 mode.

C23 (N3096 §6.4.1) promotes bool, true, and false from <stdbool.h> macros to first-class keywords. This PR adds support for all three:

  • bool: Added as an alternate spelling for TokenKind::Bool (alongside _Bool), following the same pattern already used for static_assert / _Static_assert
  • true / false: Added as new TokenKind::True and TokenKind::False variants, recognized by from_keyword() and handled in parse_primary_expr() as IntLiteral(1) and IntLiteral(0) respectively

What changed

File Change
src/frontend/lexer/token.rs Added True/False to TokenKind enum, Display impl, and from_keyword() match; added "bool" alternate for _Bool
src/frontend/parser/expressions.rs Handle TokenKind::TrueExpr::IntLiteral(1) and TokenKind::FalseExpr::IntLiteral(0) in primary expression parsing

Compatibility

  • Code using #include <stdbool.h> continues to work unchanged — preprocessor macro expansion occurs before keyword recognition
  • The fast-reject filters in from_keyword() already accept the first characters (b, t, f) and lengths (4, 4, 5), so no filter updates are needed
  • No exhaustive match breakage — all other match statements on TokenKind in the parser use wildcard catch-alls

Test case (from issue #250)

// Should compile with: ccc -std=c23 test.c
bool is_even(int number) {
    return number % 2 == 0;
}

int main(void) {
    bool x = true;
    bool y = false;
    return x && !y ? 0 : 1;
}

🤖 Generated with Claude Code

AI Disclosure

This PR was authored by Claude Opus 4.6 (Anthropic), an AI agent operated by Maxwell Calkin (@MaxwellCalkin).

C23 promotes `bool`, `true`, and `false` from <stdbool.h> macros to
first-class keywords. This change:

- Adds `"bool"` as an alternate spelling for `TokenKind::Bool` (alongside `_Bool`),
  following the same pattern used for `static_assert` / `_Static_assert`
- Adds new `TokenKind::True` and `TokenKind::False` variants
- Maps `"true"` and `"false"` to these new token kinds in `from_keyword()`
- Handles `True`/`False` tokens in the parser's `parse_primary_expr()`,
  emitting `IntLiteral(1)` and `IntLiteral(0)` respectively

Code that includes <stdbool.h> continues to work unchanged since the
preprocessor macro expansion occurs before keyword recognition.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CCC fails to recognize bool, true, and false in C23 mode

1 participant