Add support for function/methods invocation on Environment + create call stack model
This commit is contained in:
@@ -2,10 +2,21 @@ package io.smnp.callable.function
|
||||
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.error.FunctionInvocationException
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.type.model.Value
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
abstract class Function(val name: String) {
|
||||
private var definitions: List<FunctionDefinition> = mutableListOf()
|
||||
private var _module: Module? = null
|
||||
var module: Module
|
||||
get() = _module ?: throw RuntimeException("Method has not set module yet")
|
||||
set(value) {
|
||||
if (_module != null) {
|
||||
throw RuntimeException("Module of method is already set to ${module.canonicalName}")
|
||||
}
|
||||
_module = value
|
||||
}
|
||||
|
||||
abstract fun define(new: FunctionDefinitionTool)
|
||||
|
||||
|
||||
@@ -2,11 +2,22 @@ package io.smnp.callable.method
|
||||
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.error.MethodInvocationException
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.type.matcher.Matcher
|
||||
import io.smnp.type.model.Value
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
abstract class Method(val typeMatcher: Matcher, val name: String) {
|
||||
private var definitions: List<MethodDefinition> = mutableListOf()
|
||||
private var _module: Module? = null
|
||||
var module: Module
|
||||
get() = _module ?: throw RuntimeException("Method has not set module yet")
|
||||
set(value) {
|
||||
if (_module != null) {
|
||||
throw RuntimeException("Module of method is already set to ${module.canonicalName}")
|
||||
}
|
||||
_module = value
|
||||
}
|
||||
|
||||
abstract fun define(new: MethodDefinitionTool)
|
||||
|
||||
|
||||
@@ -4,6 +4,10 @@ import io.smnp.type.enumeration.DataType
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
object ActualSignatureFormatter {
|
||||
fun format(value: Value): String {
|
||||
return format(arrayOf(value), false)
|
||||
}
|
||||
|
||||
fun format(arguments: Array<out Value>, parentheses: Boolean = true): String {
|
||||
val output = mutableListOf<String>()
|
||||
for(argument in arguments) {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package io.smnp.environment
|
||||
|
||||
import io.smnp.ext.ModuleRegistry
|
||||
import io.smnp.type.module.Module
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
interface Environment {
|
||||
fun loadModule(path: String)
|
||||
fun printModules(printContent: Boolean)
|
||||
fun invokeFunction(name: String, arguments: List<Value>): Value
|
||||
fun invokeMethod(obj: Value, name: String, arguments: List<Value>): Value
|
||||
fun printCallStack()
|
||||
}
|
||||
30
api/src/main/kotlin/io/smnp/runtime/model/CallStack.kt
Normal file
30
api/src/main/kotlin/io/smnp/runtime/model/CallStack.kt
Normal file
@@ -0,0 +1,30 @@
|
||||
package io.smnp.runtime.model
|
||||
|
||||
import io.smnp.type.model.Value
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
class CallStack {
|
||||
private val items = mutableListOf<CallStackItem>()
|
||||
|
||||
fun push(module: Module, name: String, arguments: List<Value>) {
|
||||
items.add(CallStackItem(module, name, arguments))
|
||||
}
|
||||
|
||||
fun push(item: CallStackItem) {
|
||||
items.add(item)
|
||||
}
|
||||
|
||||
fun pop(): CallStackItem? {
|
||||
if(items.isEmpty()) {
|
||||
return null
|
||||
}
|
||||
|
||||
val last = items.last()
|
||||
items.removeAt(items.size-1)
|
||||
return last
|
||||
}
|
||||
|
||||
fun pretty() {
|
||||
items.asReversed().forEachIndexed { index, item -> println("[${items.size - index - 1}] $item") }
|
||||
}
|
||||
}
|
||||
13
api/src/main/kotlin/io/smnp/runtime/model/CallStackItem.kt
Normal file
13
api/src/main/kotlin/io/smnp/runtime/model/CallStackItem.kt
Normal file
@@ -0,0 +1,13 @@
|
||||
package io.smnp.runtime.model
|
||||
|
||||
import io.smnp.callable.signature.ActualSignatureFormatter
|
||||
import io.smnp.type.model.Value
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
data class CallStackItem(
|
||||
val module: Module,
|
||||
val name: String,
|
||||
val arguments: List<Value>
|
||||
) {
|
||||
override fun toString() = "${module.canonicalName}::$name${ActualSignatureFormatter.format(arguments.toTypedArray())}"
|
||||
}
|
||||
@@ -17,6 +17,8 @@ class Module(
|
||||
|
||||
init {
|
||||
children.forEach { addSubmodule(it) }
|
||||
functions.forEach { it.module = this }
|
||||
methods.forEach { it.module = this }
|
||||
}
|
||||
|
||||
fun addSubmodule(module: Module) {
|
||||
@@ -35,19 +37,19 @@ class Module(
|
||||
}
|
||||
|
||||
val canonicalName: String
|
||||
get() {
|
||||
val modules = mutableListOf(this)
|
||||
var par = parent
|
||||
while(par != null) {
|
||||
modules.add(par)
|
||||
par = par.parent
|
||||
get() {
|
||||
val modules = mutableListOf(this)
|
||||
var par = parent
|
||||
while (par != null) {
|
||||
modules.add(par)
|
||||
par = par.parent
|
||||
}
|
||||
|
||||
return modules.reversed().joinToString(".") { it.name }
|
||||
}
|
||||
|
||||
return modules.reversed().joinToString(".") { it.name }
|
||||
}
|
||||
|
||||
fun merge(module: Module): Module {
|
||||
if(name != module.name) {
|
||||
if (name != module.name) {
|
||||
return this
|
||||
}
|
||||
|
||||
@@ -73,7 +75,12 @@ class Module(
|
||||
}
|
||||
|
||||
fun findMethod(value: Value, name: String): List<Method> {
|
||||
return methods.filter { it.name == name && it.verifyType(value) } + children.flatMap { it.findMethod(value, name) }
|
||||
return methods.filter { it.name == name && it.verifyType(value) } + children.flatMap {
|
||||
it.findMethod(
|
||||
value,
|
||||
name
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = name
|
||||
@@ -84,7 +91,7 @@ class Module(
|
||||
|
||||
println(newPrefix + (if (first) "" else if (newLast) "└─ " else "├─ ") + name)
|
||||
newPrefix += if (newLast) " " else "│ "
|
||||
if(printContent) {
|
||||
if (printContent) {
|
||||
val contentPrefix = newPrefix + if (children.isNotEmpty()) "|" else ""
|
||||
for ((index, function) in functions.withIndex()) {
|
||||
println(contentPrefix + (if (index == functions.size - 1 && methods.isEmpty()) "└ " else "├ ") + "${function.name}()")
|
||||
@@ -109,20 +116,22 @@ class Module(
|
||||
children: List<Module> = emptyList()
|
||||
): Module {
|
||||
val modules = path.split(".")
|
||||
if(modules.isEmpty()) {
|
||||
if (modules.isEmpty()) {
|
||||
return Module(path, functions, methods, children)
|
||||
}
|
||||
|
||||
val root = modules.map { Module(it) }.reduceRight { m, n -> m.addSubmodule(n); m }
|
||||
|
||||
var youngest = root
|
||||
while(youngest.children.isNotEmpty()) {
|
||||
while (youngest.children.isNotEmpty()) {
|
||||
youngest = youngest.children[0]
|
||||
}
|
||||
|
||||
youngest.functions.addAll(functions)
|
||||
youngest.methods.addAll(methods)
|
||||
youngest.children.addAll(children)
|
||||
youngest.functions.forEach { it.module = youngest }
|
||||
youngest.methods.forEach { it.module = youngest }
|
||||
|
||||
return root
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user