From ddc0416e8309f5a5580ab163f8e1cb844e4f95bf Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 9 Nov 2022 19:47:16 +0800 Subject: [PATCH] feat: add comprison #22 --- .../kotlin/chapi/domain/expr/Expression.kt | 62 +++++++++++++++++-- .../chapi/domain/expr/ExpressionTest.kt | 12 +++- 2 files changed, 69 insertions(+), 5 deletions(-) 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 468a92b4..a65303d0 100644 --- a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt +++ b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt @@ -11,6 +11,10 @@ sealed class Expression { override fun toString(): String = "$op$lhs" } + class ComparisonOp(val lhs: ExpressionNode, val op: ComparisonOpKind, val rhs: ExpressionNode) : ExpressionNode { + override fun toString(): String = "$lhs $op $rhs" + } + class IntValue(val value: Int) : ExpressionNode { override fun toString() = value.toString() } @@ -31,7 +35,8 @@ sealed class Expression { override fun toString() = "try { $tryBlock } catch { $catchBlock }" } - class IfElse(val condition: ExpressionNode, val thenBlock: ExpressionNode, val elseBlock: ExpressionNode) : ExpressionNode { + class IfElse(val condition: ExpressionNode, val thenBlock: ExpressionNode, val elseBlock: ExpressionNode) : + ExpressionNode { override fun toString() = "if ($condition) { $thenBlock } else { $elseBlock }" } @@ -40,7 +45,8 @@ sealed class Expression { } // lhs: identifier, rhs: expression - class MethodCall(val functionName: String, val args: Array, val className: String = "") : ExpressionNode { + class MethodCall(val functionName: String, val args: Array, val className: String = "") : + ExpressionNode { override fun toString(): String { return if (className == "") { "$functionName(${args.joinToString(", ")})" @@ -58,8 +64,47 @@ sealed class Expression { override fun toString() = "[${args.joinToString(", ")}]" } - class CustomValueType(val value: ValueType) : ExpressionNode + class Value(val value: Any) : ExpressionNode { + override fun toString() = value.toString() + } +} + + +sealed class ComparisonOpKind { + object Equal : ComparisonOpKind() { + override fun toString() = "==" + } + object NotEqual : ComparisonOpKind() { + override fun toString() = "!=" + } + + object GreaterThan : ComparisonOpKind() { + override fun toString() = ">" + } + + object GreaterThanOrEqual : ComparisonOpKind() { + override fun toString() = ">=" + } + object LessThan : ComparisonOpKind() { + override fun toString() = "<" + } + + object LessThanOrEqual : ComparisonOpKind() { + override fun toString() = "<=" + } + + object In : ComparisonOpKind() { + override fun toString() = "in" + } + + object NotIn : ComparisonOpKind() { + override fun toString() = "!in" + } + + object Is : ComparisonOpKind() { + override fun toString() = "is" + } } sealed class BinOpKind { @@ -125,10 +170,19 @@ interface ExpressionNode { } } - interface ValueType { val name: String val value: Any } +sealed class Typed { + class IntType(val value: Int) : Typed() + class FloatType(val value: Float) : Typed() + class StringType(val value: String) : Typed() + class BoolType(val value: Boolean) : Typed() + class ArrayType(val value: Array) : Typed() + class ObjectType(val value: Any) : Typed() + class NullType(val value: Any) : Typed() +} + interface Operator : ExpressionNode 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 79478226..0be53910 100644 --- a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt +++ b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt @@ -85,7 +85,7 @@ class ExpressionTest { @Test fun tryCatch() { assertEquals(Expression.TryCatch( - tryBlock = Expression.IntValue(1), + tryBlock = Expression.Value(1), catchBlock = Expression.IntValue(2) ).toString(), "try { 1 } catch { 2 }" ) @@ -100,4 +100,14 @@ class ExpressionTest { ).toString(), "if (1) { 2 } else { 3 }" ) } + + @Test + fun compareItem() { + assertEquals(Expression.ComparisonOp( + lhs = Expression.IntValue(1), + op = ComparisonOpKind.GreaterThan, + rhs = Expression.IntValue(2) + ).toString(), "1 > 2" + ) + } }