Checkpoint
This commit is contained in:
parent
5bfa1691dd
commit
a85169dced
27 changed files with 377 additions and 250 deletions
|
@ -6,30 +6,32 @@ import com.github.h0tk3y.betterParse.parser.Parser
|
|||
import prolog.ast.arithmetic.Float
|
||||
import prolog.ast.arithmetic.Integer
|
||||
import prolog.ast.terms.*
|
||||
import prolog.builtins.Dynamic
|
||||
|
||||
/**
|
||||
* Precedence is based on the following table:
|
||||
*
|
||||
* | Precedence | Type | Operators |
|
||||
* |------------|------|-----------------------------------------------------------------------------------------------|
|
||||
* | 1200 | xfx | --\>, :-, =\>, ==\> |
|
||||
* | 1200 | fx | :-, ?- |
|
||||
* | 1105 | xfy | \| |
|
||||
* | 1100 | xfy | ; |
|
||||
* | 1050 | xfy | -\>, \*-\> |
|
||||
* | 1000 | xfy | , |
|
||||
* | 990 | xfx | := |
|
||||
* | 900 | fy | \\+ |
|
||||
* | 700 | xfx | \<, =, =.., =:=, =\<, ==, =\\=, \>, \>=, \\=, \\==, as, is, \>:\<, :\< |
|
||||
* | 600 | xfy | : |
|
||||
* | 500 | yfx | +, -, /\\, \\/, xor |
|
||||
* | 500 | fx | ? |
|
||||
* | 400 | yfx | \*, /, //, div, rdiv, \<\<, \>\>, mod, rem |
|
||||
* | 200 | xfx | \*\* |
|
||||
* | 200 | xfy | ^ |
|
||||
* | 200 | fy | +, -, \\ |
|
||||
* | 100 | yfx | . |
|
||||
* | 1 | fx | $ |
|
||||
* | 1200 | xfx | --\>, :-, =\>, ==\> |
|
||||
* | 1200 | fx | :-, ?- |
|
||||
* | 1150 | fx | dynamic |
|
||||
* | 1105 | xfy | | |
|
||||
* | 1100 | xfy | ; |
|
||||
* | 1050 | xfy | ->, *-> |
|
||||
* | 1000 | xfy | , |
|
||||
* | 990 | xfx | := |
|
||||
* | 900 | fy | \+ |
|
||||
* | 700 | xfx | <, =, =.., =:=, =<, ==, =\=, >, >=, \=, \==, as, is, >:<, :< |
|
||||
* | 600 | xfy | : |
|
||||
* | 500 | yfx | +, -, /\, \/, xor |
|
||||
* | 500 | fx | ? |
|
||||
* | 400 | yfx | *, /, //, div, rdiv, <<, >>, mod, rem |
|
||||
* | 200 | xfx | ** |
|
||||
* | 200 | xfy | ^ |
|
||||
* | 200 | fy | +, -, \ |
|
||||
* | 100 | yfx | . |
|
||||
* | 1 | fx | $ |
|
||||
*
|
||||
* It is very easy to extend this grammar to support more operators. Just add them at the appropriate rule or create a
|
||||
* new rule and chain it to the existing ones.
|
||||
|
@ -58,6 +60,8 @@ open class TermsGrammar : Tokens() {
|
|||
protected val int: Parser<Integer> by integerToken use { Integer(text.toInt()) }
|
||||
protected val float: Parser<Float> by floatToken use { Float(text.toFloat()) }
|
||||
|
||||
protected val functor: Parser<String> by (nameToken * divide * int) use { "${t1.text}${t2.text}$t3" }
|
||||
|
||||
// Base terms (atoms, compounds, variables, numbers)
|
||||
protected val baseTerm: Parser<Term> by (dummy
|
||||
or (-leftParenthesis * parser(::term) * -rightParenthesis)
|
||||
|
@ -98,8 +102,13 @@ open class TermsGrammar : Tokens() {
|
|||
t2.fold(t1) { acc, (op, term) -> CompoundTerm(Atom(op), listOf(acc, term)) }
|
||||
}
|
||||
|
||||
protected val dynamic: Parser<Term> by (dynamicOp * functor) use {
|
||||
CompoundTerm( Atom(t1.text), listOf(Atom(t2)) )
|
||||
}
|
||||
protected val term1150: Parser<Term> by (dynamic or term1100) use { this }
|
||||
|
||||
protected val op1200: Parser<String> by (neck) use { text }
|
||||
protected val term1200: Parser<Term> by (term1100 * zeroOrMore(op1200 * term1100)) use {
|
||||
protected val term1200: Parser<Term> by (term1150 * zeroOrMore(op1200 * term1100)) use {
|
||||
t2.fold(t1) { acc, (op, term) -> CompoundTerm(Atom(op), listOf(acc, term)) }
|
||||
}
|
||||
|
||||
|
|
|
@ -8,21 +8,29 @@ import com.github.h0tk3y.betterParse.lexer.regexToken
|
|||
import com.github.h0tk3y.betterParse.lexer.token
|
||||
|
||||
abstract class Tokens : Grammar<Any>() {
|
||||
// Special tokens
|
||||
protected val neck by literalToken(":-")
|
||||
protected val leftParenthesis: Token by literalToken("(")
|
||||
protected val rightParenthesis: Token by literalToken(")")
|
||||
protected val comma: Token by literalToken(",")
|
||||
protected val exclamation: Token by literalToken("!")
|
||||
// 1200
|
||||
protected val neck by literalToken(":-")
|
||||
// 1150
|
||||
protected val dynamicOp by literalToken("dynamic")
|
||||
// 1100
|
||||
protected val semicolon: Token by literalToken(";")
|
||||
// 1000
|
||||
protected val comma: Token by literalToken(",")
|
||||
// 700
|
||||
protected val equivalent: Token by literalToken("==")
|
||||
protected val equals: Token by literalToken("=")
|
||||
protected val notEquals: Token by literalToken("\\=")
|
||||
protected val isOp: Token by literalToken("is")
|
||||
// 500
|
||||
protected val plus: Token by literalToken("+")
|
||||
protected val minus: Token by literalToken("-")
|
||||
protected val notEquals: Token by literalToken("\\=")
|
||||
// 400
|
||||
protected val multiply: Token by literalToken("*")
|
||||
protected val divide: Token by literalToken("/")
|
||||
protected val exclamation: Token by literalToken("!")
|
||||
protected val isOp: Token by literalToken("is")
|
||||
// 100
|
||||
protected val dot by literalToken(".")
|
||||
|
||||
// Prolog tokens
|
||||
|
|
Reference in a new issue