[BIG REFACTOR] Create new project structure and prepare scaffolding for external modules system
This commit is contained in:
25
api/build.gradle
Normal file
25
api/build.gradle
Normal file
@@ -0,0 +1,25 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.jetbrains.kotlin.jvm'
|
||||
}
|
||||
|
||||
group 'io.bartek'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
compileTestKotlin {
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.api.callable
|
||||
package io.smnp.callable.function
|
||||
|
||||
import io.smnp.api.environment.Environment
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.error.FunctionInvocationException
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
abstract class Function(val name: String) {
|
||||
private var definitions: List<FunctionDefinition> = mutableListOf()
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.smnp.callable.function
|
||||
|
||||
import io.smnp.callable.signature.Signature
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class FunctionDefinition(val signature: Signature, val body: (Environment, List<Value>) -> Value)
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.api.callable
|
||||
package io.smnp.callable.function
|
||||
|
||||
import io.smnp.api.environment.Environment
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.api.signature.Signature
|
||||
import io.smnp.callable.signature.Signature
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class FunctionDefinitionTool {
|
||||
val definitions: MutableList<FunctionDefinition> = mutableListOf()
|
||||
@@ -1,9 +1,9 @@
|
||||
package io.smnp.api.callable
|
||||
package io.smnp.callable.method
|
||||
|
||||
import io.smnp.api.environment.Environment
|
||||
import io.smnp.api.matcher.Matcher
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.error.MethodInvocationException
|
||||
import io.smnp.type.matcher.Matcher
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
abstract class Method(val typeMatcher: Matcher, val name: String) {
|
||||
private var definitions: List<MethodDefinition> = mutableListOf()
|
||||
@@ -0,0 +1,7 @@
|
||||
package io.smnp.callable.method
|
||||
|
||||
import io.smnp.callable.signature.Signature
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class MethodDefinition(val signature: Signature, val body: (Environment, Value, List<Value>) -> Value)
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.api.callable
|
||||
package io.smnp.callable.method
|
||||
|
||||
import io.smnp.api.environment.Environment
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.api.signature.Signature
|
||||
import io.smnp.callable.signature.Signature
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class MethodDefinitionTool {
|
||||
val definitions: MutableList<MethodDefinition> = mutableListOf()
|
||||
@@ -1,15 +1,19 @@
|
||||
package io.smnp.api.signature
|
||||
package io.smnp.callable.signature
|
||||
|
||||
import io.smnp.api.enumeration.DataType
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.type.enumeration.DataType
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
object ActualSignatureFormatter {
|
||||
fun format(arguments: Array<out Value>, parentheses: Boolean = true): String {
|
||||
val output = mutableListOf<String>()
|
||||
for(argument in arguments) {
|
||||
output.add(when(argument.type) {
|
||||
DataType.LIST -> listTypes(argument.value as List<Value>)
|
||||
DataType.MAP -> mapTypes(argument.value as Map<Value, Value>)
|
||||
DataType.LIST -> listTypes(
|
||||
argument.value as List<Value>
|
||||
)
|
||||
DataType.MAP -> mapTypes(
|
||||
argument.value as Map<Value, Value>
|
||||
)
|
||||
else -> argument.type.name.toLowerCase()
|
||||
})
|
||||
}
|
||||
@@ -20,8 +24,12 @@ object ActualSignatureFormatter {
|
||||
private fun listTypes(list: List<Value>, output: MutableList<String> = mutableListOf()): String {
|
||||
for (item in list) {
|
||||
output.add(when (item.type) {
|
||||
DataType.LIST -> listTypes(item.value as List<Value>)
|
||||
DataType.MAP -> mapTypes(item.value as Map<Value, Value>)
|
||||
DataType.LIST -> listTypes(
|
||||
item.value as List<Value>
|
||||
)
|
||||
DataType.MAP -> mapTypes(
|
||||
item.value as Map<Value, Value>
|
||||
)
|
||||
else -> item.type.name.toLowerCase()
|
||||
})
|
||||
}
|
||||
@@ -32,8 +40,12 @@ object ActualSignatureFormatter {
|
||||
private fun mapTypes(map: Map<Value, Value>, output: MutableMap<Value, String> = mutableMapOf()): String {
|
||||
for ((k, v) in map) {
|
||||
output[k] = when (v.type) {
|
||||
DataType.LIST -> listTypes(v.value as List<Value>)
|
||||
DataType.MAP -> mapTypes(v.value as Map<Value, Value>)
|
||||
DataType.LIST -> listTypes(
|
||||
v.value as List<Value>
|
||||
)
|
||||
DataType.MAP -> mapTypes(
|
||||
v.value as Map<Value, Value>
|
||||
)
|
||||
else -> v.type.name.toLowerCase()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
package io.smnp.api.model
|
||||
package io.smnp.callable.signature
|
||||
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class ArgumentsList(val signatureMatched: Boolean, val arguments: List<Value>) {
|
||||
operator fun get(index: Int) = arguments[index]
|
||||
@@ -1,8 +1,7 @@
|
||||
package io.smnp.api.signature
|
||||
package io.smnp.callable.signature
|
||||
|
||||
import io.smnp.api.matcher.Matcher
|
||||
import io.smnp.api.model.ArgumentsList
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.type.matcher.Matcher
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
interface Signature {
|
||||
fun parse(arguments: List<Value>): ArgumentsList
|
||||
@@ -1,10 +1,10 @@
|
||||
package io.smnp.api.signature
|
||||
package io.smnp.callable.signature
|
||||
|
||||
import io.smnp.api.matcher.Matcher
|
||||
import io.smnp.api.model.ArgumentsList
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.type.matcher.Matcher
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class SimpleSignature(private vararg val signature: Matcher) : Signature {
|
||||
class SimpleSignature(private vararg val signature: Matcher) :
|
||||
Signature {
|
||||
override fun parse(arguments: List<Value>): ArgumentsList {
|
||||
if (arguments.size > signature.size || arguments.size < signature.count { !it.optional }) {
|
||||
return ArgumentsList.invalid()
|
||||
@@ -1,10 +1,10 @@
|
||||
package io.smnp.api.signature
|
||||
package io.smnp.callable.signature
|
||||
|
||||
import io.smnp.api.matcher.Matcher
|
||||
import io.smnp.api.model.ArgumentsList
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.type.matcher.Matcher
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class VarargSignature(private val varargMatcher: Matcher, private vararg val signature: Matcher) : Signature {
|
||||
class VarargSignature(private val varargMatcher: Matcher, private vararg val signature: Matcher) :
|
||||
Signature {
|
||||
override fun parse(arguments: List<Value>): ArgumentsList {
|
||||
if ((arrayListOf(varargMatcher) + signature).any { it.optional }) {
|
||||
throw RuntimeException("Vararg signature does not support optional arguments")
|
||||
@@ -1,16 +1,13 @@
|
||||
package io.smnp.api.environment
|
||||
package io.smnp.environment
|
||||
|
||||
import io.smnp.api.module.Module
|
||||
import io.smnp.module.loader.ModuleProvider
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
class Environment {
|
||||
private val rootModule = Module("<root>")
|
||||
private val loadedModules = mutableListOf<String>()
|
||||
|
||||
fun loadModule(path: String) {
|
||||
val module = ModuleProvider.getModule(path)
|
||||
rootModule.addSubmodule(module)
|
||||
loadedModules.add(path)
|
||||
|
||||
}
|
||||
|
||||
fun printModules(printContent: Boolean) {
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.error
|
||||
|
||||
import io.smnp.api.callable.Function
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.api.signature.ActualSignatureFormatter.format
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.callable.signature.ActualSignatureFormatter.format
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class FunctionInvocationException(private val function: Function, private val passedArguments: Array<out Value>) : Exception() {
|
||||
override fun toString() = "Invalid signature: ${function.name}${format(passedArguments)}\nAllowed signatures:\n${function.signature}"
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.error
|
||||
|
||||
import io.smnp.api.callable.Method
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.api.signature.ActualSignatureFormatter.format
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.callable.signature.ActualSignatureFormatter.format
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class MethodInvocationException(private val method: Method, private val obj: Value, private val passedArguments: Array<out Value>) : Exception() {
|
||||
override fun toString() = "Invalid signature: $obj.${method.name}${format(passedArguments)}\nAllowed signatures:\n${method.signature}"
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.smnp.api.enumeration
|
||||
package io.smnp.type.enumeration
|
||||
|
||||
import io.smnp.data.entity.Note
|
||||
import kotlin.reflect.KClass
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.smnp.api.matcher
|
||||
package io.smnp.type.matcher
|
||||
|
||||
import io.smnp.api.enumeration.DataType
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.type.enumeration.DataType
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, private val string: String, val optional: Boolean = false) {
|
||||
|
||||
@@ -79,7 +79,15 @@ class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, priv
|
||||
fun listOfMatchers(vararg matchers: Matcher): Matcher {
|
||||
return Matcher(
|
||||
DataType.LIST,
|
||||
{ list -> (list.value as List<Value>).all { item -> matchers.any { it.match(item) } } },
|
||||
{ list ->
|
||||
(list.value as List<Value>).all { item ->
|
||||
matchers.any {
|
||||
it.match(
|
||||
item
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
"list<${matchers.joinToString(", ") { it.toString() }}>"
|
||||
)
|
||||
}
|
||||
@@ -93,7 +101,11 @@ class Matcher(val type: DataType?, private val matcher: (Value) -> Boolean, priv
|
||||
}
|
||||
|
||||
fun allTypes(): Matcher {
|
||||
return Matcher(null, { it.type != DataType.VOID }, "any")
|
||||
return Matcher(
|
||||
null,
|
||||
{ it.type != DataType.VOID },
|
||||
"any"
|
||||
)
|
||||
}
|
||||
|
||||
fun ofTypes(vararg types: DataType): Matcher {
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.api.model
|
||||
package io.smnp.type.model
|
||||
|
||||
import io.smnp.data.entity.Note
|
||||
import io.smnp.api.enumeration.DataType
|
||||
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()) {
|
||||
init {
|
||||
@@ -21,7 +21,10 @@ class Value private constructor(val type: DataType, val value: Any?, val propert
|
||||
}
|
||||
|
||||
fun float(value: Float): Value {
|
||||
return Value(DataType.FLOAT, value)
|
||||
return Value(
|
||||
DataType.FLOAT,
|
||||
value
|
||||
)
|
||||
}
|
||||
|
||||
fun numeric(value: Number): Value {
|
||||
@@ -35,34 +38,38 @@ class Value private constructor(val type: DataType, val value: Any?, val propert
|
||||
fun string(value: String): Value {
|
||||
return Value(
|
||||
DataType.STRING, value, hashMapOf(
|
||||
Pair("length", int(value.length))
|
||||
))
|
||||
Pair("length", int(value.length))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun list(value: List<Value>): Value {
|
||||
return Value(
|
||||
DataType.LIST, value, hashMapOf(
|
||||
Pair("size", int(value.size))
|
||||
))
|
||||
Pair("size", int(value.size))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun map(value: Map<Value, Value>): Value {
|
||||
return Value(
|
||||
DataType.MAP, value, hashMapOf(
|
||||
Pair("size", int(value.size)),
|
||||
Pair("keys", list(value.keys.toList())),
|
||||
Pair("values", list(value.values.toList()))
|
||||
))
|
||||
Pair("size", int(value.size)),
|
||||
Pair("keys", list(value.keys.toList())),
|
||||
Pair("values", list(value.values.toList()))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun note(value: Note): Value {
|
||||
return Value(
|
||||
DataType.NOTE, value, hashMapOf(
|
||||
Pair("pitch", string(value.pitch.toString())),
|
||||
Pair("octave", int(value.octave)),
|
||||
Pair("duration", int(value.duration)),
|
||||
Pair("dot", bool(value.dot))
|
||||
))
|
||||
Pair("pitch", string(value.pitch.toString())),
|
||||
Pair("octave", int(value.octave)),
|
||||
Pair("duration", int(value.duration)),
|
||||
Pair("dot", bool(value.dot))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
fun bool(value: Boolean): Value {
|
||||
@@ -1,8 +1,8 @@
|
||||
package io.smnp.api.module
|
||||
package io.smnp.type.module
|
||||
|
||||
import io.smnp.api.callable.Function
|
||||
import io.smnp.api.callable.Method
|
||||
import io.smnp.api.model.Value
|
||||
import io.smnp.callable.function.Function
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.type.model.Value
|
||||
|
||||
class Module(
|
||||
val name: String,
|
||||
@@ -60,7 +60,12 @@ class Module(
|
||||
|
||||
val moduleChildren = module.children.filter { child -> children.none { it.name == child.name } }
|
||||
|
||||
return Module(name, functions, methods, (commonAndMyChildren + moduleChildren).toMutableList())
|
||||
return Module(
|
||||
name,
|
||||
functions,
|
||||
methods,
|
||||
(commonAndMyChildren + moduleChildren).toMutableList()
|
||||
)
|
||||
}
|
||||
|
||||
fun findFunction(name: String): List<Function> {
|
||||
47
app/build.gradle
Normal file
47
app/build.gradle
Normal file
@@ -0,0 +1,47 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.jetbrains.kotlin.jvm'
|
||||
}
|
||||
|
||||
group 'io.bartek'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile project(':api')
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
|
||||
compile group: 'org.jetbrains.kotlin', name: 'kotlin-reflect', version: '1.3.61'
|
||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||
compile group: 'org.pf4j', name: 'pf4j', version: '3.2.0'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
compileTestKotlin {
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
|
||||
jar {
|
||||
zip64 true
|
||||
manifest {
|
||||
attributes 'Main-Class': 'io.smnp.SMNPKt'
|
||||
}
|
||||
|
||||
// This line of code recursively collects and copies all of a project's files
|
||||
// and adds them to the JAR itself. One can extend this task, to skip certain
|
||||
// files or particular types at will
|
||||
from configurations.compile.collect {
|
||||
exclude "META-INF/*.SF"
|
||||
exclude "META-INF/*.DSA"
|
||||
exclude "META-INF/*.RSA"
|
||||
it.isDirectory() ? it : zipTree(it)
|
||||
}
|
||||
from files(sourceSets.main.output.classesDirs)
|
||||
from files(sourceSets.main.resources)
|
||||
}
|
||||
5
app/src/main/kotlin/io/smnp/SMNP.kt
Normal file
5
app/src/main/kotlin/io/smnp/SMNP.kt
Normal file
@@ -0,0 +1,5 @@
|
||||
package io.smnp
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user