Repository navigation
Truncate integer /= and %= in Python and JavaScript the way the C family does [patch] - #214
Merged
Merged
Conversation
…ily does [patch] #114 lowered integer / and % in BinaryExpression, but compound assignment takes its own path and still wrote x /= y, which is true division in Python and JavaScript, and x %= y, which floors in Python. When both sides are known to be integers, Python and JavaScript now write the assignment out as x = <lowered binary>, reusing GenerateBinary. Fixes #183 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw
|
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 #183
What changed
#114 lowered integer
/and%inBinaryExpression. Compound assignment goes throughGenerateAssignmentStatementinstead, which still wrotex /= yandx %= yverbatim:a = 5; a /= 2a = -7; a %= 22-12.5(a float)12.5-1PythonGeneratoroverridesGenerateAssignmentStatement. When the operator isDivideAssignorModuloAssignandIsIntegerArithmeticholds for target and value, it writesx = <lowered binary>through the existingGenerateBinary. That reuses Integer division and modulo give different results in Python and JavaScript:-7 / 2returns -3 in C/C++/C#/Go/Rust but -3.5 in Python and JS #114's integer-only truncation, including its lambda binding for an operand that must not be evaluated twice.JavaScriptGeneratordoes the same forDivideAssignonly, givingx = Math.trunc((x / y)). JavaScript's%=already takes the dividend's sign.StandardLanguageGenerator, so they don't overlap with Take a floating-point remainder with fmod or math.Mod in C, C++ and Go [patch] #210, which also overridesGenerateAssignmentStatementthere. A test merge with Take a floating-point remainder with fmod or math.Mod in C, C++ and Go [patch] #210's head is clean.Tests (
CompoundIntegerDivisionTests)Python_MatchesTheCFamilyandJavaScript_MatchesTheCFamilyrun the generated code. They cover/=and%=on a parameter, a typed local, a literal divisor, and a divisor that is a call, across seven operand pairs including every sign combination. Each result must equal the C# result, Python's must be anint, and the call must be made exactly once.OtherCompoundAssignments_AreUnchanged(Python, JavaScript):/=on unknown ordoubleoperands and+=keep their spelling.With the generator changes reverted, both run tests fail (
div_params(-7, 2) = -3.5, expected -3). With the changes, the full suite passes (1157/1157).🤖 Generated with Claude Code
https://claude.ai/code/session_01P8rVj8U65vKDPQv2LuKHEw
Generated by Claude Code