feat(control_tree): Phase 128 - add Assign(int literal) to Normalized builder (dev-only)

This commit is contained in:
nyash-codex
2025-12-18 07:03:57 +09:00
parent 44762a2467
commit c522d22daa

View File

@ -207,13 +207,13 @@ impl StepTreeNormalizedShadowLowererBox {
env_params.clone(), env_params.clone(),
); );
// Phase 123-125: Return node lowering // Phase 123-128: Return node lowering
// If Phase 123-125 patterns are not supported, return Ok(None) // If Phase 123-128 patterns are not supported, return Ok(None)
match Self::lower_return_from_tree( match Self::lower_return_from_tree(
&step_tree.root, &step_tree.root,
&mut main_func.body, &mut main_func.body,
&mut next_value_id, &mut next_value_id,
&env, &mut env,
&step_tree.contract, &step_tree.contract,
) { ) {
Ok(()) => { Ok(()) => {
@ -222,9 +222,10 @@ impl StepTreeNormalizedShadowLowererBox {
Err(msg) Err(msg)
if msg.starts_with("[phase123/") if msg.starts_with("[phase123/")
|| msg.starts_with("[phase124/") || 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); return Ok(None);
} }
Err(msg) => { Err(msg) => {
@ -245,20 +246,21 @@ impl StepTreeNormalizedShadowLowererBox {
Ok(Some((module, meta))) 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(Integer literal): Generate Const + Ret(Some(vid))
/// - Return(void): Ret(None) /// - Return(void): Ret(None)
/// - Return(Variable): Phase 124 - lookup from env (dev-only) /// - Return(Variable): Phase 124 - lookup from env (dev-only)
/// - Return(other): Fail-Fast with structured error /// - Return(other): Fail-Fast with structured error
/// - If(minimal compare): Generate Compare + Branch + Ret (P3) /// - If(minimal compare): Generate Compare + Branch + Ret (P3)
/// - Assign(int literal): Phase 128 - update env with Const
fn lower_return_from_tree( fn lower_return_from_tree(
node: &crate::mir::control_tree::step_tree::StepNode, node: &crate::mir::control_tree::step_tree::StepNode,
body: &mut Vec<crate::mir::join_ir::JoinInst>, body: &mut Vec<crate::mir::join_ir::JoinInst>,
next_value_id: &mut u32, next_value_id: &mut u32,
env: &std::collections::BTreeMap<String, crate::mir::ValueId>, env: &mut std::collections::BTreeMap<String, crate::mir::ValueId>,
contract: &crate::mir::control_tree::step_tree_contract_box::StepTreeContract, contract: &crate::mir::control_tree::step_tree_contract_box::StepTreeContract,
) -> Result<(), String> { ) -> Result<(), String> {
use crate::ast::{ASTNode, LiteralValue}; use crate::ast::{ASTNode, LiteralValue};
@ -271,6 +273,14 @@ impl StepTreeNormalizedShadowLowererBox {
// Process nodes in order // Process nodes in order
for n in nodes { for n in nodes {
match n { 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 { StepNode::Stmt {
kind: StepStmtKind::Return { value_ast }, 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<String>,
value_ast: &Option<crate::mir::control_tree::step_tree::AstNodeHandle>,
body: &mut Vec<crate::mir::join_ir::JoinInst>,
next_value_id: &mut u32,
env: &mut std::collections::BTreeMap<String, crate::mir::ValueId>,
) -> 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 /// ## Support
/// ///
/// - Minimal binary comparison: Variable vs Integer literal /// - Minimal binary comparison: Variable vs Integer literal
/// - then/else: Return(Integer literal) or Return(Variable) (Phase 124) /// - 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) /// ## Not Supported (Fail-Fast)
/// ///
/// - Compound expressions (&&, ||) /// - Compound expressions (&&, ||)
/// - Method calls /// - Method calls
/// - Complex expressions /// - Complex expressions
/// - Non-return statements in branches /// - Non-return statements in branches (except Assign in Phase 128)
fn lower_if_node( fn lower_if_node(
node: &crate::mir::control_tree::step_tree::StepNode, node: &crate::mir::control_tree::step_tree::StepNode,
body: &mut Vec<crate::mir::join_ir::JoinInst>, body: &mut Vec<crate::mir::join_ir::JoinInst>,
next_value_id: &mut u32, next_value_id: &mut u32,
env: &std::collections::BTreeMap<String, crate::mir::ValueId>, env: &mut std::collections::BTreeMap<String, crate::mir::ValueId>,
contract: &crate::mir::control_tree::step_tree_contract_box::StepTreeContract, contract: &crate::mir::control_tree::step_tree_contract_box::StepTreeContract,
) -> Result<(), String> { ) -> Result<(), String> {
use crate::ast::{ASTNode, BinaryOperator}; use crate::ast::{ASTNode, BinaryOperator};