From e20765beef85e1184ad03eb5740b3977334efc03 Mon Sep 17 00:00:00 2001 From: Octavia Togami Date: Tue, 25 Feb 2020 16:20:21 -0800 Subject: [PATCH] Fix pre- and post- ops (cherry picked from commit 0f787a89b8f2b0f29e0e4a2327224a3b0d050171) --- .../internal/expression/invoke/CompilingVisitor.java | 6 +++--- .../worldedit/internal/expression/ExpressionTest.java | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/expression/invoke/CompilingVisitor.java b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/expression/invoke/CompilingVisitor.java index 91bea6009..16b12d738 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/internal/expression/invoke/CompilingVisitor.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/internal/expression/invoke/CompilingVisitor.java @@ -269,13 +269,14 @@ class CompilingVisitor extends ExpressionBaseVisitor { return ExpressionHandles.call(data -> { LocalSlot.Variable variable = ExpressionHandles.getVariable(data, target); double value = variable.getValue(); + double result = value; if (opType == INCREMENT) { value++; } else { value--; } variable.setValue(value); - return value; + return result; }); } @@ -286,14 +287,13 @@ class CompilingVisitor extends ExpressionBaseVisitor { return ExpressionHandles.call(data -> { LocalSlot.Variable variable = ExpressionHandles.getVariable(data, target); double value = variable.getValue(); - double result = value; if (opType == INCREMENT) { value++; } else { value--; } variable.setValue(value); - return result; + return value; }); } diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java index 8508a2640..985d06c20 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java @@ -84,6 +84,14 @@ class ExpressionTest extends BaseExpressionTest { checkTestCase("3+1", 4); } + @Test + void testPostPreOps() { + checkTestCase("a=0; b=a++; a+b", 1); + checkTestCase("a=0; b=++a; a+b", 2); + checkTestCase("a=0; b=a--; a+b", -1); + checkTestCase("a=0; b=--a; a+b", -2); + } + @Test public void testErrors() { // test lexer errors