Arithmetic ops parsing
This commit is contained in:
parent
0a32797df1
commit
bfb509f41f
2 changed files with 31 additions and 14 deletions
|
@ -7,18 +7,12 @@ import com.github.h0tk3y.betterParse.combinators.unaryMinus
|
||||||
import com.github.h0tk3y.betterParse.combinators.use
|
import com.github.h0tk3y.betterParse.combinators.use
|
||||||
import com.github.h0tk3y.betterParse.grammar.parser
|
import com.github.h0tk3y.betterParse.grammar.parser
|
||||||
import com.github.h0tk3y.betterParse.parser.Parser
|
import com.github.h0tk3y.betterParse.parser.Parser
|
||||||
|
import prolog.ast.arithmetic.ArithmeticOperator
|
||||||
|
import prolog.ast.arithmetic.Expression
|
||||||
import prolog.ast.arithmetic.Float
|
import prolog.ast.arithmetic.Float
|
||||||
import prolog.ast.arithmetic.Integer
|
import prolog.ast.arithmetic.Integer
|
||||||
import prolog.ast.logic.LogicOperand
|
import prolog.ast.logic.LogicOperand
|
||||||
import prolog.ast.logic.LogicOperator
|
import prolog.ast.terms.*
|
||||||
import prolog.ast.terms.Atom
|
|
||||||
import prolog.ast.terms.Body
|
|
||||||
import prolog.ast.terms.Head
|
|
||||||
import prolog.ast.terms.Operator
|
|
||||||
import prolog.ast.terms.Structure
|
|
||||||
import prolog.ast.terms.Term
|
|
||||||
import prolog.ast.terms.Variable
|
|
||||||
import prolog.builtins.Conjunction
|
|
||||||
|
|
||||||
open class TermsGrammar : Tokens() {
|
open class TermsGrammar : Tokens() {
|
||||||
// Basic named terms
|
// Basic named terms
|
||||||
|
@ -37,19 +31,40 @@ open class TermsGrammar : Tokens() {
|
||||||
protected val float: Parser<Float> by floatToken use { Float(text.toFloat()) }
|
protected val float: Parser<Float> by floatToken use { Float(text.toFloat()) }
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
|
protected val logOps: Parser<String> by (dummy
|
||||||
|
or comma
|
||||||
|
or semicolon
|
||||||
|
) use { this.text }
|
||||||
protected val simpleLogicOperand: Parser<LogicOperand> by (dummy
|
protected val simpleLogicOperand: Parser<LogicOperand> by (dummy
|
||||||
or compound
|
or compound
|
||||||
or atom
|
or atom
|
||||||
)
|
)
|
||||||
protected val logicOperand: Parser<LogicOperand> by (dummy
|
protected val logicOperand: Parser<LogicOperand> by (dummy
|
||||||
or parser(::operator)
|
or parser(::logicOperator)
|
||||||
or simpleLogicOperand
|
or simpleLogicOperand
|
||||||
)
|
)
|
||||||
protected val logicOperator: Parser<LogicOperator> by (simpleLogicOperand * -comma * logicOperand) use {
|
protected val logicOperator: Parser<CompoundTerm> by (simpleLogicOperand * logOps * logicOperand) use {
|
||||||
Conjunction(t1, t2)
|
CompoundTerm(Atom(t2), listOf(t1, t3))
|
||||||
}
|
}
|
||||||
protected val operator: Parser<Operator> by (dummy
|
|
||||||
|
protected val arithmeticOps: Parser<String> by (dummy
|
||||||
|
or plus
|
||||||
|
) use { this.text }
|
||||||
|
protected val simpleArithmeticOperand: Parser<Expression> by (dummy
|
||||||
|
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 {
|
||||||
|
CompoundTerm(Atom(t2), listOf(t1, t3))
|
||||||
|
}
|
||||||
|
|
||||||
|
protected val operator: Parser<CompoundTerm> by (dummy
|
||||||
or logicOperator
|
or logicOperator
|
||||||
|
or arithmeticOperator
|
||||||
)
|
)
|
||||||
|
|
||||||
// Parts
|
// Parts
|
||||||
|
|
|
@ -18,9 +18,11 @@ abstract class Tokens : Grammar<Any>() {
|
||||||
|
|
||||||
// Special tokens
|
// Special tokens
|
||||||
protected val neck by literalToken(":-")
|
protected val neck by literalToken(":-")
|
||||||
protected val comma: Token by literalToken(",")
|
|
||||||
protected val leftParenthesis: Token by literalToken("(")
|
protected val leftParenthesis: Token by literalToken("(")
|
||||||
protected val rightParenthesis: Token by literalToken(")")
|
protected val rightParenthesis: Token by literalToken(")")
|
||||||
|
protected val comma: Token by literalToken(",")
|
||||||
|
protected val semicolon: Token by literalToken(";")
|
||||||
|
protected val plus: Token by literalToken("+")
|
||||||
protected val dot by literalToken(".")
|
protected val dot by literalToken(".")
|
||||||
|
|
||||||
// Ignored tokens
|
// Ignored tokens
|
||||||
|
|
Reference in a new issue