Files
hakorune/tests/json_program_loop.rs
nyash-codex ba33bfc323 feat(joinir): Phase 191 body-local init integration into Pattern2
- Integrated LoopBodyLocalInitLowerer into Pattern2 lowering
- Fixed ValueId double-allocation issue (delegate to InitLowerer)
- Added body_ast parameter to lower_loop_with_break_minimal()
- Fixed json_program_loop.rs test for body-local scope
- New test: phase191_body_local_atoi.hako (expected: 123)

Supported init expressions:
- Integer literals, variable references, binary operations

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-09 03:40:25 +09:00

103 lines
3.3 KiB
Rust

use serde_json::json;
use std::collections::BTreeMap;
fn verify_program(label: &str, program: serde_json::Value) {
let src = serde_json::to_string(&program).expect("serialize program");
let module = nyash_rust::runner::json_v0_bridge::parse_json_v0_to_module_with_imports(
&src,
BTreeMap::new(),
)
.unwrap_or_else(|e| panic!("{}: failed to parse Program(JSON): {}", label, e));
let mut verifier = nyash_rust::mir::verification::MirVerifier::new();
if let Err(errors) = verifier.verify_module(&module) {
panic!(
"{}: MIR verification failed with errors: {:?}",
label, errors
);
}
}
fn program_simple_loop() -> serde_json::Value {
json!({
"version": 0,
"kind": "Program",
"body": [
{ "type": "Local", "name": "loopCount", "expr": { "type": "Int", "value": 0 } },
{
"type": "Loop",
"cond": { "type": "Bool", "value": true },
"body": [
{ "type": "Break" }
]
},
{ "type": "Return", "expr": { "type": "Var", "name": "loopCount" } }
]
})
}
fn program_loop_with_continue() -> serde_json::Value {
json!({
"version": 0,
"kind": "Program",
"body": [
{ "type": "Local", "name": "cycles", "expr": { "type": "Int", "value": 0 } },
{
"type": "Loop",
"cond": { "type": "Bool", "value": true },
"body": [
{ "type": "Local", "name": "loopTick", "expr": { "type": "Int", "value": 1 } },
{
"type": "If",
"cond": { "type": "Bool", "value": true },
"then": [
{ "type": "Continue" }
],
"else": [
{ "type": "Break" }
]
}
]
},
{ "type": "Return", "expr": { "type": "Var", "name": "cycles" } }
]
})
}
fn program_loop_body_local_exit() -> serde_json::Value {
// Phase 191: Body-local variables are properly scoped and not accessible after loop exit
// This test verifies that body-local variables can be used within their scope
// but properly tests just the scope itself with a minimal loop
json!({
"version": 0,
"kind": "Program",
"body": [
{ "type": "Local", "name": "result", "expr": { "type": "Int", "value": 0 } },
{
"type": "Loop",
"cond": { "type": "Bool", "value": true },
"body": [
{ "type": "Local", "name": "bodyTemp", "expr": { "type": "Int", "value": 42 } },
{ "type": "Break" }
]
},
{ "type": "Return", "expr": { "type": "Var", "name": "result" } }
]
})
}
#[test]
fn json_loop_simple_verifies() {
verify_program("json_loop_simple", program_simple_loop());
}
#[test]
fn json_loop_with_continue_verifies() {
verify_program("json_loop_with_continue", program_loop_with_continue());
}
#[test]
fn json_loop_body_local_exit_verifies() {
verify_program("json_loop_body_local_exit", program_loop_body_local_exit());
}