Add support for providing modules written in SMNP language (LanguageModuleProvider extension class)
This commit is contained in:
@@ -8,7 +8,7 @@ import io.smnp.evaluation.evaluator.RootEvaluator
|
||||
import io.smnp.evaluation.model.enumeration.EvaluationResult
|
||||
import java.io.File
|
||||
|
||||
class Interpreter {
|
||||
class DefaultInterpreter : Interpreter {
|
||||
private val tokenizer = DefaultTokenizer()
|
||||
private val parser = RootParser()
|
||||
private val evaluator = RootEvaluator()
|
||||
@@ -46,4 +46,6 @@ class Interpreter {
|
||||
val lines = file.readLines()
|
||||
run(lines, printTokens, printAst, dryRun)
|
||||
}
|
||||
|
||||
override fun run(code: String) = run(code, printTokens = false, printAst = false, dryRun = false)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.smnp.interpreter
|
||||
|
||||
import io.smnp.dsl.ast.parser.RootParser
|
||||
import io.smnp.dsl.token.tokenizer.DefaultTokenizer
|
||||
import io.smnp.environment.DefaultEnvironment
|
||||
import io.smnp.environment.Environment
|
||||
import io.smnp.evaluation.evaluator.Evaluator
|
||||
import io.smnp.evaluation.evaluator.ExtendEvaluator
|
||||
import io.smnp.evaluation.evaluator.FunctionDefinitionEvaluator
|
||||
import io.smnp.evaluation.model.enumeration.EvaluationResult
|
||||
import io.smnp.type.module.Module
|
||||
|
||||
class LanguageModuleInterpreter : Interpreter {
|
||||
private var rootModule = Module.create("<root>")
|
||||
private val tokenizer = DefaultTokenizer()
|
||||
private val parser = RootParser()
|
||||
private val evaluator = Evaluator.repeat(
|
||||
Evaluator.assert(Evaluator.oneOf(
|
||||
FunctionDefinitionEvaluator(),
|
||||
ExtendEvaluator()
|
||||
), "function definition or extend statement")
|
||||
)
|
||||
|
||||
override fun updateRootModule(newRootModule: Module) {
|
||||
rootModule = newRootModule
|
||||
}
|
||||
|
||||
override fun run(code: String) {
|
||||
val lines = code.split("\n")
|
||||
val tokens = tokenizer.tokenize(lines)
|
||||
val ast = parser.parse(tokens)
|
||||
|
||||
val environment = createEnvironment()
|
||||
val result = evaluator.evaluate(ast.node, environment)
|
||||
|
||||
if (result.result == EvaluationResult.FAILED) {
|
||||
throw RuntimeException("Evaluation failed")
|
||||
}
|
||||
}
|
||||
|
||||
private fun createEnvironment(): Environment {
|
||||
val environment = DefaultEnvironment(rootModule)
|
||||
environment.loadModule("smnp.lang")
|
||||
|
||||
return environment
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user