diff --git a/src/mir/builder/exprs.rs b/src/mir/builder/exprs.rs index b01ebaf5..7b4c906a 100644 --- a/src/mir/builder/exprs.rs +++ b/src/mir/builder/exprs.rs @@ -5,6 +5,11 @@ use crate::ast::{ASTNode, AssignStmt, ReturnStmt, BinaryExpr, CallExpr, MethodCa impl super::MirBuilder { // Main expression dispatcher pub(super) fn build_expression_impl(&mut self, ast: ASTNode) -> Result { + if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { + if matches!(ast, ASTNode::Loop { .. }) { + eprintln!("[build_expression_impl] === ENTRY === processing Loop node"); + } + } match ast { // Control flow constructs (formerly in exprs_legacy) ASTNode::Program { statements, .. } => { @@ -32,6 +37,9 @@ impl super::MirBuilder { self.cf_if(*condition, then_node, else_node) } ASTNode::Loop { condition, body, .. } => { + if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { + eprintln!("[exprs.rs:35] FIRST Loop pattern matched"); + } self.cf_loop(*condition, body) } ASTNode::While { condition, body, .. } => { @@ -160,33 +168,42 @@ impl super::MirBuilder { // Special entry box: materialize main() as Program and lower others as static functions self.build_static_main_box(name.clone(), methods.clone()) } else if is_static { - // Generic static box: lower all static methods into standalone MIR functions (BoxName.method/N) - // Note: Metadata clearing is now handled by BoxCompilationContext (箱理論) - // See lifecycle.rs for context creation and builder_calls.rs for context swap - self.user_defined_boxes.insert(name.clone()); - for (method_name, method_ast) in methods.clone() { - if let ASTNode::FunctionDeclaration { params, body, .. } = method_ast { - let func_name = format!( - "{}.{}{}", - name, - method_name, - format!("/{}", params.len()) - ); - self.lower_static_method_as_function( - func_name, - params.clone(), - body.clone(), - )?; - // Index static method for fallback resolution of bare calls - self.static_method_index - .entry(method_name.clone()) - .or_insert_with(Vec::new) - .push((name.clone(), params.len())); + // In App mode (Main/main present), static boxes are lowered in lower_root(). + // Here we only handle Script/Test mode or non-root contexts. + let is_app_mode = self.root_is_app_mode.unwrap_or(false); + if is_app_mode { + // Already lowered by lifecycle pass; return Void as a pure declaration. + let void_val = crate::mir::builder::emission::constant::emit_void(self); + Ok(void_val) + } else { + // Generic static box: lower all static methods into standalone MIR functions (BoxName.method/N) + // Note: Metadata clearing is now handled by BoxCompilationContext (箱理論) + // See lifecycle.rs for context creation and builder_calls.rs for context swap + self.user_defined_boxes.insert(name.clone()); + for (method_name, method_ast) in methods.clone() { + if let ASTNode::FunctionDeclaration { params, body, .. } = method_ast { + let func_name = format!( + "{}.{}{}", + name, + method_name, + format!("/{}", params.len()) + ); + self.lower_static_method_as_function( + func_name, + params.clone(), + body.clone(), + )?; + // Index static method for fallback resolution of bare calls + self.static_method_index + .entry(method_name.clone()) + .or_insert_with(Vec::new) + .push((name.clone(), params.len())); + } } + // Return void for declaration context + let void_val = crate::mir::builder::emission::constant::emit_void(self); + Ok(void_val) } - // Return void for declaration context - let void_val = crate::mir::builder::emission::constant::emit_void(self); - Ok(void_val) } else { // Instance box: register type and lower instance methods/ctors as functions self.user_defined_boxes.insert(name.clone()); @@ -327,54 +344,12 @@ impl super::MirBuilder { self.build_await_expression(*expression.clone()) } - - - ASTNode::Program { statements, .. } => self.cf_block(statements.clone()), - ASTNode::ScopeBox { body, .. } => self.cf_block(body.clone()), - - ASTNode::Print { expression, .. } => self.build_print_statement(*expression.clone()), - - ASTNode::If { - condition, - then_body, - else_body, - .. - } => { - let else_ast = if let Some(else_statements) = else_body { - Some(ASTNode::Program { - statements: else_statements.clone(), - span: crate::ast::Span::unknown(), - }) - } else { - None - }; - self.cf_if( - *condition.clone(), - ASTNode::Program { - statements: then_body.clone(), - span: crate::ast::Span::unknown(), - }, - else_ast, - ) + // UsingStatement: namespace resolution is done at parser/runner level. + // No MIR emission needed - just return void. + ASTNode::UsingStatement { .. } => { + Ok(crate::mir::builder::emission::constant::emit_void(self)) } - ASTNode::Loop { - condition, body, .. - } => self.cf_loop(*condition.clone(), body.clone()), - - ASTNode::TryCatch { - try_body, - catch_clauses, - finally_body, - .. - } => self.cf_try_catch( - try_body.clone(), - catch_clauses.clone(), - finally_body.clone(), - ), - - ASTNode::Throw { expression, .. } => self.cf_throw(*expression.clone()), - _ => Err(format!("Unsupported AST node type: {:?}", ast)), } } diff --git a/src/mir/builder/stmts.rs b/src/mir/builder/stmts.rs index 99f9947b..cb6dc980 100644 --- a/src/mir/builder/stmts.rs +++ b/src/mir/builder/stmts.rs @@ -190,6 +190,9 @@ impl super::MirBuilder { variables: Vec, initial_values: Vec>>, ) -> Result { + if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { + eprintln!("[build_local_statement] ENTRY: variables={:?}, initial_values.len()={}", variables, initial_values.len()); + } let mut last_value = None; for (i, var_name) in variables.iter().enumerate() { let var_id = if i < initial_values.len() && initial_values[i].is_some() { @@ -198,8 +201,12 @@ impl super::MirBuilder { let init_val = self.build_expression(*init_expr.clone())?; // FIX: Allocate a new ValueId for this local variable - // and emit a Copy instruction to establish SSA form - let var_id = self.value_gen.next(); + // Use next_value_id() which respects function context + let var_id = self.next_value_id(); + + if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { + eprintln!("[build_local_statement] '{}': init_val={:?}, allocated var_id={:?}", var_name, init_val, var_id); + } self.emit_instruction(crate::mir::MirInstruction::Copy { dst: var_id, @@ -212,9 +219,16 @@ impl super::MirBuilder { var_id } else { // Create a concrete register for uninitialized locals (Void) - crate::mir::builder::emission::constant::emit_void(self) + let void_id = crate::mir::builder::emission::constant::emit_void(self); + if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { + eprintln!("[build_local_statement] '{}': uninitialized, void_id={:?}", var_name, void_id); + } + void_id }; + if std::env::var("NYASH_LOOPFORM_DEBUG").is_ok() { + eprintln!("[build_local_statement] Inserting '{}' -> {:?} into variable_map", var_name, var_id); + } self.variable_map.insert(var_name.clone(), var_id); last_value = Some(var_id); } diff --git a/src/mir/function.rs b/src/mir/function.rs index caf9c75a..489ac800 100644 --- a/src/mir/function.rs +++ b/src/mir/function.rs @@ -83,7 +83,9 @@ impl MirFunction { entry_block, locals: Vec::new(), params: Vec::new(), - next_value_id: 0, + // CRITICAL FIX: Start from 1 to reserve ValueId(0) as invalid/uninitialized + // LoopForm v2 and other systems use ValueId(0) as sentinel value + next_value_id: 1, metadata: FunctionMetadata::default(), } } diff --git a/src/mir/verification/cfg.rs b/src/mir/verification/cfg.rs index a3a2b0c3..fbe6046d 100644 --- a/src/mir/verification/cfg.rs +++ b/src/mir/verification/cfg.rs @@ -17,12 +17,12 @@ pub fn check_control_flow(function: &MirFunction) -> Result<(), Vec text_len { return -1 } + local matches = 1 + local j = 0 + loop(j < pattern_len) { + local text_ch = text.substring(i + j, i + j + 1) + local pat_ch = pattern.substring(j, j + 1) + if text_ch != pat_ch { + matches = 0 + break + } + j = j + 1 + } + if matches == 1 { return i } + i = i + 1 + } + return -1 + } + + // Simplified helper to read string after quote (replaces JsonFragBox.read_string_after) + _read_string_after(text, start_pos) { + local text_len = text.length() + local i = start_pos + local result = "" + loop(i < text_len) { + local ch = text.substring(i, i + 1) + if ch == "\"" { break } + result = result + ch + i = i + 1 + } + return result + } + + collect_entries(src_unused) { + // Simulate realistic JSON with both name and optional path + local json = "[{\"name\":\"A\",\"path\":\"x\"},{\"name\":\"B\"}]" + local out = new ArrayBox() + local pos = 0 + local n = json.length() + loop(pos < n) { + local name_idx = me._find_from(json, "\"name\":\"", pos) + if name_idx < 0 { break } + local name = me._read_string_after(json, name_idx + 8) + local obj_end = me._find_from(json, "}", name_idx) + if obj_end < 0 { obj_end = n } + + local path = null + local path_idx = me._find_from(json, "\"path\":\"", name_idx) + if path_idx >= 0 && path_idx < obj_end { + path = me._read_string_after(json, path_idx + 8) + } + + local entry = new MapBox() + entry.set("name", name) + if path != null { entry.set("path", path) } + out.push(entry) + pos = obj_end + 1 + } + return out + } + + main() { + local entries = me.collect_entries("") + if entries == null { return 0 } + return entries.length() + } +} +"#; + + let ast: ASTNode = NyashParser::parse_from_string(src).expect("parse ok"); + let mut mc = MirCompiler::with_options(false); + let cr = mc.compile(ast).expect("compile"); + + // Dump MIR for analysis + let printer = MirPrinter::verbose(); + let mir_output = printer.print_module(&cr.module); + println!("=== MIR Dump ==="); + println!("{}", mir_output); + println!("=== End MIR Dump ==="); + + let mut verifier = MirVerifier::new(); + if let Err(errors) = verifier.verify_module(&cr.module) { + for e in &errors { + eprintln!("[rust-mir-verify] {}", e); + } + panic!("MIR verification failed for Stage1UsingResolverFull"); + } +} + /// Verify MIR/SSA for ParserBox.parse_program2 in isolation by compiling a small wrapper. #[test] fn mir_parserbox_parse_program2_harness_parses_minimal_source() { @@ -99,7 +206,7 @@ static box ParserBoxHarness { let mut verifier = MirVerifier::new(); if let Err(errors) = verifier.verify_module(&cr.module) { for e in &errors { - eprintln!("[mir-verify] {}", e); + eprintln!("[rust-mir-verify] {}", e); } panic!("MIR verification failed for ParserBoxHarness"); } diff --git a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_else_only_reachable_phi_trace_vm.sh b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_else_only_reachable_phi_trace_vm.sh index 4b911e9d..853ba80f 100644 --- a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_else_only_reachable_phi_trace_vm.sh +++ b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_else_only_reachable_phi_trace_vm.sh @@ -30,7 +30,7 @@ cat > "$tmp_json" <<'JSON' JSON set +e -out="$(out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1 )"; rc=$? +out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1)"; rc=$? set -e rm -f "$tmp_json" || true diff --git a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_phi_trace_vm.sh b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_phi_trace_vm.sh index af52f141..1bd39bb1 100644 --- a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_phi_trace_vm.sh +++ b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_if_phi_trace_vm.sh @@ -31,7 +31,7 @@ cat > "$tmp_json" <<'JSON' JSON set +e -out="$(out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1 )"; rc=$? +out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1)"; rc=$? set -e rm -f "$tmp_json" || true diff --git a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_continue_break_phi_trace_vm.sh b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_continue_break_phi_trace_vm.sh index cc2a0224..bb24d0e8 100644 --- a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_continue_break_phi_trace_vm.sh +++ b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_continue_break_phi_trace_vm.sh @@ -45,7 +45,7 @@ cat > "$tmp_json" <<'JSON' JSON set +e -out="$(out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1 )"; rc=$? +out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1)"; rc=$? set -e rm -f "$tmp_json" || true diff --git a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_phi_trace_vm.sh b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_phi_trace_vm.sh index af737f74..eac2d196 100644 --- a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_phi_trace_vm.sh +++ b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_loop_phi_trace_vm.sh @@ -34,7 +34,7 @@ cat > "$tmp_json" <<'JSON' JSON set +e -out="$(out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1 )"; rc=$? +out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1)"; rc=$? set -e rm -f "$tmp_json" || true diff --git a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_nested_if_phi_trace_vm.sh b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_nested_if_phi_trace_vm.sh index 07792135..15c5090f 100644 --- a/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_nested_if_phi_trace_vm.sh +++ b/tools/smokes/v2/profiles/quick/core/phase2034/program_v0_nested_if_phi_trace_vm.sh @@ -38,7 +38,7 @@ cat > "$tmp_json" <<'JSON' JSON set +e -out="$(out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1 )"; rc=$? +out="$(NYASH_VM_TRACE_PHI=1 "$NYASH_BIN" --json-file "$tmp_json" 2>&1)"; rc=$? set -e rm -f "$tmp_json" || true