Skip to content

Commit

Permalink
feat: add comprison #22
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent 33ee8b9 commit ddc0416
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
62 changes: 58 additions & 4 deletions chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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 }"
}

Expand All @@ -40,7 +45,8 @@ sealed class Expression {
}

// lhs: identifier, rhs: expression
class MethodCall(val functionName: String, val args: Array<ExpressionNode>, val className: String = "") : ExpressionNode {
class MethodCall(val functionName: String, val args: Array<ExpressionNode>, val className: String = "") :
ExpressionNode {
override fun toString(): String {
return if (className == "") {
"$functionName(${args.joinToString(", ")})"
Expand All @@ -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 {
Expand Down Expand Up @@ -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>) : Typed()
class ObjectType(val value: Any) : Typed()
class NullType(val value: Any) : Typed()
}

interface Operator : ExpressionNode
12 changes: 11 additions & 1 deletion chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"
)
Expand All @@ -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"
)
}
}

0 comments on commit ddc0416

Please sign in to comment.