Perform some code refactor
This commit is contained in:
@@ -10,10 +10,10 @@ abstract class Function(val name: String) {
|
||||
private var definitions: List<FunctionDefinition> = mutableListOf()
|
||||
private var _module: Module? = null
|
||||
var module: Module
|
||||
get() = _module ?: throw RuntimeException("Method has not set module yet")
|
||||
get() = _module ?: throw RuntimeException("Function has not set module yet")
|
||||
set(value) {
|
||||
if (_module != null) {
|
||||
throw RuntimeException("Module of method is already set to ${module.canonicalName}")
|
||||
throw RuntimeException("Function of method is already set to ${module.canonicalName}")
|
||||
}
|
||||
_module = value
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ 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
|
||||
|
||||
interface ModuleDefinition : ExtensionPoint {
|
||||
fun modulePath(): String
|
||||
fun functions(): List<Function>
|
||||
fun methods(): List<Method>
|
||||
abstract class ModuleDefinition(val path: String) : ExtensionPoint {
|
||||
open fun functions(): List<Function> = emptyList()
|
||||
open fun methods(): List<Method> = emptyList()
|
||||
|
||||
fun module(): Module = Module.create(path, functions(), methods())
|
||||
}
|
||||
@@ -37,7 +37,8 @@ class DefaultEnvironment : Environment {
|
||||
}
|
||||
|
||||
if(foundFunctions.size > 1) {
|
||||
throw RuntimeException("Found ${foundFunctions.size} functions with name of $foundFunctions")
|
||||
throw RuntimeException("Found ${foundFunctions.size} functions with name of $name: [${foundFunctions.map { it.module.canonicalName }.joinToString()}]")
|
||||
|
||||
}
|
||||
|
||||
val function = foundFunctions[0]
|
||||
@@ -56,7 +57,7 @@ class DefaultEnvironment : Environment {
|
||||
}
|
||||
|
||||
if(foundMethods.size > 1) {
|
||||
throw RuntimeException("Found ${foundMethods.size} methods with name of '$foundMethods' for ${format(obj)}")
|
||||
throw RuntimeException("Found ${foundMethods.size} methods with name of $name for ${format(obj)}: [${foundMethods.map { it.module.canonicalName }.joinToString()}]")
|
||||
}
|
||||
|
||||
val method = foundMethods[0]
|
||||
|
||||
@@ -10,12 +10,12 @@ class RootEvaluator : Evaluator() {
|
||||
override fun supportedNodes() = listOf(RootNode::class)
|
||||
|
||||
override fun tryToEvaluate(node: Node, environment: Environment): EvaluatorOutput {
|
||||
val evaluator = oneOf(
|
||||
val evaluator = assert(oneOf(
|
||||
ImportEvaluator(),
|
||||
ExpressionEvaluator(),
|
||||
FunctionDefinitionEvaluator(),
|
||||
DefaultEvaluator()
|
||||
)
|
||||
), "correct statement")
|
||||
|
||||
for(child in node.children) {
|
||||
val output = evaluator.evaluate(child, environment)
|
||||
|
||||
@@ -11,7 +11,7 @@ object DefaultModuleRegistry : ModuleRegistry {
|
||||
pluginManager.startPlugins()
|
||||
|
||||
pluginManager.getExtensions(ModuleDefinition::class.java).forEach {
|
||||
modules.add(Pair(it.modulePath(), Module.create(it.modulePath(), it.functions(), it.methods())))
|
||||
modules.add(it.path to it.module())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import io.smnp.dsl.token.tokenizer.DefaultTokenizer
|
||||
import io.smnp.environment.DefaultEnvironment
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.evaluation.evaluator.RootEvaluator
|
||||
import io.smnp.evaluation.model.enumeration.EvaluationResult
|
||||
import java.io.File
|
||||
|
||||
class Interpreter {
|
||||
@@ -18,7 +19,11 @@ class Interpreter {
|
||||
val ast = parser.parse(tokens)
|
||||
|
||||
val environment = createEnvironment()
|
||||
evaluator.evaluate(ast.node, environment)
|
||||
val result = evaluator.evaluate(ast.node, environment)
|
||||
|
||||
if(result.result == EvaluationResult.FAILED) {
|
||||
throw RuntimeException("Evaluation failed")
|
||||
}
|
||||
}
|
||||
|
||||
private fun createEnvironment(): Environment {
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
package io.smnp.ext.io
|
||||
|
||||
import io.smnp.callable.method.Method
|
||||
import io.smnp.ext.ModuleDefinition
|
||||
import io.smnp.ext.io.function.PrintlnFunction
|
||||
import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class IoModule : ModuleDefinition {
|
||||
override fun modulePath() = "smnp.io"
|
||||
|
||||
class IoModule : ModuleDefinition("smnp.io") {
|
||||
override fun functions() = listOf(PrintlnFunction())
|
||||
|
||||
override fun methods() = emptyList<Method>()
|
||||
}
|
||||
@@ -7,10 +7,7 @@ import io.smnp.ext.lang.method.MapAccessMethod
|
||||
import org.pf4j.Extension
|
||||
|
||||
@Extension
|
||||
class LangModule : ModuleDefinition {
|
||||
override fun modulePath() = "smnp.lang"
|
||||
|
||||
class LangModule : ModuleDefinition("smnp.lang") {
|
||||
override fun functions() = listOf(DebugFunction())
|
||||
|
||||
override fun methods() = listOf(ListAccessMethod(), MapAccessMethod())
|
||||
}
|
||||
Reference in New Issue
Block a user