diff --git a/src/main/kotlin/SMNP.kt b/src/main/kotlin/SMNP.kt index 62eaaad..277615d 100644 --- a/src/main/kotlin/SMNP.kt +++ b/src/main/kotlin/SMNP.kt @@ -2,5 +2,5 @@ import interpreter.Interpreter fun main(args: Array) { val interpreter = Interpreter() - interpreter.run("2 ** 2 * true ** 123 ** 4 -\"fsfsef\".13.15 @c:14 3.14") + interpreter.run("2 + 2 * 2 / 2 ** 2") } \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/LogicOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/LogicOperatorNode.kt new file mode 100644 index 0000000..be4458f --- /dev/null +++ b/src/main/kotlin/dsl/ast/model/node/LogicOperatorNode.kt @@ -0,0 +1,3 @@ +package dsl.ast.model.node + +class LogicOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs) \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/RelationOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/RelationOperatorNode.kt new file mode 100644 index 0000000..1879e86 --- /dev/null +++ b/src/main/kotlin/dsl/ast/model/node/RelationOperatorNode.kt @@ -0,0 +1,3 @@ +package dsl.ast.model.node + +class RelationOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs) \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/SumOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/SumOperatorNode.kt new file mode 100644 index 0000000..507f2da --- /dev/null +++ b/src/main/kotlin/dsl/ast/model/node/SumOperatorNode.kt @@ -0,0 +1,3 @@ +package dsl.ast.model.node + +class SumOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs) \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/parser/ExpressionParser.kt b/src/main/kotlin/dsl/ast/parser/ExpressionParser.kt new file mode 100644 index 0000000..bd4ff34 --- /dev/null +++ b/src/main/kotlin/dsl/ast/parser/ExpressionParser.kt @@ -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) + } +} \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/parser/Parser.kt b/src/main/kotlin/dsl/ast/parser/Parser.kt index 623ca0b..588fed7 100644 --- a/src/main/kotlin/dsl/ast/parser/Parser.kt +++ b/src/main/kotlin/dsl/ast/parser/Parser.kt @@ -91,7 +91,7 @@ abstract class Parser { } // leftAssociative -> left | left OP right - fun leftAssociativeOperator(lhsParser: Parser, allowedOperators: List, rhsParser: Parser, createNode: (left: Node, operator: Node, right: Node) -> Node): Parser { + fun leftAssociativeOperator(lhsParser: Parser, allowedOperators: List, rhsParser: Parser, createNode: (lhs: Node, operator: Node, rhs: Node) -> Node): Parser { return object : Parser() { override fun tryToParse(input: TokenList): ParserOutput { val opParser = terminals(allowedOperators)