Add new modules to standard library

This commit is contained in:
2020-03-13 17:28:02 +01:00
parent 264100eef1
commit 5680ed4e42
17 changed files with 321 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package io.smnp.ext.lang
import io.smnp.ext.NativeModuleProvider
import io.smnp.ext.lang.function.TypeOfFunction
import io.smnp.ext.lang.method.CharAtMethod
import io.smnp.ext.lang.method.ListAccessMethod
import io.smnp.ext.lang.method.MapAccessMethod
import org.pf4j.Extension
@@ -9,5 +10,5 @@ import org.pf4j.Extension
@Extension
class LangModule : NativeModuleProvider("smnp.lang") {
override fun functions() = listOf(TypeOfFunction())
override fun methods() = listOf(ListAccessMethod(), MapAccessMethod())
override fun methods() = listOf(ListAccessMethod(), MapAccessMethod(), CharAtMethod())
}

View File

@@ -0,0 +1,18 @@
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.STRING
import io.smnp.type.matcher.Matcher.Companion.ofType
import io.smnp.type.model.Value
class CharAtMethod : Method(ofType(STRING),"charAt") {
override fun define(new: MethodDefinitionTool) {
new method simple(ofType(INT)) body { _, obj, (index) ->
Value.string((obj.value!! as String).getOrNull(index.value!! as Int)?.toString() ?: throw RuntimeException("Index '${index.value!!}' runs out of string bounds"))
}
}
}