Add support for defining custom methods
This commit is contained in:
42
app/src/main/kotlin/io/smnp/callable/method/CustomMethod.kt
Normal file
42
app/src/main/kotlin/io/smnp/callable/method/CustomMethod.kt
Normal file
@@ -0,0 +1,42 @@
|
||||
package io.smnp.callable.method
|
||||
|
||||
import io.smnp.callable.util.FunctionEnvironmentProvider
|
||||
import io.smnp.callable.util.FunctionSignatureParser
|
||||
import io.smnp.dsl.ast.model.node.FunctionDefinitionArgumentsNode
|
||||
import io.smnp.dsl.ast.model.node.FunctionDefinitionNode
|
||||
import io.smnp.dsl.ast.model.node.IdentifierNode
|
||||
import io.smnp.evaluation.evaluator.BlockEvaluator
|
||||
import io.smnp.evaluation.model.exception.Return
|
||||
import io.smnp.type.matcher.Matcher
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
object CustomMethod {
|
||||
fun create(type: Matcher, objectIdentifier: String, node: FunctionDefinitionNode): Method {
|
||||
val identifier = (node.identifier as IdentifierNode).token.rawValue
|
||||
|
||||
return object : Method(type, identifier) {
|
||||
override fun define(new: MethodDefinitionTool) {
|
||||
val (_, argumentsNode, bodyNode) = node
|
||||
val signature = FunctionSignatureParser.parseSignature(argumentsNode as FunctionDefinitionArgumentsNode)
|
||||
val evaluator = BlockEvaluator(dedicatedScope = false)
|
||||
|
||||
new method signature body { env, obj, args ->
|
||||
val boundArguments =
|
||||
FunctionEnvironmentProvider.provideEnvironment(argumentsNode, args, env).toMutableMap()
|
||||
boundArguments[objectIdentifier] = obj
|
||||
|
||||
try {
|
||||
env.pushScope(boundArguments)
|
||||
evaluator.evaluate(bodyNode, env)
|
||||
} catch (value: Return) {
|
||||
return@body value.value
|
||||
} finally {
|
||||
env.popScope()
|
||||
}
|
||||
|
||||
Value.void()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user