Create base for CLI

This commit is contained in:
2020-03-14 23:36:19 +01:00
parent 86fc8ae086
commit 4ce35ce34c
8 changed files with 67 additions and 7 deletions

View File

@@ -1,9 +1,35 @@
package io.smnp
import com.xenomachina.argparser.ArgParser
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.ext.DefaultModuleRegistry
import io.smnp.interpreter.DefaultInterpreter
import java.io.File;
fun main(args: Array<String>) {
val interpreter = DefaultInterpreter()
interpreter.run(File("/home/bartek/Developent/SMNP-Kotlin/examples/scratchpad.mus"))
fun main(args: Array<String>): Unit = mainBody {
ArgParser(args).parseInto(::Arguments).run {
val interpreter = DefaultInterpreter()
when {
file != null -> interpreter.run(file!!, printTokens, printAst, dryRun)
code != null -> interpreter.run(code!!, printTokens, printAst, dryRun)
else -> null
}?.let { it as DefaultEnvironment }?.let { environment ->
if(loadedModules != null) {
println("Loaded modules:")
when (loadedModules) {
ModulesPrintMode.LIST -> environment.modules.forEach { println(it) }
ModulesPrintMode.TREE -> environment.printModules(false)
ModulesPrintMode.CONTENT -> environment.printModules(true)
}
}
}
if (availableModules) {
println("Available modules:")
DefaultModuleRegistry.registeredModules().forEach { println(it) }
}
}
}

View File

@@ -0,0 +1,22 @@
package io.smnp.cli.model.entity
import com.xenomachina.argparser.ArgParser
import com.xenomachina.argparser.default
import io.smnp.cli.model.enumeration.ModulesPrintMode
import java.io.File
class Arguments(parser: ArgParser) {
val printTokens by parser.flagging("--tokens", help = "print tokens").default(false)
val printAst by parser.flagging("--ast", help = "print abstract syntax tree").default(false)
val dryRun by parser.flagging("--dry-run", help = "don't evaluate parsed code").default(false)
val availableModules by parser.flagging("--modules-all", help = "print all available modules").default(false)
val loadedModules by parser.storing(
"--modules",
argName = "MODE",
help = "print modules loaded by executed script. Allowed values for MODE are: list | tree | content. The 'list' option " +
"prints loaded modules as list of canonical names of each one. The 'tree' option organises modules into the tree model " +
"and then prints them. The 'content' option is the same as tree, however, prints also contained functions and methods."
) { ModulesPrintMode.valueOf(this.toUpperCase()) }.default<ModulesPrintMode?>(null)
val code by parser.storing("-c", "--code", help = "inline code to be executed").default<String?>(null)
val file by parser.positional("SOURCE", help = "file with SMNP language code to be executed") { File(this) }.default<File?>(null)
}

View File

@@ -0,0 +1,7 @@
package io.smnp.cli.model.enumeration
enum class ModulesPrintMode {
LIST,
TREE,
CONTENT
}

View File

@@ -20,6 +20,9 @@ class DefaultEnvironment : Environment {
var disposed = false
private set
val modules: List<String>
get() = loadedModules
init {
callStack.push(rootModule, "<entrypoint>", emptyList())
}

View File

@@ -3,9 +3,11 @@ package io.smnp.ext
import io.smnp.environment.Environment
import io.smnp.error.ModuleException
import org.pf4j.DefaultPluginManager
import java.nio.file.Paths
object DefaultModuleRegistry : ModuleRegistry {
private val pluginManager = DefaultPluginManager()
private val MODULES_DIR = Paths.get(System.getProperty("smnp.modulesDir") ?: "modules")
private val pluginManager = DefaultPluginManager(MODULES_DIR)
private val modules = mutableMapOf<String, ModuleProvider>()
init {
pluginManager.loadPlugins()