From 0d3cd2f430c3b5f63c009cd656fa573b502ed32c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Przemys=C5=82aw=20Pluta?= Date: Thu, 5 Mar 2020 20:44:40 +0100 Subject: [PATCH] Migrate factor parser to Kotlin --- src/main/kotlin/SMNP.kt | 2 +- .../dsl/ast/model/node/NotOperatorNode.kt | 3 ++ .../dsl/ast/model/node/PowerOperatorNode.kt | 3 ++ .../kotlin/dsl/ast/parser/FactorParser.kt | 31 +++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/dsl/ast/model/node/NotOperatorNode.kt create mode 100644 src/main/kotlin/dsl/ast/model/node/PowerOperatorNode.kt create mode 100644 src/main/kotlin/dsl/ast/parser/FactorParser.kt diff --git a/src/main/kotlin/SMNP.kt b/src/main/kotlin/SMNP.kt index c435514..a3b0ba8 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("true 123 -\"fsfsef\".13.15 @c:14 3.14") + interpreter.run("true ** 123 ** 4 -\"fsfsef\".13.15 @c:14 3.14") } \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/NotOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/NotOperatorNode.kt new file mode 100644 index 0000000..67be59c --- /dev/null +++ b/src/main/kotlin/dsl/ast/model/node/NotOperatorNode.kt @@ -0,0 +1,3 @@ +package dsl.ast.model.node + +class NotOperatorNode(operator: Node, operand: Node) : UnaryOperatorAbstractNode(operator, operand) \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/PowerOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/PowerOperatorNode.kt new file mode 100644 index 0000000..5391a4e --- /dev/null +++ b/src/main/kotlin/dsl/ast/model/node/PowerOperatorNode.kt @@ -0,0 +1,3 @@ +package dsl.ast.model.node + +class PowerOperatorNode(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/FactorParser.kt b/src/main/kotlin/dsl/ast/parser/FactorParser.kt new file mode 100644 index 0000000..b7198b7 --- /dev/null +++ b/src/main/kotlin/dsl/ast/parser/FactorParser.kt @@ -0,0 +1,31 @@ +package dsl.ast.parser + +import dsl.ast.model.entity.ParserOutput +import dsl.ast.model.node.NotOperatorNode +import dsl.ast.model.node.PowerOperatorNode +import dsl.token.model.entity.TokenList +import dsl.token.model.enumeration.TokenType + +class FactorParser : Parser() { + override fun tryToParse(input: TokenList): ParserOutput { + val factorParser = leftAssociativeOperator( + UnitParser(), + listOf(TokenType.DOUBLE_ASTERISK), + UnitParser() + ) { lhs, operator, rhs -> + PowerOperatorNode(lhs, operator, rhs) + } + + val notOperatorParser = allOf(listOf( + terminal(TokenType.NOT), + factorParser + )) { + NotOperatorNode(it[0], it[1]) + } + + return oneOf(listOf( + notOperatorParser, + factorParser + )).parse(input) + } +} \ No newline at end of file