diff --git a/src/mir/control_tree/normalized_shadow/builder.rs b/src/mir/control_tree/normalized_shadow/builder.rs index b540b379..716ea5b9 100644 --- a/src/mir/control_tree/normalized_shadow/builder.rs +++ b/src/mir/control_tree/normalized_shadow/builder.rs @@ -207,13 +207,13 @@ impl StepTreeNormalizedShadowLowererBox { env_params.clone(), ); - // Phase 123-125: Return node lowering - // If Phase 123-125 patterns are not supported, return Ok(None) + // Phase 123-128: Return node lowering + // If Phase 123-128 patterns are not supported, return Ok(None) match Self::lower_return_from_tree( &step_tree.root, &mut main_func.body, &mut next_value_id, - &env, + &mut env, &step_tree.contract, ) { Ok(()) => { @@ -222,9 +222,10 @@ impl StepTreeNormalizedShadowLowererBox { Err(msg) if msg.starts_with("[phase123/") || msg.starts_with("[phase124/") - || msg.starts_with("[phase125/") => + || msg.starts_with("[phase125/") + || msg.starts_with("[phase128/") => { - // Phase 123-125 limitation - out of scope + // Phase 123-128 limitation - out of scope return Ok(None); } Err(msg) => { @@ -245,20 +246,21 @@ impl StepTreeNormalizedShadowLowererBox { Ok(Some((module, meta))) } - /// Phase 123-124 P1-P3: Lower node from StepTree + /// Phase 123-128 P1-P3: Lower node from StepTree /// - /// ## Support (Phase 123-124) + /// ## Support (Phase 123-128) /// /// - Return(Integer literal): Generate Const + Ret(Some(vid)) /// - Return(void): Ret(None) /// - Return(Variable): Phase 124 - lookup from env (dev-only) /// - Return(other): Fail-Fast with structured error /// - If(minimal compare): Generate Compare + Branch + Ret (P3) + /// - Assign(int literal): Phase 128 - update env with Const fn lower_return_from_tree( node: &crate::mir::control_tree::step_tree::StepNode, body: &mut Vec, next_value_id: &mut u32, - env: &std::collections::BTreeMap, + env: &mut std::collections::BTreeMap, contract: &crate::mir::control_tree::step_tree_contract_box::StepTreeContract, ) -> Result<(), String> { use crate::ast::{ASTNode, LiteralValue}; @@ -271,6 +273,14 @@ impl StepTreeNormalizedShadowLowererBox { // Process nodes in order for n in nodes { match n { + StepNode::Stmt { + kind: StepStmtKind::Assign { target, value_ast }, + .. + } => { + // Phase 128: Process assign statement + Self::lower_assign_stmt(target, value_ast, body, next_value_id, env)?; + // Continue to next node + } StepNode::Stmt { kind: StepStmtKind::Return { value_ast }, .. @@ -306,25 +316,90 @@ impl StepTreeNormalizedShadowLowererBox { } } - /// Phase 123-124 P3: Lower If node with minimal compare + /// Phase 128: Lower assign statement (int literal only) + /// + /// ## Support + /// + /// - target: Variable name + /// - value: Integer literal only (Phase 128) + /// + /// ## Not Supported (Fail-Fast) + /// + /// - value: Any expression other than Integer literal + /// + /// ## Effect + /// + /// - Generates Const instruction + /// - Updates env[target] to new ValueId + fn lower_assign_stmt( + target: &Option, + value_ast: &Option, + body: &mut Vec, + next_value_id: &mut u32, + env: &mut std::collections::BTreeMap, + ) -> Result<(), String> { + use crate::ast::{ASTNode, LiteralValue}; + use crate::mir::join_ir::{ConstValue, JoinInst, MirLikeInst}; + use crate::mir::ValueId; + + // Check target + let target_name = target + .as_ref() + .ok_or_else(|| "[phase128/assign/target] Assign target must be a variable".to_string())?; + + // Check value_ast + let value_ast = value_ast.as_ref().ok_or_else(|| { + "[phase128/assign/value] Assign value AST is missing".to_string() + })?; + + // Parse value - Phase 128: int literal only + match value_ast.0.as_ref() { + ASTNode::Literal { + value: LiteralValue::Integer(i), + .. + } => { + // Generate Const instruction + let dst_vid = ValueId(*next_value_id); + *next_value_id += 1; + + body.push(JoinInst::Compute(MirLikeInst::Const { + dst: dst_vid, + value: ConstValue::Integer(*i), + })); + + // Update env + env.insert(target_name.clone(), dst_vid); + + Ok(()) + } + _ => { + // Phase 128 limitation: only int literal supported + Err(format!( + "[phase128/assign/unsupported] Phase128 supports int literal only Hint: Assign RHS must be an integer literal (e.g., x = 42)" + )) + } + } + } + + /// Phase 123-128 P3: Lower If node with minimal compare /// /// ## Support /// /// - Minimal binary comparison: Variable vs Integer literal /// - then/else: Return(Integer literal) or Return(Variable) (Phase 124) - /// - Merge: Not yet implemented (will use join_k tail-call in future) + /// - Merge: Phase 128 - env update + join continuation (else保持) /// /// ## Not Supported (Fail-Fast) /// /// - Compound expressions (&&, ||) /// - Method calls /// - Complex expressions - /// - Non-return statements in branches + /// - Non-return statements in branches (except Assign in Phase 128) fn lower_if_node( node: &crate::mir::control_tree::step_tree::StepNode, body: &mut Vec, next_value_id: &mut u32, - env: &std::collections::BTreeMap, + env: &mut std::collections::BTreeMap, contract: &crate::mir::control_tree::step_tree_contract_box::StepTreeContract, ) -> Result<(), String> { use crate::ast::{ASTNode, BinaryOperator};