Skip to content

Commit b82ffb9

Browse files
committed
[orx-expression-evaluator] Improve exception texts
1 parent 1e9c46e commit b82ffb9

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

orx-expression-evaluator-typed/src/commonMain/kotlin/typed/CompiledFunctions.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ fun <T0, R> compileFunction1(
2828

2929
return { p0 ->
3030
varP0 = p0
31-
ParseTreeWalker.DEFAULT.walk(listener, root)
31+
try {
32+
ParseTreeWalker.DEFAULT.walk(listener, root)
33+
} catch(e: Throwable) {
34+
throw RuntimeException("Error while evaluating '$expression' with parameter $parameter0=$p0. ${e.message}", e)
35+
}
3236
@Suppress("UNCHECKED_CAST")
33-
listener.state.lastExpressionResult as? R ?: error("no result")
37+
listener.state.lastExpressionResult as? R ?: error("No result while evaluating '$expression' with parameter $parameter0=$p0")
3438
}
3539
}
3640

orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ abstract class TypedExpressionListenerBase(
781781
IDType.VARIABLE -> s.valueStack.pushChecked(
782782
when (name) {
783783
"PI" -> PI
784-
else -> constants(name) ?: errorValue("unresolved variable: '${name}'", 0.0 / 0.0)
784+
else -> constants(name) ?: errorValue("unresolved value: '${name}'. Available constant: ${constants}", Unit)
785785
}
786786
)
787787

@@ -835,10 +835,15 @@ abstract class TypedExpressionListenerBase(
835835

836836

837837
is Function<*> -> {
838+
839+
fun input(): String {
840+
return "in '${node.getParent()?.text}'"
841+
}
842+
838843
@Suppress("UNCHECKED_CAST")
839-
receiver as (String) -> Any
844+
receiver as (String) -> Any?
840845
val function =
841-
receiver.invoke(name)
846+
receiver.invoke(name) ?: error("Unresolved function '${name} ${input()}")
842847

843848
when (idType) {
844849
IDType.MEMBER_FUNCTION0 -> {
@@ -849,19 +854,19 @@ abstract class TypedExpressionListenerBase(
849854

850855
IDType.MEMBER_FUNCTION1 -> {
851856
@Suppress("UNCHECKED_CAST")
852-
function as (Any) -> Any
857+
(function as? (Any) -> Any) ?: error("Cannot cast function '$name' ($function) to (Any) -> Any ${input()}")
853858
s.functionStack.push({ x -> function(x[0]) })
854859
}
855860

856861
IDType.MEMBER_FUNCTION2 -> {
857862
@Suppress("UNCHECKED_CAST")
858-
function as (Any, Any) -> Any
863+
function as? (Any, Any) -> Any ?: error("Cannot cast function '$name' ($function) to (Any, Any) -> Any ${input()}")
859864
s.functionStack.push({ x -> function(x[0], x[1]) })
860865
}
861866

862867
IDType.MEMBER_FUNCTION3 -> {
863868
@Suppress("UNCHECKED_CAST")
864-
function as (Any, Any, Any) -> Any
869+
function as? (Any, Any, Any) -> Any ?: error("Cannot cast function '$name' ($function) to (Any, Any, Any) -> Any ${input()}")
865870
s.functionStack.push({ x -> function(x[0], x[1], x[2]) })
866871
}
867872

orx-expression-evaluator/src/commonMain/kotlin/Expressions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ internal class ExpressionListener(
261261
IDType.VARIABLE -> doubleStack.push(
262262
when (name) {
263263
"PI" -> PI
264-
else -> constants[name] ?: errorValue("unresolved variable: '${name}'", 0.0 / 0.0)
264+
else -> constants[name] ?: errorValue("unresolved value: '${name}'. available values: ${constants}", 0.0 / 0.0)
265265
}
266266
)
267267

0 commit comments

Comments
 (0)