fix(p4): math関数算術 & variadic min/max を正答化 (closes #21; refs #22,#43) - #34
Merged
Conversation
…#21; refs #22, #43) Adopts unlaxer-dsl 3.0.5, whose generated mapper now maps a function/conditional factor operand to its real AST node instead of dropping it. Adapts the P4 evaluators to the widened BinaryExpr operand type (base AST interface) and fixes the cross-check: - P4TypedAstEvaluator: evalBinaryAsNumber dispatches each operand via evalOperandAsNumber (BinaryExpr stays on the spine; AbsExpr/PowExpr/IfExpr/… go through eval), and the source-snippet path unwraps ExpressionExpr before its BinaryExpr guard to stop the infinite recursion that StackOverflowed on "abs(-3)+pow(2,3)". - P4DefaultJavaCodeEmitter / P4TypedJavaCodeEmitter: renderOperand dispatches operands to the same effect for the Java-code backends. - AstDeclarationRuntime: evaluate a declaration's main expression with the source-aware P4TypedAstEvaluator so "$price+2" stays on the typed path (was falling to the reflection evaluator and returning the setter value). - AstEvaluatorCalculator: the numeric cross-check no longer discards a correct P4-typed result for the buggy legacy token-AST (variadic min/max, boolean precedence). Now that P4-typed math is correct it keeps the P4 result and only records the divergence (#21). Verified: abs(-3)+pow(2,3)=11, min(3,5,1)=1, max(3,5,1,9)=9, sqrt(4)*5=10, declaration "$price+2"=5 — all P4-typed; parity/corpus suites green, no regressions. Resolves the #22 javacode-math gap; the remaining #22 items (.in semantics, double-quote dot methods, method-arg scope) are not addressed here. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…e-text splitter Now that the mapper maps StringComparisonExpr.left/right to the correct operand nodes (unlaxer-dsl 3.0.5, findDescendants rule-level fix), evalStringComparisonExpr evaluates node.left()/node.right() directly instead of re-splitting the source formula at the top-level ==/!= by hand. Removes the hand-rolled char-scanning workaround that this obsoletes: tryExtractStringComparisonFromSourceFormula / splitTopLevelStringComparison / matchesComparisonOperator / resolveStringComparisonSide / tryEvaluateStringSourceSnippet and the StringComparisonSource record (~130 lines). The combinator-generated AST is the single source of truth; consumers no longer reparse the source string. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jun 25, 2026
… from AST (#35) The post-#44 mapper maps every arithmetic operand to a real AST node (mapAssocOperandToBinaryExpr), so BinaryExpr.left()/op()/right() is now a faithful tree. The source-text re-parse shadows that preceded the AST walk were therefore dead compensation: - P4TypedAstEvaluator: remove tryEvaluateStructuredBinaryNode / tryEvaluateStructuredBinarySourceSnippet / hasStructuredNumericAlternative - P4TypedJavaCodeEmitter / P4DefaultJavaCodeEmitter: remove renderStructuredBinaryNode / renderStructuredBinarySourceSnippet / hasStructuredNumericAlternative evalBinaryExpr / evalBinaryAsNumber now go straight to the AST walk. The slice path keeps renderNumericSourceSnippet for now (separate #35 cycle). Verified: full suite 630 tests, baseline gate green (no new failures), legacy-fallback observations 171->170. -120 LOC. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Both AstEvaluatorCalculatorTest#testTypeInference and P4AstEvaluatorCalculatorTest#testTypeInference pass consistently across three full runs after the 3.0.5 mapper fix + numeric-leaf cycle. The baseline gate flagged them as FIXED; tightening it so a future regression in type inference fails CI again. Only the double-parens-ternary parse case (exponential backtracking) remains known-failing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Uh oh!
There was an error while loading. Please reload this page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
P4_AST_EVALUATOR で
abs(-3)+pow(2,3)が StackOverflow→誤値、min(3,5,1)/max(3,5,1,9)が cross-check により 誤legacy値(3/5)で上書き されていた問題を修正。根因は unlaxer-dsl の生成マッパーが算術コンテキストの関数項を脱落させていたこと(unlaxer-parser #43 / PR #44 で根治)。本PRは unlaxer-dsl 3.0.5 を採用し、widメンされた BinaryExpr オペランド型に評価器を追従させ、cross-check を是正する。
変更
P4TypedAstEvaluator:evalBinaryAsNumberがオペランドをevalOperandAsNumberでディスパッチ(BinaryExprはスパイン継続、AbsExpr/PowExpr/IfExpr等はeval)。source-snippet経路は ExpressionExpr を unwrap してから BinaryExpr ガードに入れ、abs(-3)+pow(2,3)の無限再帰(StackOverflow)を解消。P4DefaultJavaCodeEmitter/P4TypedJavaCodeEmitter:renderOperandで同様にディスパッチ。AstDeclarationRuntime: 宣言式の主式を source-aware な P4TypedAstEvaluator で評価($price+2がreflection経路に落ちて setter値を返していた回帰を修正)。AstEvaluatorCalculator: 数値 cross-check が正しいP4結果を壊れたlegacy(variadic min/max・boolean優先順位)で上書きしないよう、P4を信頼し差分は観測markerのみ記録 (cross-check が正しいP4結果を壊れたlegacyで上書きする (variadic min/max・boolean優先順位・関数項脱落) #21)。検証
abs(-3)+pow(2,3)=11,min(3,5,1)=1,max(3,5,1,9)=9,sqrt(4)*5=10, 宣言式$price+2=5— 全て p4-typed で正答。パリティ/コーパス各スイート緑、回帰ゼロ。unlaxer-dsl 3.0.5 の公開が必要(依存)。unlaxer-parser PR #44 をマージ& Maven Central へ 3.0.5 をデプロイ後でないと、本PRのCIは依存解決に失敗します。
未対応(#22 残)
.in()意味バグ(常時true)、ダブルクォート+ドットメソッド parse、メソッド引数スコープ。#43 case3(variadic mapper rest-capture, 無害) も将来。🤖 Generated with Claude Code