Migrate base expression parsers to Kotlin (without loop parser)
This commit is contained in:
@@ -2,5 +2,5 @@ import interpreter.Interpreter
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val interpreter = Interpreter()
|
||||
interpreter.run("2 ** 2 * true ** 123 ** 4 -\"fsfsef\".13.15 @c:14 3.14")
|
||||
interpreter.run("2 + 2 * 2 / 2 ** 2")
|
||||
}
|
||||
3
src/main/kotlin/dsl/ast/model/node/LogicOperatorNode.kt
Normal file
3
src/main/kotlin/dsl/ast/model/node/LogicOperatorNode.kt
Normal file
@@ -0,0 +1,3 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class LogicOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs)
|
||||
@@ -0,0 +1,3 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class RelationOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs)
|
||||
3
src/main/kotlin/dsl/ast/model/node/SumOperatorNode.kt
Normal file
3
src/main/kotlin/dsl/ast/model/node/SumOperatorNode.kt
Normal file
@@ -0,0 +1,3 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
class SumOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs)
|
||||
46
src/main/kotlin/dsl/ast/parser/ExpressionParser.kt
Normal file
46
src/main/kotlin/dsl/ast/parser/ExpressionParser.kt
Normal file
@@ -0,0 +1,46 @@
|
||||
package dsl.ast.parser
|
||||
|
||||
import dsl.ast.model.entity.ParserOutput
|
||||
import dsl.ast.model.node.LogicOperatorNode
|
||||
import dsl.ast.model.node.RelationOperatorNode
|
||||
import dsl.ast.model.node.SumOperatorNode
|
||||
import dsl.token.model.entity.TokenList
|
||||
import dsl.token.model.enumeration.TokenType
|
||||
|
||||
class ExpressionParser : Parser() {
|
||||
override fun tryToParse(input: TokenList): ParserOutput {
|
||||
val expr1Parser = leftAssociativeOperator(
|
||||
TermParser(),
|
||||
listOf(TokenType.PLUS, TokenType.MINUS),
|
||||
TermParser()
|
||||
) {
|
||||
lhs, operator, rhs -> SumOperatorNode(lhs, operator, rhs)
|
||||
}
|
||||
|
||||
val expr2Parser = leftAssociativeOperator(
|
||||
expr1Parser,
|
||||
listOf(TokenType.RELATION, TokenType.OPEN_ANGLE, TokenType.CLOSE_ANGLE),
|
||||
expr1Parser
|
||||
) {
|
||||
lhs, operator, rhs -> RelationOperatorNode(lhs, operator, rhs)
|
||||
}
|
||||
|
||||
val expr3Parser = leftAssociativeOperator(
|
||||
expr2Parser,
|
||||
listOf(TokenType.AND),
|
||||
expr2Parser
|
||||
) {
|
||||
lhs, operator, rhs -> LogicOperatorNode(lhs, operator, rhs)
|
||||
}
|
||||
|
||||
val expr4Parser = leftAssociativeOperator(
|
||||
expr3Parser,
|
||||
listOf(TokenType.OR),
|
||||
expr3Parser
|
||||
) {
|
||||
lhs, operator, rhs -> LogicOperatorNode(lhs, operator, rhs)
|
||||
}
|
||||
|
||||
return expr4Parser.parse(input)
|
||||
}
|
||||
}
|
||||
@@ -91,7 +91,7 @@ abstract class Parser {
|
||||
}
|
||||
|
||||
// leftAssociative -> left | left OP right
|
||||
fun leftAssociativeOperator(lhsParser: Parser, allowedOperators: List<TokenType>, rhsParser: Parser, createNode: (left: Node, operator: Node, right: Node) -> Node): Parser {
|
||||
fun leftAssociativeOperator(lhsParser: Parser, allowedOperators: List<TokenType>, rhsParser: Parser, createNode: (lhs: Node, operator: Node, rhs: Node) -> Node): Parser {
|
||||
return object : Parser() {
|
||||
override fun tryToParse(input: TokenList): ParserOutput {
|
||||
val opParser = terminals(allowedOperators)
|
||||
|
||||
Reference in New Issue
Block a user