Skip to content

Commit ddc0416

Browse files
committed
feat: add comprison #22
1 parent 33ee8b9 commit ddc0416

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ sealed class Expression {
1111
override fun toString(): String = "$op$lhs"
1212
}
1313

14+
class ComparisonOp(val lhs: ExpressionNode, val op: ComparisonOpKind, val rhs: ExpressionNode) : ExpressionNode {
15+
override fun toString(): String = "$lhs $op $rhs"
16+
}
17+
1418
class IntValue(val value: Int) : ExpressionNode {
1519
override fun toString() = value.toString()
1620
}
@@ -31,7 +35,8 @@ sealed class Expression {
3135
override fun toString() = "try { $tryBlock } catch { $catchBlock }"
3236
}
3337

34-
class IfElse(val condition: ExpressionNode, val thenBlock: ExpressionNode, val elseBlock: ExpressionNode) : ExpressionNode {
38+
class IfElse(val condition: ExpressionNode, val thenBlock: ExpressionNode, val elseBlock: ExpressionNode) :
39+
ExpressionNode {
3540
override fun toString() = "if ($condition) { $thenBlock } else { $elseBlock }"
3641
}
3742

@@ -40,7 +45,8 @@ sealed class Expression {
4045
}
4146

4247
// lhs: identifier, rhs: expression
43-
class MethodCall(val functionName: String, val args: Array<ExpressionNode>, val className: String = "") : ExpressionNode {
48+
class MethodCall(val functionName: String, val args: Array<ExpressionNode>, val className: String = "") :
49+
ExpressionNode {
4450
override fun toString(): String {
4551
return if (className == "") {
4652
"$functionName(${args.joinToString(", ")})"
@@ -58,8 +64,47 @@ sealed class Expression {
5864
override fun toString() = "[${args.joinToString(", ")}]"
5965
}
6066

61-
class CustomValueType(val value: ValueType) : ExpressionNode
67+
class Value(val value: Any) : ExpressionNode {
68+
override fun toString() = value.toString()
69+
}
70+
}
71+
72+
73+
sealed class ComparisonOpKind {
74+
object Equal : ComparisonOpKind() {
75+
override fun toString() = "=="
76+
}
77+
object NotEqual : ComparisonOpKind() {
78+
override fun toString() = "!="
79+
}
80+
81+
object GreaterThan : ComparisonOpKind() {
82+
override fun toString() = ">"
83+
}
84+
85+
object GreaterThanOrEqual : ComparisonOpKind() {
86+
override fun toString() = ">="
87+
}
6288

89+
object LessThan : ComparisonOpKind() {
90+
override fun toString() = "<"
91+
}
92+
93+
object LessThanOrEqual : ComparisonOpKind() {
94+
override fun toString() = "<="
95+
}
96+
97+
object In : ComparisonOpKind() {
98+
override fun toString() = "in"
99+
}
100+
101+
object NotIn : ComparisonOpKind() {
102+
override fun toString() = "!in"
103+
}
104+
105+
object Is : ComparisonOpKind() {
106+
override fun toString() = "is"
107+
}
63108
}
64109

65110
sealed class BinOpKind {
@@ -125,10 +170,19 @@ interface ExpressionNode {
125170
}
126171
}
127172

128-
129173
interface ValueType {
130174
val name: String
131175
val value: Any
132176
}
133177

178+
sealed class Typed {
179+
class IntType(val value: Int) : Typed()
180+
class FloatType(val value: Float) : Typed()
181+
class StringType(val value: String) : Typed()
182+
class BoolType(val value: Boolean) : Typed()
183+
class ArrayType(val value: Array<Typed>) : Typed()
184+
class ObjectType(val value: Any) : Typed()
185+
class NullType(val value: Any) : Typed()
186+
}
187+
134188
interface Operator : ExpressionNode

chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class ExpressionTest {
8585
@Test
8686
fun tryCatch() {
8787
assertEquals(Expression.TryCatch(
88-
tryBlock = Expression.IntValue(1),
88+
tryBlock = Expression.Value(1),
8989
catchBlock = Expression.IntValue(2)
9090
).toString(), "try { 1 } catch { 2 }"
9191
)
@@ -100,4 +100,14 @@ class ExpressionTest {
100100
).toString(), "if (1) { 2 } else { 3 }"
101101
)
102102
}
103+
104+
@Test
105+
fun compareItem() {
106+
assertEquals(Expression.ComparisonOp(
107+
lhs = Expression.IntValue(1),
108+
op = ComparisonOpKind.GreaterThan,
109+
rhs = Expression.IntValue(2)
110+
).toString(), "1 > 2"
111+
)
112+
}
103113
}

0 commit comments

Comments
 (0)