Fix unary minus operators
This commit is contained in:
@@ -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) {
|
||||||
@@ -262,7 +260,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
|
|||||||
val function: (DoubleArray) -> Double =
|
val function: (DoubleArray) -> Double =
|
||||||
when (val candidate = node.text) {
|
when (val candidate = node.text) {
|
||||||
"random" -> { _ -> Double.uniform(0.0, 1.0) }
|
"random" -> { _ -> Double.uniform(0.0, 1.0) }
|
||||||
else -> functions.functions0[candidate]?.let { { _:DoubleArray -> it.invoke() } }
|
else -> functions.functions0[candidate]?.let { { _: DoubleArray -> it.invoke() } }
|
||||||
?: errorValue(
|
?: errorValue(
|
||||||
"unresolved function: '${candidate}()'"
|
"unresolved function: '${candidate}()'"
|
||||||
) { _ -> error("this is the error function") }
|
) { _ -> error("this is the error function") }
|
||||||
@@ -287,7 +285,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
|
|||||||
"floor" -> { x -> floor(x[0]) }
|
"floor" -> { x -> floor(x[0]) }
|
||||||
"ceil" -> { x -> ceil(x[0]) }
|
"ceil" -> { x -> ceil(x[0]) }
|
||||||
"saturate" -> { x -> x[0].coerceIn(0.0, 1.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(
|
?: errorValue(
|
||||||
"unresolved function: '${candidate}(x0)'"
|
"unresolved function: '${candidate}(x0)'"
|
||||||
) { _ -> error("this is the error function") }
|
) { _ -> error("this is the error function") }
|
||||||
@@ -302,7 +300,8 @@ 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]) }
|
||||||
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(
|
?: errorValue(
|
||||||
"unresolved function: '${candidate}(x0, x1)'"
|
"unresolved function: '${candidate}(x0, x1)'"
|
||||||
) { _ -> error("this is the error function") }
|
) { _ -> error("this is the error function") }
|
||||||
@@ -314,7 +313,8 @@ 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]) }
|
||||||
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(
|
?: errorValue(
|
||||||
"unresolved function: '${candidate}(x0, x1, x2)'"
|
"unresolved function: '${candidate}(x0, x1, x2)'"
|
||||||
) { _ -> error("this is the error function") }
|
) { _ -> error("this is the error function") }
|
||||||
@@ -324,7 +324,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
|
|||||||
IDType.FUNCTION4 -> {
|
IDType.FUNCTION4 -> {
|
||||||
val function: (DoubleArray) -> Double =
|
val function: (DoubleArray) -> Double =
|
||||||
when (val candidate = node.text) {
|
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(
|
?: errorValue(
|
||||||
"unresolved function: '${candidate}(x0, x1, x2, x3)'"
|
"unresolved function: '${candidate}(x0, x1, x2, x3)'"
|
||||||
) { _ -> error("this is the error function") }
|
) { _ -> error("this is the error function") }
|
||||||
@@ -336,7 +336,7 @@ internal class ExpressionListener(val functions: FunctionExtensions = FunctionEx
|
|||||||
val function: (DoubleArray) -> Double =
|
val function: (DoubleArray) -> Double =
|
||||||
when (val candidate = node.text) {
|
when (val candidate = node.text) {
|
||||||
"map" -> { x -> map(x[0], x[1], x[2], x[3], x[4]) }
|
"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(
|
?: errorValue(
|
||||||
"unresolved function: '${candidate}(x0, x1, x2, x3, x4)'"
|
"unresolved function: '${candidate}(x0, x1, x2, x3, x4)'"
|
||||||
) { _ -> error("this is the error function") }
|
) { _ -> error("this is the error function") }
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user