Checkpoint
This commit is contained in:
parent
3724ac72f9
commit
7daae860fc
8 changed files with 119 additions and 9 deletions
|
@ -5,8 +5,11 @@ import com.github.h0tk3y.betterParse.grammar.parser
|
|||
import com.github.h0tk3y.betterParse.parser.Parser
|
||||
import prolog.ast.arithmetic.Float
|
||||
import prolog.ast.arithmetic.Integer
|
||||
import prolog.ast.lists.List
|
||||
import prolog.ast.terms.*
|
||||
import prolog.builtins.Dynamic
|
||||
import prolog.ast.lists.List.Empty
|
||||
import prolog.ast.lists.List.Cons
|
||||
|
||||
/**
|
||||
* Precedence is based on the following table:
|
||||
|
@ -65,6 +68,7 @@ open class TermsGrammar : Tokens() {
|
|||
// Base terms (atoms, compounds, variables, numbers)
|
||||
protected val baseTerm: Parser<Term> by (dummy
|
||||
or (-leftParenthesis * parser(::term) * -rightParenthesis)
|
||||
or parser(::list)
|
||||
or compound
|
||||
or atom
|
||||
or variable
|
||||
|
@ -112,6 +116,20 @@ open class TermsGrammar : Tokens() {
|
|||
t2.fold(t1) { acc, (op, term) -> CompoundTerm(Atom(op), listOf(acc, term)) }
|
||||
}
|
||||
|
||||
// Lists
|
||||
protected val list: Parser<List> by (-leftBracket * separated(
|
||||
parser(::termNoConjunction),
|
||||
comma,
|
||||
acceptZero = true
|
||||
) * -rightBracket) use {
|
||||
var list: List = Empty
|
||||
// Construct the list in reverse order
|
||||
for (term in this.terms.reversed()) {
|
||||
list = Cons(term, list)
|
||||
}
|
||||
list
|
||||
}
|
||||
|
||||
// Term - highest level expression
|
||||
protected val term: Parser<Term> by term1200
|
||||
protected val termNoConjunction: Parser<Term> by term700
|
||||
|
|
|
@ -10,6 +10,8 @@ import com.github.h0tk3y.betterParse.lexer.token
|
|||
abstract class Tokens : Grammar<Any>() {
|
||||
protected val leftParenthesis: Token by literalToken("(")
|
||||
protected val rightParenthesis: Token by literalToken(")")
|
||||
protected val leftBracket: Token by literalToken("[")
|
||||
protected val rightBracket: Token by literalToken("]")
|
||||
protected val exclamation: Token by literalToken("!")
|
||||
// 1200
|
||||
protected val neck by literalToken(":-")
|
||||
|
|
Reference in a new issue