-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
28 deletions.
There are no files selected for viewing
28 changes: 0 additions & 28 deletions
28
chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://github.com/segeljakt/pratt> | ||
// | ||
// 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<ExpressionNode> get() = emptyArray() | ||
val expressionTypes: Array<ExpressionType> 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<Int>, IntBinaryOperator { | ||
PLUS { | ||
override fun apply(t: Int, u: Int): Int { | ||
return t + u | ||
} | ||
} | ||
; | ||
|
||
override fun applyAsInt(t: Int, u: Int) = apply(t, u) | ||
} |
10 changes: 10 additions & 0 deletions
10
chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package chapi.domain.expr | ||
|
||
import org.junit.jupiter.api.Test | ||
|
||
class ExpressionTest { | ||
@Test | ||
fun shouldAddChildren() { | ||
|
||
} | ||
} |