Add new modules to standard library
This commit is contained in:
@@ -4,6 +4,6 @@ import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class CollectionModule : LanguageModuleProvider("smnp.collection") {
|
||||
override fun files() = listOf("list.mus")
|
||||
override fun files() = listOf("list.mus", "map.mus")
|
||||
override fun dependencies() = listOf("smnp.lang")
|
||||
}
|
||||
@@ -12,4 +12,34 @@ function _flatten(list: list, output: list) {
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
extend list as this {
|
||||
function toFlat() {
|
||||
return flatten(this);
|
||||
}
|
||||
|
||||
function contains(value) {
|
||||
return (this as item ^ item % item == value).size > 0;
|
||||
}
|
||||
|
||||
function join(separator: string = ", ") {
|
||||
output = ""
|
||||
this as (item, index) ^ {
|
||||
output = output + item;
|
||||
if (index < this.size - 1) {
|
||||
output = output + separator;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function isEmpty() {
|
||||
return this.size == 0;
|
||||
}
|
||||
|
||||
function isNotEmpty() {
|
||||
return not this.isEmpty();
|
||||
}
|
||||
}
|
||||
21
modules/collection/src/main/resources/map.mus
Normal file
21
modules/collection/src/main/resources/map.mus
Normal file
@@ -0,0 +1,21 @@
|
||||
extend map as this {
|
||||
function containsKey(key) {
|
||||
return this.keys.contains(key);
|
||||
}
|
||||
|
||||
function containsValue(value) {
|
||||
return this.values.contains(value);
|
||||
}
|
||||
|
||||
function contains(key, value) {
|
||||
return (this as (v, k) ^ v % (k == key) and (v == value)).size > 0;
|
||||
}
|
||||
|
||||
function isEmpty() {
|
||||
return this.size == 0;
|
||||
}
|
||||
|
||||
function isNotEmpty() {
|
||||
return not this.isEmpty();
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,10 @@ package io.smnp.ext.io
|
||||
|
||||
import io.smnp.ext.NativeModuleProvider
|
||||
import io.smnp.ext.io.function.PrintlnFunction
|
||||
import io.smnp.ext.io.function.ReadFunction
|
||||
import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class IoModule : NativeModuleProvider("smnp.io") {
|
||||
override fun functions() = listOf(PrintlnFunction())
|
||||
override fun functions() = listOf(PrintlnFunction(), ReadFunction())
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.smnp.ext.io.function
|
||||
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.callable.function.FunctionDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.type.enumeration.DataType.STRING
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
import io.smnp.type.matcher.Matcher.Companion.optional
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class ReadFunction : Function("read") {
|
||||
override fun define(new: FunctionDefinitionTool) {
|
||||
new function simple(optional(ofType(STRING))) body { _, arguments ->
|
||||
arguments.getOrNull(0)?.let { print(it.value!!) }
|
||||
Value.string(readLine() ?: "")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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())
|
||||
}
|
||||
@@ -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"))
|
||||
}
|
||||
}
|
||||
}
|
||||
0
modules/system/build.gradle
Normal file
0
modules/system/build.gradle
Normal file
7
modules/system/gradle.properties
Normal file
7
modules/system/gradle.properties
Normal file
@@ -0,0 +1,7 @@
|
||||
version=0.0.1
|
||||
|
||||
pluginVersion=0.1
|
||||
pluginId=smnp.system
|
||||
pluginClass=
|
||||
pluginProvider=Bartłomiej Pluta
|
||||
pluginDependencies=
|
||||
10
modules/system/src/main/kotlin/io/smnp/ext/SystemModule.kt
Normal file
10
modules/system/src/main/kotlin/io/smnp/ext/SystemModule.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
package io.smnp.ext
|
||||
|
||||
import io.smnp.ext.function.ExitFunction
|
||||
import io.smnp.ext.function.SleepFunction
|
||||
import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class SystemModule : NativeModuleProvider("smnp.system") {
|
||||
override fun functions() = listOf(ExitFunction(), SleepFunction())
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package io.smnp.ext.function
|
||||
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.callable.function.FunctionDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.type.enumeration.DataType.INT
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
import io.smnp.type.matcher.Matcher.Companion.optional
|
||||
import io.smnp.type.model.Value
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class ExitFunction : Function("exit") {
|
||||
override fun define(new: FunctionDefinitionTool) {
|
||||
new function simple(optional(ofType(INT))) body { _, arguments ->
|
||||
val exitCode = arguments.getOrNull(0) ?: Value.int(0)
|
||||
exitProcess(exitCode.value!! as Int)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.smnp.ext.function
|
||||
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.callable.function.FunctionDefinitionTool
|
||||
import io.smnp.callable.signature.Signature.Companion.simple
|
||||
import io.smnp.type.enumeration.DataType.INT
|
||||
import io.smnp.type.matcher.Matcher.Companion.ofType
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class SleepFunction : Function("sleep") {
|
||||
override fun define(new: FunctionDefinitionTool) {
|
||||
new function simple(ofType(INT)) body { _, (milli) ->
|
||||
Thread.sleep((milli.value!! as Int).toLong())
|
||||
Value.void()
|
||||
}
|
||||
}
|
||||
}
|
||||
0
modules/text/build.gradle
Normal file
0
modules/text/build.gradle
Normal file
7
modules/text/gradle.properties
Normal file
7
modules/text/gradle.properties
Normal file
@@ -0,0 +1,7 @@
|
||||
version=0.0.1
|
||||
|
||||
pluginVersion=0.1
|
||||
pluginId=smnp.text
|
||||
pluginClass=
|
||||
pluginProvider=Bartłomiej Pluta
|
||||
pluginDependencies=
|
||||
8
modules/text/src/main/kotlin/io/smnp/ext/TextModule.kt
Normal file
8
modules/text/src/main/kotlin/io/smnp/ext/TextModule.kt
Normal file
@@ -0,0 +1,8 @@
|
||||
package io.smnp.ext
|
||||
|
||||
import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class TextModule : LanguageModuleProvider("smnp.text") {
|
||||
override fun dependencies() = listOf("smnp.lang", "smnp.collection")
|
||||
}
|
||||
158
modules/text/src/main/resources/main.mus
Normal file
158
modules/text/src/main/resources/main.mus
Normal file
@@ -0,0 +1,158 @@
|
||||
extend string as this {
|
||||
function find(char: string) {
|
||||
if(char.length > 1) {
|
||||
throw "Only single character can act as a pattern to be found";
|
||||
}
|
||||
|
||||
this as (c, index) ^ {
|
||||
if(c == char) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
function isEmpty() {
|
||||
return this.length == 0;
|
||||
}
|
||||
|
||||
function isNotEmpty() {
|
||||
return not this.isEmpty();
|
||||
}
|
||||
|
||||
function toInt() {
|
||||
if (this.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
|
||||
digits = {
|
||||
"0" -> 0,
|
||||
"1" -> 1,
|
||||
"2" -> 2,
|
||||
"3" -> 3,
|
||||
"4" -> 4,
|
||||
"5" -> 5,
|
||||
"6" -> 6,
|
||||
"7" -> 7,
|
||||
"8" -> 8,
|
||||
"9" -> 9
|
||||
};
|
||||
|
||||
str = this;
|
||||
negative = false;
|
||||
if(str.charAt(0) == "-") {
|
||||
negative = true;
|
||||
str = this.substring(1, this.length);
|
||||
}
|
||||
|
||||
output = 0
|
||||
str as (char, index) ^ {
|
||||
if(not digits.containsKey(char)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
output = output + (digits.get(char) * 10 ** (str.length-index-1))
|
||||
}
|
||||
|
||||
if (negative) {
|
||||
output = -output;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function substring(startIndex: int, endIndex: int) {
|
||||
output = ""
|
||||
(endIndex - startIndex) as index ^ {
|
||||
output = output + this.charAt(index + startIndex);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function split(delimiter: string) {
|
||||
output = []
|
||||
|
||||
previousDelimiter = 0;
|
||||
this as (char, index) ^ {
|
||||
if(char == delimiter) {
|
||||
output = output + [this.substring(previousDelimiter, index)];
|
||||
previousDelimiter = index+1;
|
||||
}
|
||||
}
|
||||
|
||||
output = output + [this.substring(previousDelimiter, this.length)];
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
function toFloat() {
|
||||
if (this.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
digits = {
|
||||
"0" -> 0,
|
||||
"1" -> 1,
|
||||
"2" -> 2,
|
||||
"3" -> 3,
|
||||
"4" -> 4,
|
||||
"5" -> 5,
|
||||
"6" -> 6,
|
||||
"7" -> 7,
|
||||
"8" -> 8,
|
||||
"9" -> 9
|
||||
};
|
||||
|
||||
str = this;
|
||||
negative = false;
|
||||
if(str.charAt(0) == "-") {
|
||||
negative = true;
|
||||
str = this.substring(1, this.length);
|
||||
}
|
||||
|
||||
split = str.split(".");
|
||||
if(split.size > 2) {
|
||||
return this;
|
||||
}
|
||||
|
||||
iStr = split.get(0);
|
||||
dStr = split.get(1);
|
||||
|
||||
i = 0;
|
||||
d = 0;
|
||||
|
||||
iStr as (char, index) ^ {
|
||||
if(not digits.containsKey(char)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
i = i + (digits.get(char) * 10 ** (iStr.length-index-1))
|
||||
}
|
||||
|
||||
dStr as (char, index) ^ {
|
||||
if(not digits.containsKey(char)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
d = d + (digits.get(char)) * 10 ** (-index-1);
|
||||
}
|
||||
|
||||
number = i + d;
|
||||
if(negative) {
|
||||
number = -number;
|
||||
}
|
||||
|
||||
return number;
|
||||
}
|
||||
|
||||
function toBool() {
|
||||
if(this == "true") {
|
||||
return true;
|
||||
} else if (this == "false") {
|
||||
return false;
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,7 @@ include 'modules'
|
||||
|
||||
include 'modules:lang'
|
||||
include 'modules:debug'
|
||||
include 'modules:system'
|
||||
include 'modules:io'
|
||||
include 'modules:collection'
|
||||
include 'modules:collection'
|
||||
include 'modules:text'
|
||||
Reference in New Issue
Block a user