mirror of
https://github.com/GayPizzaSpecifications/pork.git
synced 2025-08-03 05:10:55 +00:00
Utilize CallableFunction to avoid Java dependency.
This commit is contained in:
parent
7bec148d79
commit
ef4045ecb4
5
src/main/kotlin/gay/pizza/pork/eval/CallableFunction.kt
Normal file
5
src/main/kotlin/gay/pizza/pork/eval/CallableFunction.kt
Normal file
@ -0,0 +1,5 @@
|
||||
package gay.pizza.pork.eval
|
||||
|
||||
fun interface CallableFunction {
|
||||
fun call(argument: Any): Any
|
||||
}
|
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user