Implement Python → LLVM IR → WASM reactive framework
MAJOR ACHIEVEMENT: Full working reactive framework! ## What's Working ✅ Python → AST → Ritz IR → LLVM IR → WASM pipeline ✅ Reactive signals stored in WASM memory (@signal_name = global i32) ✅ Event handlers exported from WASM and callable from JS ✅ Clean runtime with RitzLoader and debug tools ✅ Two working examples: HelloWorld and HighFive counter ## Core Components ### Parser (ritz/parser.py) - Uses Python's ast module to parse components - Handles @component decorator, signals, event handlers - Supports yield, with statements, f-strings, augmented assignment (+=) - Clean syntax: from ritz.syntax import * ### LLVM IR Code Generator (ritz/llvm_codegen_signals.py) - Generates LLVM IR directly (not C!) - Signals as global variables in WASM memory - Exports getter/setter functions for each signal - Exports event handler functions for JS to call - UTF-8 string handling ### Build System (ritz/build.py) - Python → AST → IR → LLVM IR → clang --target=wasm32 → WASM - Saves intermediate .ll files for inspection - Debug symbols support (-g flag) ### Runtime (runtime/ritz-runtime.js) - RitzRuntime class: DOM bridge for WASM - RitzLoader class: Simplified component loading - Debug bar with Ctrl+` toggle - Signal updates trigger DOM updates ## Examples ### HelloWorld.py Simple static content - renders <p>Hello World</p> ### HighFive.py Reactive counter with: - Signal in WASM memory (high_fives = signal(0)) - Event handler (increment function) - DOM updates on signal change ## Test Coverage - 4 parser tests (all passing) - 4 LLVM codegen tests (all passing) - 4 signal parsing tests (all passing) - Code formatted with black and ruff ## Next Steps - <script type="text/ritz"> in-browser compilation - Better signal-to-DOM binding tracking - More complex examples (TodoMVC, forms, lists) - Playwright browser tests --- This is a genuinely innovative framework: write Python, compile to WASM, run with reactive signals in WASM memory. No virtual DOM, no transpilation, just compiled Python with fine-grained reactivity. 🎉 Python → WASM → Reactive UI is REAL!
This commit is contained in:
1 parent
316c090778
commit
635d466874
54 files changed
+4393
No files matched your search
@@ -0,0 +1,125 @@
|
||||
"""Tests for Signal reactive system - parsing, codegen, and execution."""
|
||||
|
||||
import textwrap
|
||||
|
||||
|
||||
def test_parse_signal_declaration():
|
||||
"""Test parsing a signal assignment."""
|
||||
from ritz.parser import parse_component
|
||||
|
||||
source = textwrap.dedent(
|
||||
"""
|
||||
@component
|
||||
def Counter():
|
||||
count = signal(0)
|
||||
|
||||
with P():
|
||||
yield "test"
|
||||
"""
|
||||
)
|
||||
|
||||
ir = parse_component(source)
|
||||
|
||||
# Should have signal in body
|
||||
assert len(ir["body"]) == 2
|
||||
signal_stmt = ir["body"][0]
|
||||
|
||||
assert signal_stmt["type"] == "signal"
|
||||
assert signal_stmt["name"] == "count"
|
||||
assert signal_stmt["initial"]["type"] == "constant"
|
||||
assert signal_stmt["initial"]["value"] == 0
|
||||
|
||||
|
||||
def test_parse_signal_in_template():
|
||||
"""Test parsing signal reference in f-string."""
|
||||
from ritz.parser import parse_component
|
||||
|
||||
source = textwrap.dedent(
|
||||
"""
|
||||
@component
|
||||
def Counter():
|
||||
count = signal(0)
|
||||
|
||||
with P():
|
||||
yield f"Count: {count}"
|
||||
"""
|
||||
)
|
||||
|
||||
ir = parse_component(source)
|
||||
|
||||
# Find the yield statement
|
||||
with_stmt = ir["body"][1]
|
||||
yield_stmt = with_stmt["body"][0]
|
||||
|
||||
# Should be an f-string
|
||||
assert yield_stmt["value"]["type"] == "fstring"
|
||||
parts = yield_stmt["value"]["parts"]
|
||||
|
||||
# Should have "Count: " as constant and count as formatted value
|
||||
assert any(p["type"] == "constant" for p in parts)
|
||||
assert any(p["type"] == "formatted_value" for p in parts)
|
||||
|
||||
|
||||
def test_parse_event_handler():
|
||||
"""Test parsing onclick event handler."""
|
||||
from ritz.parser import parse_component
|
||||
|
||||
source = textwrap.dedent(
|
||||
"""
|
||||
@component
|
||||
def Counter():
|
||||
count = signal(0)
|
||||
|
||||
def increment():
|
||||
count += 1
|
||||
|
||||
with Button(onclick=increment):
|
||||
yield "Click me"
|
||||
"""
|
||||
)
|
||||
|
||||
ir = parse_component(source)
|
||||
|
||||
# Should have signal, function, and with statement
|
||||
assert len(ir["body"]) == 3
|
||||
|
||||
# Check function definition
|
||||
func_stmt = ir["body"][1]
|
||||
assert func_stmt["type"] == "function"
|
||||
assert func_stmt["name"] == "increment"
|
||||
|
||||
# Check onclick prop
|
||||
with_stmt = ir["body"][2]
|
||||
onclick_prop = with_stmt["element"]["props"].get("onclick")
|
||||
assert onclick_prop is not None
|
||||
assert onclick_prop["type"] == "name"
|
||||
assert onclick_prop["id"] == "increment"
|
||||
|
||||
|
||||
def test_parse_signal_update():
|
||||
"""Test parsing signal increment (count += 1)."""
|
||||
from ritz.parser import parse_component
|
||||
|
||||
source = textwrap.dedent(
|
||||
"""
|
||||
@component
|
||||
def Counter():
|
||||
count = signal(0)
|
||||
|
||||
def increment():
|
||||
count += 1
|
||||
"""
|
||||
)
|
||||
|
||||
ir = parse_component(source)
|
||||
|
||||
# Get the increment function
|
||||
func_stmt = ir["body"][1]
|
||||
func_body = func_stmt["body"][0]
|
||||
|
||||
# Should be an augmented assignment
|
||||
assert func_body["type"] == "augassign"
|
||||
assert func_body["target"] == "count"
|
||||
assert func_body["op"] == "Add"
|
||||
assert func_body["value"]["type"] == "constant"
|
||||
assert func_body["value"]["value"] == 1
|
||||
Reference in new issue
Block a user