Create smnp.lang package with basic get() methods for map and list
This commit is contained in:
@@ -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