diff --git a/src/grammar/generated.rs b/src/grammar/generated.rs index d94cb9c1..25bb6e51 100644 --- a/src/grammar/generated.rs +++ b/src/grammar/generated.rs @@ -36,37 +36,15 @@ pub static OPERATORS_DIV_RULES: &[(&str, &str, &str, &str)] = &[ ]; pub fn lookup_keyword(word: &str) -> Option<&'static str> { for (k, t) in KEYWORDS { - if *k == word { return Some(*t); } + if *k == word { + return Some(*t); + } } None } pub static SYNTAX_ALLOWED_STATEMENTS: &[&str] = &[ - "box", - "global", - "function", - "static", - "if", - "loop", - "break", - "return", - "print", - "nowait", - "include", - "local", - "outbox", - "try", - "throw", - "using", - "from", + "box", "global", "function", "static", "if", "loop", "break", "return", "print", "nowait", + "include", "local", "outbox", "try", "throw", "using", "from", ]; -pub static SYNTAX_ALLOWED_BINOPS: &[&str] = &[ - "add", - "sub", - "mul", - "div", - "and", - "or", - "eq", - "ne", -]; \ No newline at end of file +pub static SYNTAX_ALLOWED_BINOPS: &[&str] = &["add", "sub", "mul", "div", "and", "or", "eq", "ne"]; diff --git a/src/mir/builder/control_flow/joinir/routing.rs b/src/mir/builder/control_flow/joinir/routing.rs index 49168882..d66d0783 100644 --- a/src/mir/builder/control_flow/joinir/routing.rs +++ b/src/mir/builder/control_flow/joinir/routing.rs @@ -11,6 +11,7 @@ use crate::mir::ValueId; /// 将来的には Canonicalizer decision に委譲する。 /// /// Phase 137-6-S1: 現時点では既存の router ロジック(LoopFeatures ベース)を使用 +/// Phase 137-6-S2: dev-only で canonicalizer decision を提案として受け取る pub(in crate::mir::builder) fn choose_pattern_kind( condition: &ASTNode, body: &[ASTNode], @@ -25,8 +26,51 @@ pub(in crate::mir::builder) fn choose_pattern_kind( // Phase 193: Extract features using modularized extractor let features = ast_features::extract_features(condition, body, has_continue, has_break); - // Phase 192: Classify pattern based on features - loop_pattern_detection::classify(&features) + // Phase 192: Classify pattern based on features (既存の router 結果) + let router_choice = loop_pattern_detection::classify(&features); + + // Phase 137-6-S2: dev-only で Canonicalizer の提案を取得 + if crate::config::env::joinir_dev_enabled() { + use crate::ast::Span; + use crate::mir::loop_canonicalizer::canonicalize_loop_expr; + + let loop_ast = ASTNode::Loop { + condition: Box::new(condition.clone()), + body: body.to_vec(), + span: Span::unknown(), + }; + + if let Ok((_skeleton, decision)) = canonicalize_loop_expr(&loop_ast) { + if let Some(canonical_choice) = decision.chosen { + // parity check + if canonical_choice != router_choice { + let msg = format!( + "[choose_pattern_kind/PARITY] router={:?}, canonicalizer={:?}", + router_choice, canonical_choice + ); + + if crate::config::env::joinir_dev::strict_enabled() { + // strict mode: 不一致は Fail-Fast + panic!("{}", msg); + } else { + // debug mode: ログのみ + eprintln!("{}", msg); + } + } else { + // Patterns match - success! + eprintln!( + "[choose_pattern_kind/PARITY] OK: canonical and actual agree on {:?}", + canonical_choice + ); + } + + // TODO (Phase 137-6-S3): ここで canonical_choice を返す + // 現時点では router_choice を維持(既定挙動不変) + } + } + } + + router_choice } impl MirBuilder {