Refactor LanguageModuleInterpreter and its dependencies

This commit is contained in:
2020-03-13 13:07:30 +01:00
parent 1c7b56159a
commit a5fba7e8b1
7 changed files with 43 additions and 39 deletions

View File

@@ -3,18 +3,21 @@ package io.smnp.environment
import io.smnp.callable.function.Function
import io.smnp.callable.method.Method
import io.smnp.type.model.Value
import io.smnp.type.module.Module
interface Environment {
fun loadModule(path: String)
fun printModules(printContent: Boolean)
fun invokeFunction(name: String, arguments: List<Value>): Value
fun invokeMethod(obj: Value, name: String, arguments: List<Value>): Value
fun printCallStack()
fun defineFunction(function: Function)
fun defineMethod(method: Method)
fun pushScope(scope: MutableMap<String, Value> = mutableMapOf())
fun popScope(): Map<String, Value>?
fun printScopes()
fun setVariable(name: String, value: Value)
fun getVariable(name: String): Value
fun loadModule(path: String)
fun printModules(printContent: Boolean)
fun invokeFunction(name: String, arguments: List<Value>): Value
fun invokeMethod(obj: Value, name: String, arguments: List<Value>): Value
fun printCallStack()
fun defineFunction(function: Function)
fun defineMethod(method: Method)
fun pushScope(scope: MutableMap<String, Value> = mutableMapOf())
fun popScope(): Map<String, Value>?
fun printScopes()
fun setVariable(name: String, value: Value)
fun getVariable(name: String): Value
fun getRootModule(): Module
}

View File

@@ -7,14 +7,20 @@ 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)
val segments = path.split(".")
val parentNodesChainPath = segments.dropLast(1).joinToString(".")
val moduleName = segments.last()
files()
val module = files()
.asSequence()
.map { javaClass.classLoader.getResource(it) }
.map { it.readText() }
.forEach { interpreter.run(it) }
.map { interpreter.run(it) }
.map { it.getRootModule() }
.reduce { acc, module -> acc.merge(module) }
return module
module.name = moduleName
return Module.create(parentNodesChainPath, children = listOf(module))
}
}

View File

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

View File

@@ -5,7 +5,7 @@ import io.smnp.callable.method.Method
import io.smnp.type.model.Value
class Module(
val name: String,
var name: String,
functions: List<Function> = emptyList(),
methods: List<Method> = emptyList(),
children: List<Module> = emptyList()