Parenthesise a negative literal under a unary minus - #92
Merged
Merged
Conversation
Negating -1 was written (--1), which C, C++, C#, JavaScript and Go read as a decrement of a literal and refuse to compile. GenerateUnaryExpression now wraps an operand whose spelling starts with the operator's own last character, so it comes out (-(-1)). Fixes #82 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NtawxkUssqE3PHcqRCW1Ag
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #82
What changed
GenerateUnaryExpressionwrote the operator and the operand with nothing between them. Negating-1therefore came out as(--1). C, C++, C#, JavaScript and Go read that as a decrement of a literal, so it doesn't compile.The operand is now parenthesised when its spelling starts with the operator's last character, so the same expression comes out as
(-(-1)). A newLeadingSignhelper handles the check. Only a negativeintordoubleliteral (or a legacyAstLeafNode<int>) can start with a sign. Every other expression starts with a name, a quote, a digit or a parenthesis, so nothing else changes.-x,+-1and-1are still written bare.Tests
Coder.Test/Languages/UnaryOperandSignTests.csruns all seven generators:Negate(Literal.Number(-1))is written as(-(-1)).Negate(Literal.DecimalValue(-1.5))contains no--.With the fix reverted, both negation tests fail. The full suite passes: 917/917.
🤖 Generated with Claude Code
https://claude.ai/code/session_01NtawxkUssqE3PHcqRCW1Ag
Generated by Claude Code