From 34755a31a4299279a5b03b407cd0d8ba802c3331 Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 9 Nov 2022 12:14:15 +0800 Subject: [PATCH] feat: init basic pratter for #22 --- .../kotlin/chapi/domain/expr/Expression.kt | 28 -------- .../kotlin/chapi/domain/expr/TokenTree.kt | 67 +++++++++++++++++++ .../chapi/domain/expr/ExpressionTest.kt | 10 +++ 3 files changed, 77 insertions(+), 28 deletions(-) delete mode 100644 chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt create mode 100644 chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt create mode 100644 chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt diff --git a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt deleted file mode 100644 index c6535764..00000000 --- a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt +++ /dev/null @@ -1,28 +0,0 @@ -package chapi.domain.expr - -import java.util.function.BinaryOperator -import java.util.function.IntBinaryOperator - -class Expression( - var Type: ExpressionType, - var Value: String = "", - var Children: Array = arrayOf() -) - -enum class ExpressionType { - Primary, - Infix, - Prefix, - Postfix, -} - -enum class Arithmetics : BinaryOperator, IntBinaryOperator { - PLUS { - override fun apply(t: Int, u: Int): Int { - return t + u - } - } - ; - - override fun applyAsInt(t: Int, u: Int) = apply(t, u) -} diff --git a/chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt b/chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt new file mode 100644 index 00000000..d171f10a --- /dev/null +++ b/chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt @@ -0,0 +1,67 @@ +package chapi.domain.expr + +import java.util.function.BinaryOperator +import java.util.function.IntBinaryOperator + +// todo: mapping to pratt parser ? +// mini sample +// +// enum class TokenTree { +// Primary, +// Prefix, +// Infix, +// Postfix; +// } +sealed class Expression { + class BinOp(lhs: Expression, op: BinOpKind, rhs: Expression) : ExpressionNode + class UnOp(lhs: Expression, op: UnOpKind) : ExpressionNode + class Int(val value: Int?) : ExpressionNode +} + +sealed class BinOpKind { + object Add : BinOpKind() // + + object Sub : BinOpKind() // - + object Mul : BinOpKind() // * + object Div : BinOpKind() // / + object Pow : BinOpKind() // ^ + object Eq : BinOpKind() // = +} + +sealed class UnOpKind { + object Not : UnOpKind() // ! + object Neg : UnOpKind() // - + object Try : UnOpKind() // ? +} + +interface ExpressionType { + val name: String +} + +interface ExpressionNode { + val childrens: Array get() = emptyArray() + val expressionTypes: Array get() = emptyArray() + + fun getPriority(): Int = 0 + + fun addChildren(child: Operator) { + childrens.plus(child) + } + + fun addExpressionType(type: ExpressionType) { + expressionTypes.plus(type) + } +} + + +interface Operator : ExpressionNode + +enum class Arithmetics : BinaryOperator, IntBinaryOperator { + PLUS { + override fun apply(t: Int, u: Int): Int { + return t + u + } + } + ; + + override fun applyAsInt(t: Int, u: Int) = apply(t, u) +} diff --git a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt new file mode 100644 index 00000000..4b7080d7 --- /dev/null +++ b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt @@ -0,0 +1,10 @@ +package chapi.domain.expr + +import org.junit.jupiter.api.Test + +class ExpressionTest { + @Test + fun shouldAddChildren() { + + } +}