Move global error handling to root main() function
This commit is contained in:
@@ -5,37 +5,45 @@ import com.xenomachina.argparser.mainBody
|
||||
import io.smnp.cli.model.entity.Arguments
|
||||
import io.smnp.cli.model.enumeration.ModulesPrintMode
|
||||
import io.smnp.environment.DefaultEnvironment
|
||||
import io.smnp.error.SmnpException
|
||||
import io.smnp.ext.DefaultModuleRegistry
|
||||
import io.smnp.interpreter.DefaultInterpreter
|
||||
import io.smnp.preset.PresetProvider.providePresetCode
|
||||
import io.smnp.type.model.Value
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
fun main(args: Array<String>): Unit = mainBody {
|
||||
ArgParser(args).parseInto(::Arguments).run {
|
||||
val interpreter = DefaultInterpreter()
|
||||
val environment = DefaultEnvironment()
|
||||
try {
|
||||
val interpreter = DefaultInterpreter()
|
||||
val environment = DefaultEnvironment()
|
||||
|
||||
environment.setVariable("__param__", Value.wrap(parameters.toMap()))
|
||||
environment.setVariable("__param__", Value.wrap(parameters.toMap()))
|
||||
|
||||
when {
|
||||
file != null -> interpreter.run(file!!, environment, printTokens, printAst, dryRun)
|
||||
code != null -> interpreter.run(code!!, environment, printTokens, printAst, dryRun)
|
||||
preset != null -> interpreter.run(providePresetCode(preset!!), environment, printTokens, printAst, dryRun)
|
||||
else -> null
|
||||
}?.let { it as DefaultEnvironment }?.let { disposedEnvironment ->
|
||||
if(loadedModules != null) {
|
||||
println("Loaded modules:")
|
||||
when (loadedModules) {
|
||||
ModulesPrintMode.LIST -> disposedEnvironment.modules.forEach { println(it) }
|
||||
ModulesPrintMode.TREE -> disposedEnvironment.printModules(false)
|
||||
ModulesPrintMode.CONTENT -> disposedEnvironment.printModules(true)
|
||||
when {
|
||||
file != null -> interpreter.run(file!!, environment, printTokens, printAst, dryRun)
|
||||
code != null -> interpreter.run(code!!, environment, printTokens, printAst, dryRun)
|
||||
preset != null -> interpreter.run(providePresetCode(preset!!), environment, printTokens, printAst, dryRun)
|
||||
else -> null
|
||||
}?.let { it as DefaultEnvironment }?.let { disposedEnvironment ->
|
||||
if (loadedModules != null) {
|
||||
println("Loaded modules:")
|
||||
when (loadedModules) {
|
||||
ModulesPrintMode.LIST -> disposedEnvironment.modules.forEach { println(it) }
|
||||
ModulesPrintMode.TREE -> disposedEnvironment.printModules(false)
|
||||
ModulesPrintMode.CONTENT -> disposedEnvironment.printModules(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (availableModules) {
|
||||
println("Available modules:")
|
||||
DefaultModuleRegistry.registeredModules().forEach { println(it) }
|
||||
if (availableModules) {
|
||||
println("Available modules:")
|
||||
DefaultModuleRegistry.registeredModules().forEach { println(it) }
|
||||
}
|
||||
} catch (e: SmnpException) {
|
||||
System.err.println(e.friendlyName)
|
||||
System.err.println(e.message)
|
||||
exitProcess(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,9 @@ 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.error.SmnpException
|
||||
import io.smnp.evaluation.evaluator.RootEvaluator
|
||||
import io.smnp.evaluation.model.enumeration.EvaluationResult
|
||||
import java.io.File
|
||||
import java.lang.System.err
|
||||
import kotlin.system.exitProcess
|
||||
|
||||
class DefaultInterpreter : Interpreter {
|
||||
private val tokenizer = DefaultTokenizer()
|
||||
@@ -28,20 +25,6 @@ class DefaultInterpreter : Interpreter {
|
||||
}
|
||||
|
||||
private fun run(lines: List<String>, environment: Environment, printTokens: Boolean, printAst: Boolean, dryRun: Boolean): Environment {
|
||||
try {
|
||||
return tryToRun(lines, environment, printTokens, printAst, dryRun)
|
||||
} catch (e: SmnpException) {
|
||||
printError(e)
|
||||
exitProcess(1)
|
||||
}
|
||||
}
|
||||
|
||||
private fun printError(e: SmnpException) {
|
||||
err.println(e.friendlyName)
|
||||
err.println(e.message)
|
||||
}
|
||||
|
||||
private fun tryToRun(lines: List<String>, environment: Environment, printTokens: Boolean, printAst: Boolean, dryRun: Boolean): Environment {
|
||||
environment.loadModule("smnp.lang")
|
||||
|
||||
val tokens = tokenizer.tokenize(lines)
|
||||
|
||||
Reference in New Issue
Block a user