Argument parsing
This commit is contained in:
parent
32165a90f5
commit
8e6a34a231
5 changed files with 63 additions and 8 deletions
42
src/Main.kt
42
src/Main.kt
|
@ -1,15 +1,45 @@
|
|||
import com.sun.org.apache.bcel.internal.util.Args
|
||||
import com.xenomachina.argparser.ArgParser
|
||||
import com.xenomachina.argparser.mainBody
|
||||
import interpreter.FileLoader
|
||||
import io.GhentPrologArgParser
|
||||
import io.Logger
|
||||
import prolog.Program
|
||||
import prolog.ast.logic.Clause
|
||||
import repl.Repl
|
||||
|
||||
fun main() {
|
||||
// TODO Make this a command line argument
|
||||
// Turn on debug mode
|
||||
Logger.level = Logger.Level.DEBUG
|
||||
fun main(args: Array<String>) = mainBody {
|
||||
// Parse command line arguments
|
||||
val parsedArgs = ArgParser(args).parseInto(::GhentPrologArgParser)
|
||||
|
||||
FileLoader().load("tests/parser/resources/parent.pl")
|
||||
parsedArgs.run {
|
||||
val loader = FileLoader()
|
||||
|
||||
Repl().start()
|
||||
// Set the verbosity level
|
||||
Logger.level = verbosity
|
||||
|
||||
// Check if script was provided
|
||||
for (file in script) {
|
||||
loader.load(file)
|
||||
}
|
||||
|
||||
// Check if REPL was requested
|
||||
if (repl) {
|
||||
Repl().start()
|
||||
} else {
|
||||
Logger.warn("REPL not started. Use -r or --repl to start the REPL.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun help() {
|
||||
println("""
|
||||
Ghent Prolog: A Prolog interpreter in Kotlin
|
||||
|
||||
Options:
|
||||
-s, --source <file> Specify the source file to load
|
||||
-r, --repl Start the REPL (default)
|
||||
-v, --verb
|
||||
-h, --help Show this help message
|
||||
""".trimIndent())
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ open class Preprocessor {
|
|||
}
|
||||
|
||||
if (prepped != term || prepped::class != term::class) {
|
||||
Logger.debug("Preprocessed term: $term -> $prepped (${prepped::class})")
|
||||
Logger.debug("Preprocessed term: $term -> $prepped (is ${prepped::class.simpleName})")
|
||||
}
|
||||
|
||||
return prepped
|
||||
|
|
21
src/io/GhentPrologArgParser.kt
Normal file
21
src/io/GhentPrologArgParser.kt
Normal file
|
@ -0,0 +1,21 @@
|
|||
package io
|
||||
|
||||
import com.xenomachina.argparser.ArgParser
|
||||
import com.xenomachina.argparser.default
|
||||
|
||||
class GhentPrologArgParser(parser: ArgParser) {
|
||||
val script by parser.adding("-s", "--script", help = "Script to run")
|
||||
val repl by parser.flagging("-r", "--repl", help = "Start the REPL")
|
||||
|
||||
val verbosity by parser.mapping(
|
||||
"--vvv" to Logger.Level.DEBUG,
|
||||
"--debug" to Logger.Level.DEBUG,
|
||||
"--vv" to Logger.Level.INFO,
|
||||
"--verbose" to Logger.Level.INFO,
|
||||
"--info" to Logger.Level.INFO,
|
||||
"-v" to Logger.Level.WARN,
|
||||
"--warn" to Logger.Level.WARN,
|
||||
"--error" to Logger.Level.ERROR,
|
||||
help = "Set the verbosity level (default: WARN)",
|
||||
).default(Logger.defaultLevel)
|
||||
}
|
|
@ -144,7 +144,7 @@ class Divide(private val expr1: Expression, private val expr2: Expression) :
|
|||
// TODO Expr rem Expr
|
||||
|
||||
class Between(private val expr1: Expression, private val expr2: Expression, private val expr3: Expression) :
|
||||
Operator(Atom("between"), expr1, expr2) {
|
||||
CompoundTerm(Atom("between"), listOf(expr1, expr2, expr3)), Satisfiable {
|
||||
override fun satisfy(subs: Substitutions): Answers {
|
||||
val e1 = expr1.simplify(subs)
|
||||
val e2 = expr2.simplify(subs)
|
||||
|
@ -165,4 +165,6 @@ class Between(private val expr1: Expression, private val expr2: Expression, priv
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString(): String = "$expr1..$expr3..$expr2"
|
||||
}
|
||||
|
|
Reference in a new issue