Migrate unit parser to Kotlin
This commit is contained in:
@@ -2,5 +2,5 @@ import interpreter.Interpreter
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val interpreter = Interpreter()
|
||||
interpreter.run("true 123 \"fsfsef\" @c:14 3.14")
|
||||
interpreter.run("true 123 -\"fsfsef\".13.15 @c:14 3.14")
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class AccessOperatorNode(position: TokenPosition) : BinaryOperatorAbstractNode(position)
|
||||
class AccessOperatorNode(lhs: Node, operator: Node, rhs: Node) : BinaryOperatorAbstractNode(lhs, operator, rhs)
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
package dsl.ast.model.node
|
||||
|
||||
import dsl.token.model.entity.TokenPosition
|
||||
|
||||
class MinusOperatorNode(position: TokenPosition) : UnaryOperatorAbstractNode(position)
|
||||
class MinusOperatorNode(operator: Node, operand: Node) : UnaryOperatorAbstractNode(operator, operand)
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ abstract class Parser {
|
||||
}
|
||||
|
||||
// allOf -> a b c ...
|
||||
fun allOf(parsers: List<Parser>, createNode: (List<Node>, TokenPosition) -> Node): Parser {
|
||||
fun allOf(parsers: List<Parser>, createNode: (List<Node>) -> 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))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
29
src/main/kotlin/dsl/ast/parser/UnitParser.kt
Normal file
29
src/main/kotlin/dsl/ast/parser/UnitParser.kt
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user