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

@ -40,8 +40,15 @@ fn assignment(target: ASTNode, value: ASTNode) -> ASTNode {
fn has_continue(node: &ASTNode) -> bool {
match node {
ASTNode::Continue { .. } => true,
ASTNode::If { then_body, else_body, .. } => {
then_body.iter().any(has_continue) || else_body.as_ref().map_or(false, |b| b.iter().any(has_continue))
ASTNode::If {
then_body,
else_body,
..
} => {
then_body.iter().any(has_continue)
|| else_body
.as_ref()
.map_or(false, |b| b.iter().any(has_continue))
}
ASTNode::Loop { body, .. } => body.iter().any(has_continue),
_ => false,
@ -51,8 +58,15 @@ fn has_continue(node: &ASTNode) -> bool {
fn has_break(node: &ASTNode) -> bool {
match node {
ASTNode::Break { .. } => true,
ASTNode::If { then_body, else_body, .. } => {
then_body.iter().any(has_break) || else_body.as_ref().map_or(false, |b| b.iter().any(has_break))
ASTNode::If {
then_body,
else_body,
..
} => {
then_body.iter().any(has_break)
|| else_body
.as_ref()
.map_or(false, |b| b.iter().any(has_break))
}
ASTNode::Loop { body, .. } => body.iter().any(has_break),
_ => false,
@ -69,7 +83,11 @@ fn carrier_count(body: &[ASTNode]) -> usize {
for n in nodes {
match n {
ASTNode::Assignment { .. } => c += 1,
ASTNode::If { then_body, else_body, .. } => {
ASTNode::If {
then_body,
else_body,
..
} => {
c += count(then_body);
if let Some(else_body) = else_body {
c += count(else_body);
@ -80,7 +98,11 @@ fn carrier_count(body: &[ASTNode]) -> usize {
}
c
}
if count(body) > 0 { 1 } else { 0 }
if count(body) > 0 {
1
} else {
0
}
}
fn classify_body(body: &[ASTNode]) -> LoopPatternKind {
@ -102,7 +124,10 @@ fn classify_body(body: &[ASTNode]) -> LoopPatternKind {
#[test]
fn pattern1_simple_while_is_detected() {
// loop(i < len) { i = i + 1 }
let body = vec![assignment(var("i"), bin(BinaryOperator::Add, var("i"), lit_i(1)))];
let body = vec![assignment(
var("i"),
bin(BinaryOperator::Add, var("i"), lit_i(1)),
)];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern1SimpleWhile);
}
@ -211,7 +236,7 @@ fn test_atoi_loop_classified_as_pattern2() {
let mul_expr = bin(BinaryOperator::Multiply, var("result"), lit_i(10));
let result_update = assignment(
var("result"),
bin(BinaryOperator::Add, mul_expr, var("digit_pos"))
bin(BinaryOperator::Add, mul_expr, var("digit_pos")),
);
// i = i + 1
@ -229,6 +254,9 @@ fn test_atoi_loop_classified_as_pattern2() {
];
let kind = classify_body(&body);
assert_eq!(kind, LoopPatternKind::Pattern2Break,
"_atoi loop should be classified as Pattern2 (Break) due to if-break structure");
assert_eq!(
kind,
LoopPatternKind::Pattern2Break,
"_atoi loop should be classified as Pattern2 (Break) due to if-break structure"
);
}