Fix unary minus operators

This commit is contained in:
Edwin Jakobs
2020-04-21 11:46:01 +02:00
parent dafd309924
commit 6a29853c71
2 changed files with 88 additions and 84 deletions

View File

@@ -5,10 +5,7 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker
import org.antlr.v4.runtime.tree.TerminalNode import org.antlr.v4.runtime.tree.TerminalNode
import org.openrndr.extra.keyframer.antlr.* import org.openrndr.extra.keyframer.antlr.*
import org.openrndr.extra.noise.uniform import org.openrndr.extra.noise.uniform
import org.openrndr.math.map import org.openrndr.math.*
import org.openrndr.math.mix
import org.openrndr.math.mod
import org.openrndr.math.smoothstep
import java.util.* import java.util.*
import kotlin.math.* import kotlin.math.*
@@ -68,7 +65,8 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
} }
override fun exitMinusExpression(ctx: KeyLangParser.MinusExpressionContext) { override fun exitMinusExpression(ctx: KeyLangParser.MinusExpressionContext) {
super.exitMinusExpression(ctx) val op = doubleStack.pop()
doubleStack.push(-op)
} }
override fun exitBinaryOperation1(ctx: KeyLangParser.BinaryOperation1Context) { override fun exitBinaryOperation1(ctx: KeyLangParser.BinaryOperation1Context) {
@@ -302,6 +300,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
"pow" -> { x -> x[0].pow(x[1]) } "pow" -> { x -> x[0].pow(x[1]) }
"atan2" -> { x -> atan2(x[0], x[1]) } "atan2" -> { x -> atan2(x[0], x[1]) }
"random" -> { x -> Double.uniform(x[0], x[1]) } "random" -> { x -> Double.uniform(x[0], x[1]) }
"length" -> { x -> Vector2(x[0], x[1]).length }
else -> functions.functions2[candidate]?.let { { x: DoubleArray -> it.invoke(x[0], x[1]) } } else -> functions.functions2[candidate]?.let { { x: DoubleArray -> it.invoke(x[0], x[1]) } }
?: errorValue( ?: errorValue(
"unresolved function: '${candidate}(x0, x1)'" "unresolved function: '${candidate}(x0, x1)'"
@@ -314,6 +313,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
when (val candidate = node.text) { when (val candidate = node.text) {
"mix" -> { x -> mix(x[0], x[1], x[2]) } "mix" -> { x -> mix(x[0], x[1], x[2]) }
"smoothstep" -> { x -> smoothstep(x[0], x[1], x[2]) } "smoothstep" -> { x -> smoothstep(x[0], x[1], x[2]) }
"length" -> { x -> Vector3(x[0], x[1], x[2]).length }
else -> functions.functions3[candidate]?.let { { x: DoubleArray -> it.invoke(x[0], x[1], x[2]) } } else -> functions.functions3[candidate]?.let { { x: DoubleArray -> it.invoke(x[0], x[1], x[2]) } }
?: errorValue( ?: errorValue(
"unresolved function: '${candidate}(x0, x1, x2)'" "unresolved function: '${candidate}(x0, x1, x2)'"

View File

@@ -32,4 +32,8 @@ object TestOperators : Spek({
val result = evaluateExpression("4 + 2 * 3") val result = evaluateExpression("4 + 2 * 3")
result?.shouldBeNear(10.0, 10E-6) result?.shouldBeNear(10.0, 10E-6)
} }
describe("unary minus") {
val result = evaluateExpression("-4.0")
result?.shouldBeNear(-4.0, 10E-6)
}
}) })