From 898a876bf83ccd225b79da8f5b62674f05937083 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sun, 8 Nov 2026 14:48:18 +0000 Subject: [PATCH] Fix negative coefficient parsing for terms like -2x Tokens ending in x that started with '-' were always turned into Const(-1) * x, discarding digits. Parse the coefficient text instead, keeping Const(-1) only when the text is exactly "-". Co-authored-by: Barry G Becker --- .../mathexpression/MathExpressionParser.scala | 7 +++++-- .../expression/ExprEvaluatorSuite.scala | 14 ++++++++++++++ .../mathexpression/MathExpressionParserSuite.scala | 9 +++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/scala-source/com/barrybecker4/expression/mathexpression/MathExpressionParser.scala b/scala-source/com/barrybecker4/expression/mathexpression/MathExpressionParser.scala index d94dc27..438bf81 100644 --- a/scala-source/com/barrybecker4/expression/mathexpression/MathExpressionParser.scala +++ b/scala-source/com/barrybecker4/expression/mathexpression/MathExpressionParser.scala @@ -75,8 +75,11 @@ class MathExpressionParser(opDef: OperatorsDefinition = new MathOperatorsDefinit val len = token.length if (token.charAt(len - 1) == 'x') { if (len > 1) { - if (token.charAt(0) == LexToken.Minus.symbol) nodes.append(PlOperand(Const(-1))) - else nodes.append(PlOperand(numberExpr(token.substring(0, len - 1), exp, errPos))) + val coefficient = token.substring(0, len - 1) + val coef = + if (coefficient == "" + LexToken.Minus.symbol) Const(-1) + else numberExpr(coefficient, exp, errPos) + nodes.append(PlOperand(coef)) nodes.append(PlOperator(TIMES)) } nodes.append(PlOperand(X)) diff --git a/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala b/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala index 83cc1a5..141847a 100644 --- a/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala +++ b/scala-test/com/barrybecker4/expression/ExprEvaluatorSuite.scala @@ -12,6 +12,20 @@ class ExprEvaluatorSuite extends AnyFunSuite { assert(5.0 === ExprEvaluator.eval(expr, 2.0)) } + test("-2x keeps the coefficient") { + assert(-6.0 === ExprEvaluator.eval(parser.parse("-2x"), 3.0)) + } + + test("-2.5x and -.5x keep the coefficient") { + assert(-5.0 === ExprEvaluator.eval(parser.parse("-2.5x"), 2.0)) + assert(-1.5 === ExprEvaluator.eval(parser.parse("-.5x"), 3.0)) + } + + test("explicit star and parentheses already keep a negative coefficient") { + assert(-6.0 === ExprEvaluator.eval(parser.parse("-2*x"), 3.0)) + assert(-6.0 === ExprEvaluator.eval(parser.parse("(-2)x"), 3.0)) + } + test("eval unwraps Group") { val expr = BinOp(PLUS, Group(Const(1.0)), Const(2.0)) assert(3.0 === ExprEvaluator.eval(expr, 99.0)) diff --git a/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala b/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala index 672fcd9..46f2d9b 100644 --- a/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala +++ b/scala-test/com/barrybecker4/expression/mathexpression/MathExpressionParserSuite.scala @@ -29,6 +29,15 @@ class MathExpressionParserSuite extends AnyFunSuite { verifyParse("-x", "-1 * x") } + test("-2x keeps the coefficient") { + verifyParse("-2x", "-2.0 * x") + } + + test("-2.5x and -.5x keep the coefficient") { + verifyParse("-2.5x", "-2.5 * x") + verifyParse("-.5x", "-0.5 * x") + } + test("ConstantIntOnlyExp(") { verifyParse("5", "5.0") }