Fix duplicate condition bug in transpiler for synthesized operators#2456
Conversation
When code uses both direct (<, >, ==) and synthesized (>=, <=, !=)
comparison operators with the same operands, the transpiler was creating
duplicate conditions instead of reusing existing ones.
Example:
if (x < 6) { ... } // Creates condition 0: x < 6
if (x >= 6) { ... } // Was creating duplicate condition 2: x < 6
// Should reuse condition 0
Root cause:
The condition cache had separate namespaces for 'binary' (direct ops)
and 'binary_synth' (synthesized ops). When generating >= as NOT(x < 6),
the code didn't check if 'x < 6' already existed in the binary cache.
Fix:
In generateBinary(), when processing synthesized operators (>=, <=, !=),
check if the inverse comparison already exists in the cache before
generating a new one. If found, reuse the existing LC index.
Changes:
- js/transpiler/transpiler/condition_generator.js: Added cache lookup
for inverse comparison before generating new condition
- Added comprehensive tests for duplicate detection
Testing:
- test_not_precedence.js: Verifies >= reuses existing <
- test_duplicate_detection_comprehensive.js: Tests >=, <=, != reuse
- All tests pass
Impact: Reduces wasted logic condition slots, prevents FC slot exhaustion
PR Compliance Guide 🔍Below is a summary of compliance checks for this PR:
Compliance status legend🟢 - Fully Compliant🟡 - Partial Compliant 🔴 - Not Compliant ⚪ - Requires Further Human Verification 🏷️ - Compliance label |
||||||||||||||||||||||||
PR Code Suggestions ✨Explore these optional code suggestions:
|
||||||||||||
User description
Summary
Fixes a bug where the JavaScript Programming tab transpiler was generating duplicate logic conditions when code used both direct comparison operators (
<,>,==) and their synthesized counterparts (>=,<=,!=) with the same operands.Problem
When user writes:
Expected behavior:
gpsSats < 6NOT(LC 0)- reuses condition 0Buggy behavior:
gpsSats < 6gpsSats < 6(DUPLICATE)NOT(LC 2)Root Cause
The condition cache maintained separate namespaces for direct operations (
binary) and synthesized operations (binary_synth). When generating>=asNOT(x < 6), the code didn't check ifx < 6already existed in thebinarycache before creating a new condition.Solution
Modified
generateBinary()incondition_generator.jsto check if the inverse comparison already exists in the cache before generating a new one. If found, reuse the existing LC index.Changes
Testing
All tests pass:
Impact
Files Changed
js/transpiler/transpiler/condition_generator.js(17 insertions, 2 deletions)js/transpiler/transpiler/tests/test_not_precedence.js(132 lines, new file)js/transpiler/transpiler/tests/test_duplicate_detection_comprehensive.js(125 lines, new file)PR Type
Bug fix, Tests
Description
Fixes duplicate condition generation when synthesized operators reuse inverse comparisons
Added cache lookup for inverse comparisons in
generateBinary()methodPrevents wasting logic condition slots on duplicate conditions
Added comprehensive tests for >=, <=, != operator reuse scenarios
Diagram Walkthrough
flowchart LR A["User Code:<br/>x < 6 and x >= 6"] --> B["Transpiler:<br/>Check Cache"] B --> C{"Inverse<br/>Exists?"} C -->|Yes| D["Reuse LC Index"] C -->|No| E["Generate New<br/>Condition"] D --> F["Result:<br/>2 Conditions"] E --> FFile Walkthrough
condition_generator.js
Add inverse comparison cache lookup logicjs/transpiler/transpiler/condition_generator.js
conditions
binarycache for inverse operator when processing synthesizedoperators
test_not_precedence.js
Test duplicate detection for >= synthesisjs/transpiler/transpiler/tests/test_not_precedence.js
x >= 6correctly reuses condition 0 fromx < 6test_duplicate_detection_comprehensive.js
Comprehensive tests for synthesized operator reusejs/transpiler/transpiler/tests/test_duplicate_detection_comprehensive.js