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,11 +2,17 @@ package parser
import com.github.h0tk3y.betterParse.grammar.Grammar
import com.github.h0tk3y.betterParse.grammar.parseToEnd
import interpreter.Preprocessor
import io.Logger
import parser.grammars.LogicGrammar
import prolog.ast.logic.Clause
class ScriptParser: Parser {
private val grammar: Grammar<List<Clause>> = LogicGrammar() as Grammar<List<Clause>>
private val preprocessor = Preprocessor()
override fun parse(input: String): List<Clause> = grammar.parseToEnd(input)
override fun parse(input: String): List<Clause> {
val raw = grammar.parseToEnd(input)
return preprocessor.preprocess(raw)
}
}

View file

@ -1,22 +1,21 @@
package parser.grammars
import com.github.h0tk3y.betterParse.combinators.oneOrMore
import com.github.h0tk3y.betterParse.combinators.or
import com.github.h0tk3y.betterParse.combinators.separated
import com.github.h0tk3y.betterParse.combinators.times
import com.github.h0tk3y.betterParse.combinators.unaryMinus
import com.github.h0tk3y.betterParse.combinators.use
import com.github.h0tk3y.betterParse.combinators.*
import com.github.h0tk3y.betterParse.parser.Parser
import prolog.ast.logic.Clause
import prolog.ast.logic.Fact
import prolog.ast.logic.Rule
import prolog.ast.terms.Atom
class LogicGrammar : TermsGrammar() {
protected val constraint: Parser<Rule> by (-neck * body) use {
Rule(Atom(""), this)
}
protected val rule: Parser<Rule> by (head * -neck * body) use { Rule(t1, t2) }
protected val fact: Parser<Fact> by head use { Fact(this) }
protected val clause: Parser<Clause> by ((rule or fact) * -dot)
protected val clause: Parser<Clause> by ((rule or constraint or fact) * -dot)
protected val clauses: Parser<List<Clause>> by oneOrMore(clause)
override val rootParser: Parser<Any> by clauses
}
}

View file

@ -1,16 +1,10 @@
package parser.grammars
import com.github.h0tk3y.betterParse.combinators.or
import com.github.h0tk3y.betterParse.combinators.separated
import com.github.h0tk3y.betterParse.combinators.times
import com.github.h0tk3y.betterParse.combinators.unaryMinus
import com.github.h0tk3y.betterParse.combinators.use
import com.github.h0tk3y.betterParse.combinators.*
import com.github.h0tk3y.betterParse.grammar.parser
import com.github.h0tk3y.betterParse.parser.Parser
import prolog.ast.arithmetic.Expression
import prolog.ast.arithmetic.Float
import prolog.ast.arithmetic.Integer
import prolog.ast.logic.LogicOperand
import prolog.ast.terms.*
open class TermsGrammar : Tokens() {
@ -37,42 +31,32 @@ open class TermsGrammar : Tokens() {
protected val float: Parser<Float> by floatToken use { Float(text.toFloat()) }
// Operators
protected val logOps: Parser<String> by (dummy
protected val ops: Parser<String> by (dummy
// Logic
or comma
or semicolon
// Arithmetic
or plus
or equals
or notEquals
) use { this.text }
protected val simpleLogicOperand: Parser<LogicOperand> by (dummy
protected val simpleOperand: Parser<Operand> by (dummy
// Logic
or compound
or atom
)
protected val logicOperand: Parser<LogicOperand> by (dummy
or parser(::logicOperator)
or simpleLogicOperand
)
protected val logicOperator: Parser<CompoundTerm> by (simpleLogicOperand * logOps * logicOperand) use {
CompoundTerm(Atom(t2), listOf(t1, t3))
}
protected val arithmeticOps: Parser<String> by (dummy
or plus
) use { this.text }
protected val simpleArithmeticOperand: Parser<Expression> by (dummy
or variable
// Arithmetic
or int
or float
)
protected val arithmeticOperand: Parser<Expression> by (dummy
or parser(::arithmeticOperator)
or simpleArithmeticOperand
) use { this as Expression }
protected val arithmeticOperator: Parser<CompoundTerm> by (simpleArithmeticOperand * arithmeticOps * arithmeticOperand) use {
protected val operand: Parser<Operand> by (dummy
or parser(::operator)
or simpleOperand
)
protected val operator: Parser<CompoundTerm> by (simpleOperand * ops * operand) use {
CompoundTerm(Atom(t2), listOf(t1, t3))
}
protected val operator: Parser<CompoundTerm> by (dummy
or logicOperator
or arithmeticOperator
)
// Parts
protected val head: Parser<Head> by (dummy
or compound
@ -81,6 +65,7 @@ open class TermsGrammar : Tokens() {
protected val body: Parser<Body> by (dummy
or operator
or head
or variable
) use { this as Body }
protected val term: Parser<Term> by (dummy

View file

@ -22,6 +22,8 @@ abstract class Tokens : Grammar<Any>() {
protected val rightParenthesis: Token by literalToken(")")
protected val comma: Token by literalToken(",")
protected val semicolon: Token by literalToken(";")
protected val equals: Token by literalToken("=")
protected val notEquals: Token by literalToken("\\=")
protected val plus: Token by literalToken("+")
protected val dot by literalToken(".")