Utilize CallableFunction to avoid Java dependency.

This commit is contained in:
Alex Zenla 2023-08-19 16:58:34 -07:00
parent 7bec148d79
commit ef4045ecb4
Signed by: alex
GPG Key ID: C0780728420EBFE5
3 changed files with 10 additions and 9 deletions

View File

@ -0,0 +1,5 @@
package gay.pizza.pork.eval
fun interface CallableFunction {
fun call(argument: Any): Any
}

View File

@ -1,7 +1,6 @@
package gay.pizza.pork.eval
import gay.pizza.pork.ast.*
import java.util.function.Function
class Evaluator(root: Scope) : Visitor<Any> {
private var currentScope: Scope = root
@ -21,8 +20,8 @@ class Evaluator(root: Scope) : Visitor<Any> {
return Unit
}
override fun visitLambda(node: Lambda): Function<Any, Any> {
return Function { _ ->
override fun visitLambda(node: Lambda): CallableFunction {
return CallableFunction { _ ->
currentScope = currentScope.fork()
try {
var value: Any? = null

View File

@ -1,7 +1,5 @@
package gay.pizza.pork.eval
import java.util.function.Function
class Scope(val parent: Scope? = null) {
private val variables = mutableMapOf<String, Any>()
@ -25,12 +23,11 @@ class Scope(val parent: Scope? = null) {
fun call(name: String, argument: Any = Unit): Any {
val value = value(name)
if (value !is Function<*, *>) {
if (value !is CallableFunction) {
throw RuntimeException("$value is not callable.")
}
@Suppress("UNCHECKED_CAST")
val casted = value as Function<Any, Any>
return casted.apply(argument)
val function = value as CallableFunction
return function.call(argument)
}
fun fork(): Scope {