[orx-expression-evaluator-typed] Add zip, construction from lists for rgb, mat4
This commit is contained in:
@@ -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<Double>; 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<Double>).toDoubleArray())
|
||||
4 -> {
|
||||
(x as List<Vector4>)
|
||||
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<String, TypedFunction1>): ((Array<Any>) -> 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]) }
|
||||
|
||||
@@ -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<String, TypedFunction2>): ((Array<Any>) -> Any)? {
|
||||
return when (name) {
|
||||
"min" -> { x -> min(x[0], x[1]) }
|
||||
|
||||
@@ -34,14 +34,18 @@ internal fun List<*>.memberFunctions(n: String): ((Array<Any>) -> Any)? {
|
||||
"minBy" -> { n ->
|
||||
val lambda = (n[0] as (Any) -> Any); this.minByOrNull { lambda(it!!) as Comparable<Any> } ?: error("no max")
|
||||
}
|
||||
|
||||
"sorted" -> { n -> (this as List<Comparable<Any>>).sorted() }
|
||||
"sortedBy" -> { n ->
|
||||
val lambda = (n[0] as (Any) -> Any); this.sortedBy { lambda(it!!) as Comparable<Any> }
|
||||
}
|
||||
|
||||
"sortedByDescending" -> { n ->
|
||||
val lambda = (n[0] as (Any) -> Any); this.sortedByDescending { lambda(it!!) as Comparable<Any> }
|
||||
}
|
||||
|
||||
"reversed" -> { n -> this.reversed() }
|
||||
"zip" -> { n -> this.zip(n[0] as List<Any>).map { listOf(it.first, it.second) } }
|
||||
|
||||
else -> null
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 }""")}")
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user