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
|
package gay.pizza.pork.eval
|
||||||
|
|
||||||
import gay.pizza.pork.ast.*
|
import gay.pizza.pork.ast.*
|
||||||
import java.util.function.Function
|
|
||||||
|
|
||||||
class Evaluator(root: Scope) : Visitor<Any> {
|
class Evaluator(root: Scope) : Visitor<Any> {
|
||||||
private var currentScope: Scope = root
|
private var currentScope: Scope = root
|
||||||
@ -21,8 +20,8 @@ class Evaluator(root: Scope) : Visitor<Any> {
|
|||||||
return Unit
|
return Unit
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun visitLambda(node: Lambda): Function<Any, Any> {
|
override fun visitLambda(node: Lambda): CallableFunction {
|
||||||
return Function { _ ->
|
return CallableFunction { _ ->
|
||||||
currentScope = currentScope.fork()
|
currentScope = currentScope.fork()
|
||||||
try {
|
try {
|
||||||
var value: Any? = null
|
var value: Any? = null
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package gay.pizza.pork.eval
|
package gay.pizza.pork.eval
|
||||||
|
|
||||||
import java.util.function.Function
|
|
||||||
|
|
||||||
class Scope(val parent: Scope? = null) {
|
class Scope(val parent: Scope? = null) {
|
||||||
private val variables = mutableMapOf<String, Any>()
|
private val variables = mutableMapOf<String, Any>()
|
||||||
|
|
||||||
@ -25,12 +23,11 @@ class Scope(val parent: Scope? = null) {
|
|||||||
|
|
||||||
fun call(name: String, argument: Any = Unit): Any {
|
fun call(name: String, argument: Any = Unit): Any {
|
||||||
val value = value(name)
|
val value = value(name)
|
||||||
if (value !is Function<*, *>) {
|
if (value !is CallableFunction) {
|
||||||
throw RuntimeException("$value is not callable.")
|
throw RuntimeException("$value is not callable.")
|
||||||
}
|
}
|
||||||
@Suppress("UNCHECKED_CAST")
|
val function = value as CallableFunction
|
||||||
val casted = value as Function<Any, Any>
|
return function.call(argument)
|
||||||
return casted.apply(argument)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun fork(): Scope {
|
fun fork(): Scope {
|
||||||
|
Loading…
Reference in New Issue
Block a user