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.openrndr.extra.keyframer.antlr.*
import org.openrndr.extra.noise.uniform
import org.openrndr.math.map
import org.openrndr.math.mix
import org.openrndr.math.mod
import org.openrndr.math.smoothstep
import org.openrndr.math.*
import java.util.*
import kotlin.math.*
@@ -68,7 +65,8 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
}
override fun exitMinusExpression(ctx: KeyLangParser.MinusExpressionContext) {
super.exitMinusExpression(ctx)
val op = doubleStack.pop()
doubleStack.push(-op)
}
override fun exitBinaryOperation1(ctx: KeyLangParser.BinaryOperation1Context) {
@@ -262,7 +260,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
val function: (DoubleArray) -> Double =
when (val candidate = node.text) {
"random" -> { _ -> Double.uniform(0.0, 1.0) }
else -> functions.functions0[candidate]?.let { { _:DoubleArray -> it.invoke() } }
else -> functions.functions0[candidate]?.let { { _: DoubleArray -> it.invoke() } }
?: errorValue(
"unresolved function: '${candidate}()'"
) { _ -> error("this is the error function") }
@@ -287,7 +285,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
"floor" -> { x -> floor(x[0]) }
"ceil" -> { x -> ceil(x[0]) }
"saturate" -> { x -> x[0].coerceIn(0.0, 1.0) }
else -> functions.functions1[candidate]?.let { { x:DoubleArray -> it.invoke(x[0]) } }
else -> functions.functions1[candidate]?.let { { x: DoubleArray -> it.invoke(x[0]) } }
?: errorValue(
"unresolved function: '${candidate}(x0)'"
) { _ -> error("this is the error function") }
@@ -302,7 +300,8 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
"pow" -> { x -> x[0].pow(x[1]) }
"atan2" -> { x -> atan2(x[0], x[1]) }
"random" -> { x -> Double.uniform(x[0], x[1]) }
else -> functions.functions2[candidate]?.let { { x:DoubleArray -> it.invoke(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]) } }
?: errorValue(
"unresolved function: '${candidate}(x0, x1)'"
) { _ -> error("this is the error function") }
@@ -314,7 +313,8 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
when (val candidate = node.text) {
"mix" -> { x -> mix(x[0], x[1], x[2]) }
"smoothstep" -> { x -> smoothstep(x[0], x[1], x[2]) }
else -> functions.functions3[candidate]?.let { { x:DoubleArray -> it.invoke(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]) } }
?: errorValue(
"unresolved function: '${candidate}(x0, x1, x2)'"
) { _ -> error("this is the error function") }
@@ -324,7 +324,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
IDType.FUNCTION4 -> {
val function: (DoubleArray) -> Double =
when (val candidate = node.text) {
else -> functions.functions4[candidate]?.let { { x:DoubleArray -> it.invoke(x[0], x[1], x[2], x[3]) } }
else -> functions.functions4[candidate]?.let { { x: DoubleArray -> it.invoke(x[0], x[1], x[2], x[3]) } }
?: errorValue(
"unresolved function: '${candidate}(x0, x1, x2, x3)'"
) { _ -> error("this is the error function") }
@@ -336,7 +336,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
val function: (DoubleArray) -> Double =
when (val candidate = node.text) {
"map" -> { x -> map(x[0], x[1], x[2], x[3], x[4]) }
else -> functions.functions5[candidate]?.let { { x:DoubleArray -> it.invoke(x[0], x[1], x[2], x[3], x[4]) } }
else -> functions.functions5[candidate]?.let { { x: DoubleArray -> it.invoke(x[0], x[1], x[2], x[3], x[4]) } }
?: errorValue(
"unresolved function: '${candidate}(x0, x1, x2, x3, x4)'"
) { _ -> error("this is the error function") }

View File

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