Skip to content

Add numeric literal support to shader preprocessor#8402

Merged
mvaligursky merged 1 commit into
mainfrom
mv-preprocessor-numeric-literals
Jan 23, 2026
Merged

Add numeric literal support to shader preprocessor#8402
mvaligursky merged 1 commit into
mainfrom
mv-preprocessor-numeric-literals

Conversation

@mvaligursky

@mvaligursky mvaligursky commented Jan 23, 2026

Copy link
Copy Markdown
Contributor

Description

Adds support for numeric literals in #if preprocessor expressions, following standard C preprocessor behavior.

Changes

  • #if 0 now evaluates to false (block is excluded)
  • #if 1 and any non-zero numeric literal evaluates to true (block is included)
  • Works with floating point values (#if 0.0 is falsy)
  • Combines correctly with other expressions (#if 1 && defined(FOO))

Why

This is standard C preprocessor behavior that developers expect to work. Previously, #if 1 would incorrectly evaluate to false because the preprocessor checked if '1' was a defined symbol rather than treating it as a numeric literal.

Test Plan

  • Added 6 unit tests covering numeric literals (positive integers, zero, non-zero, floats, combined expressions)
  • All 49 preprocessor tests pass

@mvaligursky mvaligursky self-assigned this Jan 23, 2026
@mvaligursky mvaligursky requested review from a team and Copilot January 23, 2026 11:21
@mvaligursky mvaligursky merged commit a13e14e into main Jan 23, 2026
11 checks passed
@mvaligursky mvaligursky deleted the mv-preprocessor-numeric-literals branch January 23, 2026 11:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds support for numeric literals in #if preprocessor expressions, bringing the shader preprocessor closer to standard C preprocessor behavior. Previously, #if 1 would incorrectly be treated as an undefined symbol check rather than a numeric literal.

Changes:

  • Added numeric literal evaluation in evaluateAtomicExpression() using parseFloat()
  • Numeric literals are evaluated as truthy if non-zero, falsy if zero
  • Added 6 comprehensive test cases covering various numeric literal scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/core/preprocessor.js Adds numeric literal handling to evaluateAtomicExpression() using parseFloat, checking after boolean literals but before defined() and comparison checks
test/core/preprocessor.test.mjs Adds 6 test cases for numeric literals including integers, zero, floats, and combinations with logical operators

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/core/preprocessor.js
Comment on lines +541 to +545
// Handle numeric literals (0 is false, non-zero is true) - standard C preprocessor behavior
const num = parseFloat(expr);
if (!isNaN(num)) {
return { result: num !== 0, error };
}

Copilot AI Jan 23, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The numeric literal check using parseFloat(expr) can produce incorrect results when the expression contains comparison operators with numeric literals on the left side. For example, #if 3 == 3 would be parsed as parseFloat("3 == 3") which returns 3 (parseFloat stops at the first non-numeric character), causing the expression to evaluate to true while ignoring the comparison operator entirely.

The COMPARISON regex requires the left operand to start with a letter or underscore, so it won't match numeric literals. This means numeric comparisons like #if 3 == 3 or #if 0 != 1 won't be caught by the comparison handler and will incorrectly fall through to the numeric literal handler.

Consider checking if the expression contains comparison operators before attempting to parse it as a numeric literal, or modify the COMPARISON regex to also match numeric literals on the left side.

Copilot uses AI. Check for mistakes.
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.

2 participants