diff --git a/src/mir/join_ir/frontend/ast_lowerer.rs b/src/mir/join_ir/frontend/ast_lowerer.rs index a0a3b17f..0844e642 100644 --- a/src/mir/join_ir/frontend/ast_lowerer.rs +++ b/src/mir/join_ir/frontend/ast_lowerer.rs @@ -63,17 +63,21 @@ impl AstToJoinIrLowerer { .expect("Function must have 'name'"); // 3. 関数名で分岐(Phase 34-2/34-3) + // simple も local も同じ If Return pattern なので共通処理 match func_name { - "test" => self.lower_simple_if(program_json), - "local" => self.lower_local_if(program_json), + "test" | "local" => self.lower_if_return_pattern(program_json), _ => panic!("Unsupported function: {}", func_name), } } - /// Simple pattern lowering: `if cond { return 1 } else { return 2 }` + /// If Return pattern の共通 lowering /// - /// Phase 34-2: IfSelectTest.test 用の lowering - fn lower_simple_if(&mut self, program_json: &serde_json::Value) -> JoinModule { + /// Phase 34-2/34-3: simple/local 両対応 + /// - simple: `if cond { return 10 } else { return 20 }` + /// - local: `if cond { x=10 } else { x=20 }; return x` (意味論的) + /// + /// 両方とも同じ JoinIR Select に正規化される + fn lower_if_return_pattern(&mut self, program_json: &serde_json::Value) -> JoinModule { // 1. Program(JSON) から defs を取得 let defs = program_json["defs"] .as_array() @@ -92,17 +96,17 @@ impl AstToJoinIrLowerer { .as_array() .expect("Function must have 'params' array"); - // 3. body 内の If statement を検索 + // 3. body 内の If statement を検索(Phase 34-2/34-3 共通) let body = &func_def["body"]["body"]; let if_stmt = body .as_array() .and_then(|stmts| stmts.get(0)) - .expect("Phase 34-2: Function body must have at least one statement"); + .expect("Function body must have at least one statement"); assert_eq!( if_stmt["type"].as_str(), Some("If"), - "Phase 34-2: First statement must be If" + "First statement must be If" ); // 4. then/else の Return から値を抽出 @@ -181,134 +185,7 @@ impl AstToJoinIrLowerer { .map(|i| crate::mir::ValueId(i as u32)) .collect(), body: insts, - exit_cont: None, // Phase 34-2: ルート関数なので exit_cont は None - }; - - let mut functions = BTreeMap::new(); - functions.insert(func_id, func); - - JoinModule { - functions, - entry: Some(func_id), - } - } - - /// Local pattern lowering: `if cond { x=10 } else { x=20 }; return x` - /// - /// Phase 34-3: IfSelectTest.local 用の lowering - /// JoinIR 出力は simple と同じ(Select ベース) - fn lower_local_if(&mut self, program_json: &serde_json::Value) -> JoinModule { - // Phase 34-3: simple と同じロジック(JoinIR は同じため) - // 意味論的には「値としての if」を実証 - - // 1. Program(JSON) から defs を取得 - let defs = program_json["defs"] - .as_array() - .expect("Program(JSON v0) must have 'defs' array"); - - // 2. 最初の関数定義を取得(IfSelectTest.local 想定) - let func_def = defs - .get(0) - .expect("At least one function definition required"); - - let func_name = func_def["name"] - .as_str() - .expect("Function must have 'name'"); - - let params = func_def["params"] - .as_array() - .expect("Function must have 'params' array"); - - // 3. body 内の If statement を検索 - let body = &func_def["body"]["body"]; - let if_stmt = body - .as_array() - .and_then(|stmts| stmts.get(0)) - .expect("Function body must have at least one statement"); - - assert_eq!( - if_stmt["type"].as_str(), - Some("If"), - "First statement must be If" - ); - - // 4. then/else の Return から値を抽出 - let then_stmts = if_stmt["then"] - .as_array() - .expect("If must have 'then' array"); - let else_stmts = if_stmt["else"] - .as_array() - .expect("If must have 'else' array (local pattern)"); - - let then_ret = then_stmts - .get(0) - .expect("then branch must have Return"); - let else_ret = else_stmts - .get(0) - .expect("else branch must have Return"); - - assert_eq!( - then_ret["type"].as_str(), - Some("Return"), - "then branch must be Return" - ); - assert_eq!( - else_ret["type"].as_str(), - Some("Return"), - "else branch must be Return" - ); - - let then_val = self.extract_int_value(&then_ret["expr"]); - let else_val = self.extract_int_value(&else_ret["expr"]); - - // 5. JoinIR 組み立て(Const + Select + Ret) - let func_id = self.next_func_id(); - - // パラメータ: cond (VarId(0)) - let cond_var = crate::mir::ValueId(0); - - // パラメータ分の変数IDをスキップ(params.len() = 1) - self.next_var_id = params.len() as u32; - - // 定数変数(これで ValueId(1), ValueId(2) になる) - let then_var = self.next_var_id(); - let else_var = self.next_var_id(); - - // Select 結果変数 - let result_var = self.next_var_id(); - - let insts = vec![ - // Compute: then_var = Const(then_val) - JoinInst::Compute(crate::mir::join_ir::MirLikeInst::Const { - dst: then_var, - value: ConstValue::Integer(then_val), - }), - // Compute: else_var = Const(else_val) - JoinInst::Compute(crate::mir::join_ir::MirLikeInst::Const { - dst: else_var, - value: ConstValue::Integer(else_val), - }), - // Select: result = Select(cond, then_var, else_var) - JoinInst::Select { - dst: result_var, - cond: cond_var, - then_val: then_var, - else_val: else_var, - }, - // Ret result - JoinInst::Ret { - value: Some(result_var), - }, - ]; - - let func = JoinFunction { - id: func_id, - name: func_name.to_string(), - params: (0..params.len()) - .map(|i| crate::mir::ValueId(i as u32)) - .collect(), - body: insts, - exit_cont: None, // Phase 34-3: ルート関数なので exit_cont は None + exit_cont: None, // Phase 34-2/34-3: ルート関数なので exit_cont は None }; let mut functions = BTreeMap::new(); @@ -325,7 +202,7 @@ impl AstToJoinIrLowerer { assert_eq!( expr["type"].as_str(), Some("Int"), - "Phase 34-2: Only Int literals supported" + "Only Int literals supported" ); expr["value"]