diff --git a/rocky.jar b/rocky.jar index 51fcd42..6502fa0 100644 Binary files a/rocky.jar and b/rocky.jar differ diff --git a/src/rockstar/expression/NotExpression.java b/src/rockstar/expression/NotExpression.java index f3bb930..83c74a4 100644 --- a/src/rockstar/expression/NotExpression.java +++ b/src/rockstar/expression/NotExpression.java @@ -33,7 +33,7 @@ public Value evaluate(BlockContext ctx) { ctx.beforeExpression(this); Expression expr = this.getParameters().get(0); Value v = expr.evaluate(ctx); - return ctx.afterExpression(this, v.negate()); + return ctx.afterExpression(this, v.negateBoolean()); } @Override diff --git a/src/rockstar/expression/UnaryMinusExpression.java b/src/rockstar/expression/UnaryMinusExpression.java index df961fc..d82b12b 100644 --- a/src/rockstar/expression/UnaryMinusExpression.java +++ b/src/rockstar/expression/UnaryMinusExpression.java @@ -33,7 +33,7 @@ public Value evaluate(BlockContext ctx) { ctx.beforeExpression(this); Expression expr1 = this.getParameters().get(0); Value v1 = expr1.evaluate(ctx); - return ctx.afterExpression(this, Value.NULL.minus(v1)); + return ctx.afterExpression(this, v1.negateNumeric()); } @Override diff --git a/src/rockstar/runtime/RockNumber.java b/src/rockstar/runtime/RockNumber.java index bb2c216..ed18546 100644 --- a/src/rockstar/runtime/RockNumber.java +++ b/src/rockstar/runtime/RockNumber.java @@ -90,4 +90,6 @@ public static RockNumber getValueFromLong(long l) { public abstract RockNumber floor(); public abstract RockNumber round(); + protected abstract RockNumber negate(); + } diff --git a/src/rockstar/runtime/RockNumberBigDecimal.java b/src/rockstar/runtime/RockNumberBigDecimal.java index feecf94..fba2f60 100644 --- a/src/rockstar/runtime/RockNumberBigDecimal.java +++ b/src/rockstar/runtime/RockNumberBigDecimal.java @@ -96,6 +96,7 @@ public boolean isNegative() { return value.compareTo(ZERO.value) < 0; } + @Override public RockNumberBigDecimal negate() { return create(value.negate()); } diff --git a/src/rockstar/runtime/RockNumberDec64.java b/src/rockstar/runtime/RockNumberDec64.java index b267497..0d40481 100644 --- a/src/rockstar/runtime/RockNumberDec64.java +++ b/src/rockstar/runtime/RockNumberDec64.java @@ -255,6 +255,7 @@ public boolean isNegative() { return mantissa>=0; } + @Override public RockNumberDec64 negate() { return getFromCache(-mantissa, exponent); } diff --git a/src/rockstar/runtime/RockNumberDouble.java b/src/rockstar/runtime/RockNumberDouble.java index fdec8e3..4164f6f 100644 --- a/src/rockstar/runtime/RockNumberDouble.java +++ b/src/rockstar/runtime/RockNumberDouble.java @@ -16,6 +16,9 @@ public class RockNumberDouble extends RockNumber { public final Double dblValue; private RockNumberDouble(Double dblValue) { + if (dblValue != null && dblValue == -0.0) { + dblValue = 0.0; + } this.dblValue = dblValue; } @@ -94,6 +97,11 @@ public RockNumberDouble multiply(RockNumber rn) { public RockNumberDouble divide(RockNumber rn) { return new RockNumberDouble(dblValue / convertToDbl(rn)); } + + @Override + protected RockNumber negate() { + return new RockNumberDouble(-dblValue); + } @Override public int asInt() { @@ -164,6 +172,5 @@ public RockNumber floor() { public RockNumber round() { return new RockNumberDouble((double)Math.round(dblValue)); } - } diff --git a/src/rockstar/runtime/Value.java b/src/rockstar/runtime/Value.java index d7020f1..e0c4c6f 100644 --- a/src/rockstar/runtime/Value.java +++ b/src/rockstar/runtime/Value.java @@ -381,9 +381,17 @@ public String describe() { return null; } - public Value negate() { - // bool negation - return getBool() ? BOOLEAN_FALSE : BOOLEAN_TRUE; + public Value negateBoolean() { + return getBool() ? BOOLEAN_FALSE : BOOLEAN_TRUE; + } + + public Value negateNumeric() { + switch (this.type) { + case NUMBER: + return Value.getValue(getNumeric().negate()); + default: + throw new RockstarRuntimeException("Cannot negate "+this.getType()+" type"); + } } public Value plus(Value other) { diff --git a/src/rockstar/statement/DecrementStatement.java b/src/rockstar/statement/DecrementStatement.java index edbfd5a..9e1c8f8 100644 --- a/src/rockstar/statement/DecrementStatement.java +++ b/src/rockstar/statement/DecrementStatement.java @@ -58,7 +58,7 @@ public void execute(BlockContext ctx) { v = v.asBoolean(); if (count % 2 == 1) { // negate boolean - v = v.negate(); + v = v.negateBoolean(); } ctx.setVariable(variable, v); return; diff --git a/src/rockstar/statement/IncrementStatement.java b/src/rockstar/statement/IncrementStatement.java index 1ddaead..1c8b3a8 100644 --- a/src/rockstar/statement/IncrementStatement.java +++ b/src/rockstar/statement/IncrementStatement.java @@ -58,7 +58,7 @@ public void execute(BlockContext ctx) { v = v.asBoolean(); if (count % 2 == 1) { // negate boolean - v = v.negate(); + v = v.negateBoolean(); } ctx.setVariable(variable, v); return;