Fix reactive conditional rendering with proper branch swapping

Implements efficient reactive conditionals that pre-render branches once
and swap DOM nodes without recreation, enabling fast switching with
thousands of elements.

Key Fixes:
- Runtime now calls reevaluate_conditional_*() on signal changes
- Match/case swap logic uses phi nodes to load correct old case
- Previously hardcoded case_0 as old value, causing accumulation
- Branch render functions now wrap all content in container div

Implementation:
- Pre-render both if/else branches at init
- Store roots in globals (@cond_N_then_root, @cond_N_else_root)
- On signal change, swap DOM nodes via js_replace_at_marker()
- Match statements use switch/phi pattern to select old case

Tests:
- Added test_reactive_conditionals.py (5 new tests)
- Updated test_llvm_codegen_conditional.py for reactive infrastructure
- All 35 tests passing

Performance: No re-rendering on switches, perfect for dynamic content.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Aaron SinclairandClaude committed 2025-11-15 10:32:40 -08:00
1 parent ac7726df46
commit e686903170
4 files changed
+272 -41

No files matched your search

+11 -8
View File
@@ -6,7 +6,7 @@ from ritz.llvm_codegen_signals import generate_llvm_ir
def test_if_statement_codegen():
"""Test LLVM IR generation for if statement."""
"""Test LLVM IR generation for if statement with reactive rendering."""
source = """
from ritz.syntax import *
@@ -27,13 +27,15 @@ def Toggle():
# Should have conditional branch
assert "br i1" in llvm_ir
# Should have basic blocks for then/else/merge
assert "if.then." in llvm_ir
assert "if.end." in llvm_ir
# Should have reactive conditional infrastructure
assert "@cond_0_marker" in llvm_ir
assert "_init_conditional_0" in llvm_ir
assert "_render_cond_0_then" in llvm_ir
assert "_render_cond_0_else" in llvm_ir
def test_if_else_codegen():
"""Test LLVM IR generation for if/else."""
"""Test LLVM IR generation for if/else with reactive rendering."""
source = """
from ritz.syntax import *
@@ -54,9 +56,10 @@ def Toggle():
# Should have conditional branch
assert "br i1" in llvm_ir
# Should have both then and else blocks
assert "if.then." in llvm_ir
assert "if.else." in llvm_ir
# Should have reactive infrastructure with both branches
assert "_render_cond_0_then" in llvm_ir
assert "_render_cond_0_else" in llvm_ir
assert "reevaluate_conditional_0" in llvm_ir
def test_if_comparison_codegen():