From ab04e6b00193e56d57fcdfe19628d81f5a05078e Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Fri, 28 Jun 2024 07:07:20 +0200 Subject: [PATCH] [orx-expression-evaluator-typed] Add zip, construction from lists for rgb, mat4 --- .../src/commonMain/kotlin/typed/Function1.kt | 82 +++++++++++++++++-- .../src/commonMain/kotlin/typed/Function2.kt | 15 ++++ .../kotlin/typed/MemberFunctions.kt | 4 + .../kotlin/typed/TypedExpressions.kt | 2 +- .../kotlin/typed/TestTypedExpression.kt | 8 ++ 5 files changed, 103 insertions(+), 8 deletions(-) diff --git a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function1.kt b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function1.kt index 0ef205cd..92d2b5ee 100644 --- a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function1.kt +++ b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function1.kt @@ -13,18 +13,67 @@ import kotlin.math.sin as sin_ import kotlin.math.sqrt as sqrt_ internal fun vec2(x: Any): Vector2 { - require(x is Double) - return Vector2(x, x) + return when (x) { + is Double -> Vector2(x, x) + is List<*> -> { + when (x.size) { + 2 -> { + x as List; Vector2(x[0], x[1]) + } + + else -> error("vec2(): unsupported argument: '$x'") + } + } + + else -> error("vec2(): unsupported argument: '$x'") + } } internal fun vec3(x: Any): Vector3 { - require(x is Double) - return Vector3(x, x, x) + return when (x) { + is Double -> Vector3(x, x, x) + is List<*> -> { + when (x.size) { + 2 -> { + vec3(x[0]!!, x[1]!!) + } + + 3 -> { + vec3(x[0]!!, x[1]!!, x[2]!!) + } + + else -> error("vec3(): unsupported argument: '$x'") + } + } + + else -> error("vec3(): unsupported argument: '$x'") + } } + internal fun vec4(x: Any): Vector4 { - require(x is Double) - return Vector4(x, x, x, x) + return when (x) { + is Double -> Vector4(x, x, x, x) + is List<*> -> { + when (x.size) { + 2 -> { + vec4(x[0]!!, x[1]!!) + } + + 3 -> { + vec4(x[0]!!, x[1]!!, x[2]!!) + } + + 4 -> { + vec4(x[0]!!, x[1]!!, x[2]!!, x[3]!!) + } + + else -> error("vec4(): unsupported argument: '$x'") + } + } + + else -> error("vec4(): unsupported argument: '$x'") + } } internal fun rgba(x: Any): ColorRGBa { @@ -120,17 +169,36 @@ internal fun translate(translation: Any): Matrix44 { return Matrix44.translate(translation) } +internal fun mat4(x: Any): Matrix44 { + return when (x) { + is List<*> -> { + when (x.size) { + 16 -> Matrix44.fromDoubleArray((x as List).toDoubleArray()) + 4 -> { + (x as List) + Matrix44.fromColumnVectors(x[0], x[1], x[2], x[3]) + } + + else -> error("mat4(): unsupported argument: '$x'") + } + } + + else -> error("mat4(): unsupported argument: '$x'") + } +} + internal fun dispatchFunction1(name: String, functions: Map): ((Array) -> Any)? { return when (name) { "vec2" -> { x -> vec2(x[0]) } "vec3" -> { x -> vec3(x[0]) } "vec4" -> { x -> vec4(x[0]) } - + "mat4" -> { x -> mat4(x[0]) } "cos" -> { x -> cos(x[0]) } "sin" -> { x -> sin(x[0]) } "sqrt" -> { v -> sqrt(v[0]) } "abs" -> { v -> abs(v[0]) } "scale" -> { x -> scale(x[0]) } + "rgb", "rgba" -> { x -> rgba(x[0]) } "translate" -> { x -> translate(x[0]) } "transpose" -> { x -> transpose(x[0]) } "inverse" -> { x -> inverse(x[0]) } diff --git a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function2.kt b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function2.kt index 615c25f5..fa9711d6 100644 --- a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function2.kt +++ b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/Function2.kt @@ -59,6 +59,21 @@ internal fun vec3(x: Any, y: Any): Vector3 = when { } } +internal fun vec4(x: Any, y: Any): Vector4 = when { + x is Double && y is Vector3 -> { + Vector4(x, y.x, y.y, y.z) + } + x is Vector2 && y is Vector2 -> { + Vector4(x.x, x.y, y.x, y.y) + } + x is Vector3 && y is Double -> { + Vector4(x.x, x.y, x.z, y) + } + else -> { + error("unsupported arguments, '$x' (${x::class}) '$y' (${y::class}") + } +} + internal fun dispatchFunction2(name: String, functions: Map): ((Array) -> Any)? { return when (name) { "min" -> { x -> min(x[0], x[1]) } diff --git a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/MemberFunctions.kt b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/MemberFunctions.kt index 7140a1e5..1f4f3a0b 100644 --- a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/MemberFunctions.kt +++ b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/MemberFunctions.kt @@ -34,14 +34,18 @@ internal fun List<*>.memberFunctions(n: String): ((Array) -> Any)? { "minBy" -> { n -> val lambda = (n[0] as (Any) -> Any); this.minByOrNull { lambda(it!!) as Comparable } ?: error("no max") } + "sorted" -> { n -> (this as List>).sorted() } "sortedBy" -> { n -> val lambda = (n[0] as (Any) -> Any); this.sortedBy { lambda(it!!) as Comparable } } + "sortedByDescending" -> { n -> val lambda = (n[0] as (Any) -> Any); this.sortedByDescending { lambda(it!!) as Comparable } } + "reversed" -> { n -> this.reversed() } + "zip" -> { n -> this.zip(n[0] as List).map { listOf(it.first, it.second) } } else -> null } diff --git a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt index b3c356bd..f13008b1 100644 --- a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt +++ b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt @@ -819,7 +819,7 @@ abstract class TypedExpressionListenerBase( } - else -> error("receiver '${receiver}' not supported") + else -> error("receiver for '$name' '${receiver.toString().take(30)}' ${receiver::class} not supported") } } diff --git a/orx-expression-evaluator-typed/src/jvmTest/kotlin/typed/TestTypedExpression.kt b/orx-expression-evaluator-typed/src/jvmTest/kotlin/typed/TestTypedExpression.kt index 8592791f..d3a699a4 100644 --- a/orx-expression-evaluator-typed/src/jvmTest/kotlin/typed/TestTypedExpression.kt +++ b/orx-expression-evaluator-typed/src/jvmTest/kotlin/typed/TestTypedExpression.kt @@ -51,6 +51,14 @@ class TestTypedExpression { println("result is: ${evaluateTypedExpression("[] + []")}") println("result is: ${evaluateTypedExpression("([1] * 2 + [2] * 1)*5")}" ) + + println("result is: ${evaluateTypedExpression("[] + []")}") + println("result is: ${evaluateTypedExpression("[[0, 1, 2][1]]")}" ) + + println("result is: ${evaluateTypedExpression("[0, 1, 2].max()")}" ) + println("result is: ${evaluateTypedExpression("[0, 1, 2].maxBy { x -> x }")}" ) + println("result is: ${evaluateTypedExpression("""["one", "two", "three"].maxBy { x -> x.length }""")}") + }