diff --git a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt index 6d46d4ac..cbcc2006 100644 --- a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt +++ b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt @@ -1,8 +1,5 @@ package chapi.domain.expr -import java.util.function.BinaryOperator -import java.util.function.IntBinaryOperator - // todo: mapping to pratt parser ? // mini sample // @@ -19,11 +16,34 @@ sealed class Expression { } } - class UnOp(lhs: ExpressionNode, op: UnOpKind) : ExpressionNode + class UnaryOp(lhs: ExpressionNode, op: UnaryOpKind) : ExpressionNode class IntValue(val value: kotlin.Int) : ExpressionNode { override fun toString() = value.toString() } + class Variable(val name: kotlin.String) : ExpressionNode { + override fun toString() = name + } + + class Identifier(val name: kotlin.String) : ExpressionNode { + override fun toString() = name + } + + // lhs: identifier, rhs: expression + class MethodCall(val functionName: String, val args: Array, val className: String = "") : ExpressionNode { + override fun toString(): String { + return if (className == "") { + "$functionName(${args.joinToString(", ")})" + } else { + "$className.$functionName(${args.joinToString(", ")})" + } + } + } + + class Arguments(val args: kotlin.Array) : ExpressionNode { + override fun toString() = args.joinToString(", ") + } + class CustomValueType(val value: ValueType) : ExpressionNode } @@ -48,12 +68,28 @@ sealed class BinOpKind { object Pow : BinOpKind() { override fun toString() = "^" } + + object Mod : BinOpKind() { + override fun toString() = "%" + } } -sealed class UnOpKind { - object Not : UnOpKind() // ! - object Neg : UnOpKind() // - - object Try : UnOpKind() // ? +sealed class UnaryOpKind { + object Not : UnaryOpKind() { + override fun toString() = "!" + } + + object Neg : UnaryOpKind() { + override fun toString() = "-" + } + + object Try : UnaryOpKind() { + override fun toString() = "?" + } + + class Custom(val symbol: String = "") : UnaryOpKind() { + override fun toString() = symbol + } } interface ExpressionType { @@ -82,14 +118,3 @@ interface ValueType { } 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 index 5d5448da..2e6d6d96 100644 --- a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt +++ b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt @@ -15,4 +15,59 @@ class ExpressionTest { assertEquals("1 + 2", binOp.toString()) } + + @Test + fun shouldAddWithSub() { + val add = Expression.BinOp(lhs = Expression.IntValue(1), op = BinOpKind.Add, rhs = Expression.IntValue(2)) + val binOp = Expression.BinOp( + lhs = add, + op = BinOpKind.Sub, + rhs = Expression.IntValue(2) + ) + + assertEquals("1 + 2 - 2", binOp.toString()) + } + + @Test + fun shouldUnaryOp() { + val binOp = Expression.UnaryOp( + lhs = Expression.IntValue(1), + op = UnaryOpKind.Neg + ) + + assertEquals("-1", binOp.toString()) + } + + @Test + fun shouldPresentationIdentSub() { + val binOp = Expression.BinOp( + lhs = Expression.Variable("a"), + op = BinOpKind.Add, + rhs = Expression.Variable("b") + ) + + assertEquals("a + b", binOp.toString()) + } + + @Test + fun functionCall() { + assertEquals(Expression.MethodCall( + functionName = "add", + className = "math", + args = arrayOf( + Expression.IntValue(1), + Expression.IntValue(2) + ) + ).toString(), "math.add(1, 2)" + ) + + assertEquals(Expression.MethodCall( + functionName = "add", + args = arrayOf( + Expression.IntValue(1), + Expression.IntValue(2) + ) + ).toString(), "add(1, 2)" + ) + } }