Skip to content

Commit 4d2f12e

Browse files
committed
feat: Fix block statement collection in Zyn grammar runtime
- Add blocks storage to TypedAstBuilder for proper block handling - Fix create_block to store entire TypedBlock instead of just first stmt - Add get_block helper to retrieve blocks by handle - Update create_function to properly retrieve body blocks - Convert zig.zyn to JSON command format (from Rust action blocks) - Add GetAllChildren command for statement* patterns - Fix InternedString serialization to output actual string This fixes the issue where function bodies only captured the first statement. Now all statements in a block are properly preserved through the TypedAST pipeline. Test results: - return 42 → 42 ✓ - return 2 + 3 * 4 → 14 ✓ - const x = 10; return x; → 10 ✓ - const x = 10; const y = 20; return x + y; → 30 ✓
1 parent 133067a commit 4d2f12e

6 files changed

Lines changed: 837 additions & 1222 deletions

File tree

crates/typed_ast/src/arena.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,14 @@ impl Serialize for InternedString {
4848
where
4949
S: serde::Serializer,
5050
{
51-
// Serialize as the underlying symbol index
52-
self.0.to_usize().serialize(serializer)
51+
// Serialize as the actual string value for portability
52+
// This makes the JSON human-readable and works across processes
53+
if let Some(resolved) = self.resolve_global() {
54+
resolved.serialize(serializer)
55+
} else {
56+
// Fallback to empty string if resolution fails
57+
"".serialize(serializer)
58+
}
5359
}
5460
}
5561

crates/zyn_parser/tests/zig_e2e_jit.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,52 @@ use zyntax_compiler::{
2020
};
2121
use std::sync::{Arc, Mutex};
2222

23+
/// Debug test to print TypedAST from Rust ZigParser for comparison with Zyn grammar output
24+
#[test]
25+
fn test_debug_typed_ast_variable_reference() {
26+
use zyntax_typed_ast::{TypedDeclaration, TypedStatement};
27+
28+
let source = r#"
29+
fn calculate() i32 {
30+
const x = 10;
31+
return x;
32+
}
33+
"#;
34+
35+
let pairs = ZigParser::parse(Rule::program, source)
36+
.expect("Failed to parse Zig source");
37+
let mut builder = ZigBuilder::new();
38+
let program = builder.build_program(pairs)
39+
.expect("Failed to build TypedAST");
40+
41+
// Verify both statements are in the body
42+
assert_eq!(program.declarations.len(), 1);
43+
if let TypedDeclaration::Function(func) = &program.declarations[0].node {
44+
let body = func.body.as_ref().expect("Function should have a body");
45+
println!("[DEBUG] Function body has {} statements", body.statements.len());
46+
assert_eq!(body.statements.len(), 2, "Expected 2 statements (const x = 10 and return x)");
47+
48+
// Check first statement is Let
49+
match &body.statements[0].node {
50+
TypedStatement::Let(let_binding) => {
51+
let name_str = let_binding.name.resolve_global().unwrap_or_default();
52+
println!("[DEBUG] Statement 0: Let binding for '{}'", name_str);
53+
}
54+
other => panic!("Expected Let statement, got {:?}", other),
55+
}
56+
57+
// Check second statement is Return
58+
match &body.statements[1].node {
59+
TypedStatement::Return(expr) => {
60+
println!("[DEBUG] Statement 1: Return with expr: {:?}", expr);
61+
}
62+
other => panic!("Expected Return statement, got {:?}", other),
63+
}
64+
} else {
65+
panic!("Expected function declaration");
66+
}
67+
}
68+
2369
#[test]
2470
fn test_zig_jit_simple_function() {
2571
let source = r#"

0 commit comments

Comments
 (0)