Fix double unary minus (#697)

Fix an issue where doubly unary minus (e.g. `--1`) on int/float literals are incorrectly
parsed as single unary minus.
This commit is contained in:
Josh B
2024-10-17 07:30:00 -07:00
committed by GitHub
parent 9b5c5a5c98
commit 62c796a257
5 changed files with 16 additions and 4 deletions

View File

@@ -1869,12 +1869,12 @@ public final class AstBuilder extends AbstractAstBuilder<Object> {
@Override
public ExpressionNode visitUnaryMinusExpr(UnaryMinusExprContext ctx) {
var childExpr = visitExpr(ctx.expr());
if (childExpr instanceof IntLiteralNode || childExpr instanceof FloatLiteralNode) {
// negation already handled in child expr (see corresponding code)
var childCtx = ctx.expr();
var childExpr = visitExpr(childCtx);
if (childCtx instanceof IntLiteralContext || childCtx instanceof FloatLiteralContext) {
// negation already handled (see visitIntLiteral/visitFloatLiteral)
return childExpr;
}
return UnaryMinusNodeGen.create(createSourceSection(ctx), childExpr);
}

View File

@@ -141,6 +141,10 @@ examples {
-1.2.abs
-x.abs
// https://github.com/apple/pkl/issues/650
--1.0
-(-1.0)
}
["power"] {

View File

@@ -163,6 +163,10 @@ examples {
-0b11.abs
local x = 4
-x.abs
// https://github.com/apple/pkl/issues/650
--1
-(-1)
}
["power"] {

View File

@@ -98,6 +98,8 @@ examples {
["negation"] {
-1.2
-1.2
1.0
1.0
}
["power"] {
27.98409999999999

View File

@@ -121,6 +121,8 @@ examples {
-2
-3
-4
1
1
}
["power"] {
16