Checkpoint

This commit is contained in:
Tibo De Peuter 2025-05-01 17:13:35 +02:00
parent 43b364044e
commit 9db1c66781
Signed by: tdpeuter
GPG key ID: 38297DE43F75FFE2
34 changed files with 746 additions and 194 deletions

View file

@ -2,22 +2,27 @@ package interpreter
import io.Logger
import parser.ScriptParser
import prolog.ast.Database
import prolog.Program
import prolog.ast.logic.Clause
class FileLoader {
private val parser = ScriptParser()
fun load(filePath: String): () -> Unit {
fun load(filePath: String) {
Logger.info("Loading file: $filePath")
val input = readFile(filePath)
Logger.debug("Parsing content of $filePath")
val clauses: List<Clause> = parser.parse(input)
Program.load(clauses)
val db = Database(filePath)
db.load(clauses)
Program.add(db)
db.initialize()
// TODO Pass next commands to execute
return {}
Logger.debug("Finished loading file: $filePath")
}
fun readFile(filePath: String): String {

View file

@ -47,7 +47,7 @@ open class Preprocessor {
}
}
protected open fun preprocess(term: Term): Term {
protected open fun preprocess(term: Term, nested: Boolean = false): Term {
val prepped = when (term) {
Atom("true") -> True
Structure(Atom("true"), emptyList()) -> True
@ -61,7 +61,7 @@ open class Preprocessor {
Atom("nl") -> Nl
is Structure -> {
// Preprocess the arguments first to recognize builtins
val args = term.arguments.map { preprocess(it) }
val args = term.arguments.map { preprocess(it, nested = true) }
when {
// TODO Remove hardcoding by storing the functors as constants in operators?
@ -77,7 +77,7 @@ open class Preprocessor {
term.functor == "\\+/1" -> {
Not(args[0] as Goal)
}
// Arithmetic
term.functor == "=\\=/2" && args.all { it is Expression } -> {
EvaluatesToDifferent(args[0] as Expression, args[1] as Expression)
}
@ -90,6 +90,16 @@ open class Preprocessor {
Is(args[0] as Expression, args[1] as Expression)
}
// Arithmetic
term.functor == "=/2" && args.all { it is Expression } -> {
Unify(args[0] as Expression, args[1] as Expression)
}
term.functor == "\\=/2" && args.all { it is Expression } -> {
NotUnify(args[0] as Expression, args[1] as Expression)
}
term.functor == "-/1" && args.all { it is Expression } -> {
Negate(args[0] as Expression)
}
@ -121,6 +131,7 @@ open class Preprocessor {
// Other
term.functor == "write/1" -> Write(args[0])
term.functor == "read/1" -> Read(args[0])
term.functor == "initialization/1" -> Initialization(args[0] as Goal)
else -> term
}
@ -129,9 +140,10 @@ open class Preprocessor {
else -> term
}
if (prepped != term || prepped::class != term::class) {
Logger.debug("Preprocessed term: $term -> $prepped (is ${prepped::class.simpleName})")
}
Logger.debug(
"Preprocessed term $term into $prepped (kind ${prepped::class.simpleName})",
!nested && (prepped != term || prepped::class != term::class)
)
return prepped
}