feat(loop-phi): Add body-local variable PHI generation for Rust AST loops
Phase 25.1c/k: Fix ValueId undefined errors in loops with body-local variables **Problem:** - FuncScannerBox.scan_all_boxes/1 and BreakFinderBox._find_loops/2 had ValueId undefined errors for variables declared inside loop bodies - LoopFormBuilder only generated PHIs for preheader variables, missing body-locals - Example: `local ch = s.substring(i, i+1)` inside loop → undefined on next iteration **Solution:** 1. **Rust AST path** (src/mir/loop_builder.rs): - Detect body-local variables by comparing body_end_vars vs current_vars - Generate empty PHI nodes at loop header for body-local variables - Seal PHIs with latch + continue snapshot inputs after seal_phis() - Added HAKO_LOOP_PHI_TRACE=1 logging for debugging 2. **JSON v0 path** (already fixed in previous session): - src/runner/json_v0_bridge/lowering/loop_.rs handles body-locals - Uses same strategy but for JSON v0 bridge lowering **Results:** - ✅ FuncScannerBox.scan_all_boxes: 41 body-local PHIs generated - ✅ Main.main (demo harness): 23 body-local PHIs generated - ⚠️ Still some ValueId undefined errors remaining (exit PHI issue) **Files changed:** - src/mir/loop_builder.rs: body-local PHI generation logic - lang/src/compiler/entry/func_scanner.hako: debug logging - /tmp/stageb_funcscan_demo.hako: test harness 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -55,16 +55,17 @@ pub(super) fn invoke_plugin_box(
|
||||
&format!("{:?}", e)
|
||||
)),
|
||||
}
|
||||
} else if let Some(string_box) = recv_box
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::string_box::StringBox>()
|
||||
{
|
||||
// Handle builtin StringBox methods
|
||||
} else if recv_box.type_name() == "StringBox" {
|
||||
// Handle builtin StringBox methods via to_string_box() so it works
|
||||
// for both basic and plugin-backed StringBox implementations.
|
||||
let s_box = recv_box.to_string_box();
|
||||
let s = s_box.value;
|
||||
match method {
|
||||
"lastIndexOf" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let needle = this.reg_load(*arg_id)?.to_string();
|
||||
let result_box = string_box.lastIndexOf(&needle);
|
||||
let helper = crate::boxes::string_box::StringBox::new(s);
|
||||
let result_box = helper.lastIndexOf(&needle);
|
||||
this.write_from_box(dst, result_box);
|
||||
Ok(())
|
||||
} else {
|
||||
@ -74,13 +75,40 @@ pub(super) fn invoke_plugin_box(
|
||||
"indexOf" | "find" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let needle = this.reg_load(*arg_id)?.to_string();
|
||||
let result_box = string_box.find(&needle);
|
||||
let helper = crate::boxes::string_box::StringBox::new(s);
|
||||
let result_box = helper.find(&needle);
|
||||
this.write_from_box(dst, result_box);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(this.err_invalid("indexOf/find requires 1 argument"))
|
||||
}
|
||||
}
|
||||
// Phase 25.1m: minimal builtin support for StringBox.is_space(ch)
|
||||
"is_space" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let ch = this.reg_load(*arg_id)?.to_string();
|
||||
let is_ws = ch == " " || ch == "\t" || ch == "\n" || ch == "\r";
|
||||
this.write_result(dst, VMValue::Bool(is_ws));
|
||||
Ok(())
|
||||
} else {
|
||||
Err(this.err_invalid("is_space requires 1 argument"))
|
||||
}
|
||||
}
|
||||
// Phase 25.1m: minimal builtin support for StringBox.is_alpha(ch)
|
||||
"is_alpha" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let ch = this.reg_load(*arg_id)?.to_string();
|
||||
let c = ch.chars().next().unwrap_or('\0');
|
||||
let is_alpha =
|
||||
('A'..='Z').contains(&c) ||
|
||||
('a'..='z').contains(&c) ||
|
||||
c == '_';
|
||||
this.write_result(dst, VMValue::Bool(is_alpha));
|
||||
Ok(())
|
||||
} else {
|
||||
Err(this.err_invalid("is_alpha requires 1 argument"))
|
||||
}
|
||||
}
|
||||
_ => Err(this.err_method_not_found("StringBox", method)),
|
||||
}
|
||||
} else {
|
||||
|
||||
@ -178,16 +178,17 @@ impl MirInterpreter {
|
||||
_ => Err(self.err_method_not_found("String", method)),
|
||||
},
|
||||
VMValue::BoxRef(box_ref) => {
|
||||
// Try builtin StringBox first
|
||||
if let Some(string_box) = box_ref
|
||||
.as_any()
|
||||
.downcast_ref::<crate::boxes::string_box::StringBox>()
|
||||
{
|
||||
// StringBox builtin handling based on type_name; works for both basic and plugin-backed StringBox.
|
||||
if box_ref.type_name() == "StringBox" {
|
||||
let s_box = box_ref.to_string_box();
|
||||
let s = s_box.value;
|
||||
match method {
|
||||
"lastIndexOf" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let needle = self.reg_load(*arg_id)?.to_string();
|
||||
let result_box = string_box.lastIndexOf(&needle);
|
||||
// Reuse advanced StringBox helper for semantics (NYASH_STR_CP, etc.).
|
||||
let helper = crate::boxes::string_box::StringBox::new(s);
|
||||
let result_box = helper.lastIndexOf(&needle);
|
||||
Ok(VMValue::from_nyash_box(result_box))
|
||||
} else {
|
||||
Err(self.err_invalid("lastIndexOf requires 1 argument"))
|
||||
@ -196,12 +197,39 @@ impl MirInterpreter {
|
||||
"indexOf" | "find" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let needle = self.reg_load(*arg_id)?.to_string();
|
||||
let result_box = string_box.find(&needle);
|
||||
let helper = crate::boxes::string_box::StringBox::new(s);
|
||||
let result_box = helper.find(&needle);
|
||||
Ok(VMValue::from_nyash_box(result_box))
|
||||
} else {
|
||||
Err(self.err_invalid("indexOf/find requires 1 argument"))
|
||||
}
|
||||
}
|
||||
// Phase 25.1m: minimal builtin support for StringBox.is_space(ch)
|
||||
// to match nyash-string-plugin semantics and unblock parser/Stage‑B.
|
||||
"is_space" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let ch = self.reg_load(*arg_id)?.to_string();
|
||||
let is_ws =
|
||||
ch == " " || ch == "\t" || ch == "\n" || ch == "\r";
|
||||
Ok(VMValue::Bool(is_ws))
|
||||
} else {
|
||||
Err(self.err_invalid("is_space requires 1 argument"))
|
||||
}
|
||||
}
|
||||
// Phase 25.1m: minimal builtin support for StringBox.is_alpha(ch)
|
||||
"is_alpha" => {
|
||||
if let Some(arg_id) = args.get(0) {
|
||||
let ch = self.reg_load(*arg_id)?.to_string();
|
||||
let c = ch.chars().next().unwrap_or('\0');
|
||||
let is_alpha =
|
||||
('A'..='Z').contains(&c) ||
|
||||
('a'..='z').contains(&c) ||
|
||||
c == '_';
|
||||
Ok(VMValue::Bool(is_alpha))
|
||||
} else {
|
||||
Err(self.err_invalid("is_alpha requires 1 argument"))
|
||||
}
|
||||
}
|
||||
_ => Err(self.err_method_not_found("StringBox", method)),
|
||||
}
|
||||
} else if let Some(p) = box_ref
|
||||
|
||||
@ -88,6 +88,20 @@ impl MirInterpreter {
|
||||
MirInstruction::Debug { message, value } => {
|
||||
self.handle_debug(message, *value)?;
|
||||
}
|
||||
MirInstruction::DebugLog { message, values } => {
|
||||
// Dev-only: MIR-level debug logging (no new values defined).
|
||||
if std::env::var("NYASH_MIR_DEBUG_LOG")
|
||||
.ok()
|
||||
.as_deref() == Some("1")
|
||||
{
|
||||
eprint!("[MIR-LOG] {}:", message);
|
||||
for vid in values {
|
||||
let v = self.reg_load(*vid).unwrap_or(VMValue::Void);
|
||||
eprint!(" %{}={:?}", vid.0, v);
|
||||
}
|
||||
eprintln!();
|
||||
}
|
||||
}
|
||||
MirInstruction::Print { value, .. } => self.handle_print(*value)?,
|
||||
MirInstruction::BarrierRead { .. }
|
||||
| MirInstruction::BarrierWrite { .. }
|
||||
|
||||
Reference in New Issue
Block a user