diff --git a/src/main/kotlin/SMNP.kt b/src/main/kotlin/SMNP.kt index b43e3ec..c435514 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\" @c:14 3.14") + interpreter.run("true 123 -\"fsfsef\".13.15 @c:14 3.14") } \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/AccessOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/AccessOperatorNode.kt index b6a51bf..0175781 100644 --- a/src/main/kotlin/dsl/ast/model/node/AccessOperatorNode.kt +++ b/src/main/kotlin/dsl/ast/model/node/AccessOperatorNode.kt @@ -1,5 +1,3 @@ package dsl.ast.model.node -import dsl.token.model.entity.TokenPosition - -class AccessOperatorNode(position: TokenPosition) : BinaryOperatorAbstractNode(position) \ No newline at end of file +class AccessOperatorNode(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/BinaryOperatorAbstractNode.kt b/src/main/kotlin/dsl/ast/model/node/BinaryOperatorAbstractNode.kt index 5bc8ade..d99cf4c 100644 --- a/src/main/kotlin/dsl/ast/model/node/BinaryOperatorAbstractNode.kt +++ b/src/main/kotlin/dsl/ast/model/node/BinaryOperatorAbstractNode.kt @@ -1,23 +1,18 @@ package dsl.ast.model.node -import dsl.token.model.entity.TokenPosition - -abstract class BinaryOperatorAbstractNode(position: TokenPosition) : Node(3, position) { - var lhs: Any +abstract class BinaryOperatorAbstractNode(lhs: Node, operator: Node, rhs: Node) : Node(3, operator.position) { + val lhs: Any get() = children[0] - set(value) { - children[0] = value - } - var operator: Any + val operator: Any get() = children[1] - set(value) { - children[1] = value - } - var rhs: Any + val rhs: Any get() = children[2] - set(value) { - children[2] = value + + init { + children[0] = lhs + children[1] = operator + children[2] = rhs } } \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/MinusOperatorNode.kt b/src/main/kotlin/dsl/ast/model/node/MinusOperatorNode.kt index f89a37e..573010d 100644 --- a/src/main/kotlin/dsl/ast/model/node/MinusOperatorNode.kt +++ b/src/main/kotlin/dsl/ast/model/node/MinusOperatorNode.kt @@ -1,5 +1,3 @@ package dsl.ast.model.node -import dsl.token.model.entity.TokenPosition - -class MinusOperatorNode(position: TokenPosition) : UnaryOperatorAbstractNode(position) \ No newline at end of file +class MinusOperatorNode(operator: Node, operand: Node) : UnaryOperatorAbstractNode(operator, operand) \ No newline at end of file diff --git a/src/main/kotlin/dsl/ast/model/node/UnaryOperatorAbstractNode.kt b/src/main/kotlin/dsl/ast/model/node/UnaryOperatorAbstractNode.kt index 90bcd48..327afe0 100644 --- a/src/main/kotlin/dsl/ast/model/node/UnaryOperatorAbstractNode.kt +++ b/src/main/kotlin/dsl/ast/model/node/UnaryOperatorAbstractNode.kt @@ -1,17 +1,14 @@ package dsl.ast.model.node -import dsl.token.model.entity.TokenPosition - -abstract class UnaryOperatorAbstractNode(position: TokenPosition) : Node(2, position) { - var operator: Any +abstract class UnaryOperatorAbstractNode(operator: Node, operand: Node) : Node(2, operator.position) { + val operator: Any get() = children[0] - set(value) { - children[0] = value - } - var value: Any + val operand: Any get() = children[1] - set(_value) { - children[1] = _value + + init { + children[0] = operator + children[1] = operand } } \ 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 6501090..623ca0b 100644 --- a/src/main/kotlin/dsl/ast/parser/Parser.kt +++ b/src/main/kotlin/dsl/ast/parser/Parser.kt @@ -65,7 +65,7 @@ abstract class Parser { } // allOf -> a b c ... - fun allOf(parsers: List, createNode: (List, TokenPosition) -> Node): Parser { + fun allOf(parsers: List, createNode: (List) -> Node): Parser { return object : Parser() { override fun tryToParse(input: TokenList): ParserOutput { if(parsers.isEmpty()) { @@ -84,7 +84,7 @@ abstract class Parser { nodes += output.node } - return ParserOutput.ok(createNode(nodes, nodes.first().position)) + return ParserOutput.ok(createNode(nodes)) } } diff --git a/src/main/kotlin/dsl/ast/parser/UnitParser.kt b/src/main/kotlin/dsl/ast/parser/UnitParser.kt new file mode 100644 index 0000000..554eaea --- /dev/null +++ b/src/main/kotlin/dsl/ast/parser/UnitParser.kt @@ -0,0 +1,29 @@ +package dsl.ast.parser + +import dsl.ast.model.entity.ParserOutput +import dsl.ast.model.node.AccessOperatorNode +import dsl.ast.model.node.MinusOperatorNode +import dsl.token.model.entity.TokenList +import dsl.token.model.enumeration.TokenType + +class UnitParser : Parser() { + override fun tryToParse(input: TokenList): ParserOutput { + val minusOperatorParser = allOf( + listOf( + terminal(TokenType.MINUS), + assert(AtomParser(), "atom") + ) + ) { + MinusOperatorNode(it[0], it[1]) + } + + val atom2 = oneOf(listOf( + minusOperatorParser, + AtomParser() + )) + + return leftAssociativeOperator(atom2, listOf(TokenType.DOT), assert(atom2, "atom")) { + lhs, operator, rhs -> AccessOperatorNode(lhs, operator, rhs) + }.parse(input) + } +} \ No newline at end of file