Add support for providing modules written in SMNP language (LanguageModuleProvider extension class)

This commit is contained in:
2020-03-13 12:16:33 +01:00
parent 9e4c9d3b11
commit 9b79d6ef7d
14 changed files with 171 additions and 78 deletions

View File

@@ -0,0 +1,20 @@
package io.smnp.ext
import io.smnp.interpreter.Interpreter
import io.smnp.type.module.Module
abstract class LanguageModuleProvider(path: String) : ModuleProvider(path) {
open fun files() = listOf("main.mus")
override fun provideModule(interpreter: Interpreter): Module {
val module = Module.create(path)
interpreter.updateRootModule(module)
files()
.map { javaClass.classLoader.getResource(it) }
.map { it.readText() }
.forEach { interpreter.run(it) }
return module
}
}

View File

@@ -1,14 +0,0 @@
package io.smnp.ext
import io.smnp.callable.function.Function
import io.smnp.callable.method.Method
import io.smnp.type.module.Module
import org.pf4j.ExtensionPoint
abstract class ModuleDefinition(val path: String) : ExtensionPoint {
open fun functions(): List<Function> = emptyList()
open fun methods(): List<Method> = emptyList()
open fun dependencies(): List<String> = emptyList()
fun module(): Module = Module.create(path, functions(), methods())
}

View File

@@ -0,0 +1,10 @@
package io.smnp.ext
import io.smnp.interpreter.Interpreter
import io.smnp.type.module.Module
import org.pf4j.ExtensionPoint
abstract class ModuleProvider(val path: String) : ExtensionPoint {
open fun dependencies(): List<String> = emptyList()
abstract fun provideModule(interpreter: Interpreter): Module
}

View File

@@ -1,6 +1,6 @@
package io.smnp.ext
interface ModuleRegistry {
fun requestModulesForPath(path: String): ModuleDefinition?
fun requestModulesForPath(path: String): ModuleProvider?
fun registeredModules(): List<String>
}

View File

@@ -0,0 +1,13 @@
package io.smnp.ext
import io.smnp.callable.function.Function
import io.smnp.callable.method.Method
import io.smnp.interpreter.Interpreter
import io.smnp.type.module.Module
abstract class NativeModuleProvider(path: String) : ModuleProvider(path) {
open fun functions(): List<Function> = emptyList()
open fun methods(): List<Method> = emptyList()
final override fun provideModule(interpreter: Interpreter) = Module.create(path, functions(), methods())
}

View File

@@ -0,0 +1,8 @@
package io.smnp.interpreter
import io.smnp.type.module.Module
interface Interpreter {
fun run(code: String)
fun updateRootModule(newRootModule: Module): Unit = throw RuntimeException("Replacing root module is not supported in this Interpreter implementation")
}