Create smnp.lang package with basic get() methods for map and list
This commit is contained in:
@@ -19,7 +19,7 @@ abstract class Function(val name: String) {
|
||||
.firstOrNull { (_, args) -> args.signatureMatched }
|
||||
?: throw FunctionInvocationException(this, arguments)
|
||||
|
||||
return definition.body(environment, args.arguments)
|
||||
return definition.body(environment, arguments.toList())
|
||||
}
|
||||
|
||||
val signature: String
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.smnp.environment
|
||||
|
||||
import io.smnp.ext.ModuleRegistry
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
class Environment {
|
||||
@@ -7,7 +8,10 @@ class Environment {
|
||||
private val loadedModules = mutableListOf<String>()
|
||||
|
||||
fun loadModule(path: String) {
|
||||
|
||||
ModuleRegistry.requestModulesForPath(path).forEach {
|
||||
rootModule.addSubmodule(it)
|
||||
loadedModules.add(path)
|
||||
}
|
||||
}
|
||||
|
||||
fun printModules(printContent: Boolean) {
|
||||
|
||||
3
api/src/main/kotlin/io/smnp/error/RuntimeException.kt
Normal file
3
api/src/main/kotlin/io/smnp/error/RuntimeException.kt
Normal file
@@ -0,0 +1,3 @@
|
||||
package io.smnp.error
|
||||
|
||||
class RuntimeException(message: String?) : Exception(message)
|
||||
21
api/src/main/kotlin/io/smnp/ext/ModuleRegistry.kt
Normal file
21
api/src/main/kotlin/io/smnp/ext/ModuleRegistry.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package io.smnp.ext
|
||||
|
||||
import io.smnp.type.module.Module
|
||||
import org.pf4j.DefaultPluginManager
|
||||
|
||||
object ModuleRegistry {
|
||||
private val modules = mutableListOf<Pair<String, Module>>()
|
||||
init {
|
||||
val pluginManager = DefaultPluginManager()
|
||||
pluginManager.loadPlugins()
|
||||
pluginManager.startPlugins()
|
||||
|
||||
pluginManager.getExtensions(ModuleDefinition::class.java).forEach {
|
||||
modules.add(Pair(it.modulePath(), Module.create(it.modulePath(), it.functions(), it.methods())))
|
||||
}
|
||||
}
|
||||
|
||||
fun requestModulesForPath(path: String): List<Module> {
|
||||
return modules.filter { it.first == path }.map { it.second }
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import io.smnp.data.entity.Note
|
||||
import io.smnp.error.ShouldNeverReachThisLineException
|
||||
import io.smnp.type.enumeration.DataType
|
||||
|
||||
class Value private constructor(val type: DataType, val value: Any?, val properties: Map<String, Value> = emptyMap()) {
|
||||
data class Value(val type: DataType, val value: Any?, val properties: Map<String, Value> = emptyMap()) {
|
||||
init {
|
||||
if(!type.isInstance(value)) {
|
||||
throw RuntimeException("'$value' is not of type $type")
|
||||
|
||||
@@ -44,7 +44,7 @@ task plugin(type: Jar) {
|
||||
}
|
||||
|
||||
task assemblePlugin(type: Copy) {
|
||||
from plugin
|
||||
from jar
|
||||
into pluginsDir
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package io.smnp.ext.lang
|
||||
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.callable.function.FunctionDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.vararg
|
||||
import io.smnp.type.matcher.Matcher.Companion.allTypes
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class DisplayFunction : Function("println") {
|
||||
override fun define(new: FunctionDefinitionTool) {
|
||||
new function vararg(allTypes()) define { _, v ->
|
||||
println(v.joinToString("") { it.toString() })
|
||||
Value.void()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
package io.smnp.ext.lang
|
||||
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.ext.ModuleDefinition
|
||||
import io.smnp.ext.lang.method.ListAccessMethod
|
||||
import io.smnp.ext.lang.method.MapAccessMethod
|
||||
import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class LangModule : ModuleDefinition {
|
||||
override fun modulePath() = "smnp.io"
|
||||
override fun modulePath() = "smnp.lang"
|
||||
|
||||
override fun functions() = listOf(DisplayFunction())
|
||||
override fun functions() = emptyList<Function>()
|
||||
|
||||
override fun methods() = emptyList<Method>()
|
||||
override fun methods() = listOf(ListAccessMethod(), MapAccessMethod())
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package io.smnp.ext.lang.method
|
||||
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.method.MethodDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.type.enumeration.DataType.INT
|
||||
import io.smnp.type.enumeration.DataType.LIST
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class ListAccessMethod : Method(ofType(LIST), "get") {
|
||||
override fun define(new: MethodDefinitionTool) {
|
||||
new method simple(ofType(INT)) define { _, value, (index) ->
|
||||
val list = value.value!! as List<Value>
|
||||
val i = index.value!! as Int
|
||||
|
||||
if(i >= list.size) {
|
||||
throw RuntimeException("Index '$i' runs out of array bounds")
|
||||
}
|
||||
|
||||
list[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.smnp.ext.lang.method
|
||||
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.method.MethodDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.error.RuntimeException
|
||||
import io.smnp.type.enumeration.DataType.MAP
|
||||
import io.smnp.type.matcher.Matcher.Companion.allTypes
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class MapAccessMethod : Method(ofType(MAP), "get") {
|
||||
override fun define(new: MethodDefinitionTool) {
|
||||
new method simple(allTypes()) define { _, obj, (key) ->
|
||||
val map = (obj.value!! as Map<Value, Value>)
|
||||
map[key] ?: throw RuntimeException("Key '${key.value!!}' not found")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user