Skip to content

Commit

Permalink
feat: init basic pratter for #22
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent 6562aa6 commit 34755a3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 28 deletions.
28 changes: 0 additions & 28 deletions chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt

This file was deleted.

67 changes: 67 additions & 0 deletions chapi-domain/src/main/kotlin/chapi/domain/expr/TokenTree.kt
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 chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt
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() {

}
}

0 comments on commit 34755a3

Please sign in to comment.