Phase 33 NORM canon test: enforce normalized dev route for P1/P2/JP mini

This commit is contained in:
nyash-codex
2025-12-11 20:54:33 +09:00
parent 59a985b7fa
commit af6f95cd4b
170 changed files with 4423 additions and 1897 deletions

View File

@ -15,12 +15,12 @@
//! **Fail-Safe**: Unsupported AST nodes return explicit errors, allowing callers
//! to fall back to legacy paths.
use crate::ast::ASTNode;
use crate::mir::ValueId;
use crate::mir::join_ir::JoinInst;
use crate::mir::builder::MirBuilder;
use super::scope_manager::ScopeManager;
use super::condition_lowerer::lower_condition_to_joinir;
use super::scope_manager::ScopeManager;
use crate::ast::ASTNode;
use crate::mir::builder::MirBuilder;
use crate::mir::join_ir::JoinInst;
use crate::mir::ValueId;
mod ast_support;
mod scope_resolution;
@ -165,11 +165,9 @@ impl<'env, 'builder, S: ScopeManager> ExprLowerer<'env, 'builder, S> {
pub fn lower(&mut self, ast: &ASTNode) -> Result<ValueId, ExprLoweringError> {
match self.context {
ExprContext::Condition => self.lower_condition(ast),
ExprContext::General => {
Err(ExprLoweringError::UnsupportedNode(
"General expression context not yet implemented (Phase 231)".to_string()
))
}
ExprContext::General => Err(ExprLoweringError::UnsupportedNode(
"General expression context not yet implemented (Phase 231)".to_string(),
)),
}
}
@ -185,9 +183,10 @@ impl<'env, 'builder, S: ScopeManager> ExprLowerer<'env, 'builder, S> {
fn lower_condition(&mut self, ast: &ASTNode) -> Result<ValueId, ExprLoweringError> {
// 1. Check if AST is supported in condition context
if !ast_support::is_supported_condition(ast) {
return Err(ExprLoweringError::UnsupportedNode(
format!("Unsupported condition node: {:?}", ast)
));
return Err(ExprLoweringError::UnsupportedNode(format!(
"Unsupported condition node: {:?}",
ast
)));
}
// 2. Build ConditionEnv from ScopeManager
@ -204,17 +203,18 @@ impl<'env, 'builder, S: ScopeManager> ExprLowerer<'env, 'builder, S> {
id
};
let (result_value, instructions) = lower_condition_to_joinir(
ast,
&mut alloc_value,
&condition_env,
).map_err(|e| ExprLoweringError::LoweringError(e))?;
let (result_value, instructions) =
lower_condition_to_joinir(ast, &mut alloc_value, &condition_env)
.map_err(|e| ExprLoweringError::LoweringError(e))?;
// Phase 235: 保存しておき、テストから観察できるようにする
self.last_instructions = instructions;
if self.debug {
eprintln!("[expr_lowerer/phase231] Lowered condition → ValueId({:?})", result_value);
eprintln!(
"[expr_lowerer/phase231] Lowered condition → ValueId({:?})",
result_value
);
}
Ok(result_value)
@ -230,7 +230,7 @@ impl<'env, 'builder, S: ScopeManager> ExprLowerer<'env, 'builder, S> {
// Phase 244: ConditionLoweringBox trait implementation
// ============================================================================
use super::condition_lowering_box::{ConditionLoweringBox, ConditionContext};
use super::condition_lowering_box::{ConditionContext, ConditionLoweringBox};
impl<'env, 'builder, S: ScopeManager> ConditionLoweringBox<S> for ExprLowerer<'env, 'builder, S> {
/// Phase 244: Implement ConditionLoweringBox trait for ExprLowerer
@ -280,13 +280,13 @@ impl<'env, 'builder, S: ScopeManager> ConditionLoweringBox<S> for ExprLowerer<'e
#[cfg(test)]
mod tests {
use super::test_helpers::{bin, lit_i, span, var};
use super::*;
use crate::ast::{BinaryOperator, LiteralValue, Span, UnaryOperator};
use crate::mir::join_ir::lowering::carrier_info::CarrierInfo;
use crate::mir::join_ir::lowering::condition_env::ConditionEnv;
use crate::mir::join_ir::lowering::scope_manager::Pattern2ScopeManager;
use crate::mir::join_ir::{BinOpKind, MirLikeInst, UnaryOp as JoinUnaryOp};
use super::test_helpers::{bin, lit_i, span, var};
// Helper to create a test MirBuilder (Phase 231: minimal stub)
fn create_test_builder() -> MirBuilder {
@ -340,7 +340,10 @@ mod tests {
let mut expr_lowerer = ExprLowerer::new(&scope, ExprContext::Condition, &mut builder);
let result = expr_lowerer.lower(&ast);
assert!(result.is_ok(), "Should lower simple comparison successfully");
assert!(
result.is_ok(),
"Should lower simple comparison successfully"
);
}
#[test]
@ -381,7 +384,10 @@ mod tests {
let mut expr_lowerer = ExprLowerer::new(&scope, ExprContext::Condition, &mut builder);
let result = expr_lowerer.lower(&ast);
assert!(matches!(result, Err(ExprLoweringError::VariableNotFound(_))));
assert!(matches!(
result,
Err(ExprLoweringError::VariableNotFound(_))
));
}
#[test]
@ -406,7 +412,9 @@ mod tests {
let mut builder = create_test_builder();
// AST: Break (unsupported in condition context)
let ast = ASTNode::Break { span: Span::unknown() };
let ast = ASTNode::Break {
span: Span::unknown(),
};
let mut expr_lowerer = ExprLowerer::new(&scope, ExprContext::Condition, &mut builder);
let result = expr_lowerer.lower(&ast);
@ -429,7 +437,9 @@ mod tests {
}),
span: Span::unknown(),
};
assert!(ExprLowerer::<Pattern2ScopeManager>::is_supported_condition(&ast));
assert!(ExprLowerer::<Pattern2ScopeManager>::is_supported_condition(
&ast
));
// Supported: MethodCall
let ast = ASTNode::MethodCall {
@ -441,10 +451,14 @@ mod tests {
arguments: vec![],
span: Span::unknown(),
};
assert!(ExprLowerer::<Pattern2ScopeManager>::is_supported_condition(&ast));
assert!(ExprLowerer::<Pattern2ScopeManager>::is_supported_condition(
&ast
));
// Unsupported: Break node
let ast = ASTNode::Break { span: Span::unknown() };
let ast = ASTNode::Break {
span: Span::unknown(),
};
assert!(!ExprLowerer::<Pattern2ScopeManager>::is_supported_condition(&ast));
}
@ -507,10 +521,9 @@ mod tests {
fn assert_has_compare(instructions: &[JoinInst]) {
assert!(
instructions.iter().any(|inst| matches!(
inst,
JoinInst::Compute(MirLikeInst::Compare { .. }
))),
instructions
.iter()
.any(|inst| matches!(inst, JoinInst::Compute(MirLikeInst::Compare { .. }))),
"Expected at least one Compare instruction, got {:?}",
instructions
);
@ -532,8 +545,11 @@ mod tests {
assert!(
instructions.iter().any(|inst| matches!(
inst,
JoinInst::Compute(MirLikeInst::UnaryOp { op: JoinUnaryOp::Not, .. }
))),
JoinInst::Compute(MirLikeInst::UnaryOp {
op: JoinUnaryOp::Not,
..
})
)),
"Expected at least one UnaryOp::Not, got {:?}",
instructions
);