Skip to content

Commit

Permalink
feat: add jumpop #22
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent c31435a commit 86e948e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
31 changes: 28 additions & 3 deletions chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ sealed class Expression {
override fun toString() = "[${args.joinToString(", ")}]"
}

class SwitchCases(val lhs: ExpressionNode, val cases: Array<CaseOp>, val defaultBlock: ExpressionNode?) : ExpressionNode {
class SwitchCases(val lhs: ExpressionNode, val cases: Array<CaseOp>, val defaultBlock: ExpressionNode?) :
ExpressionNode {
override fun toString(): String {
val casesStr = cases.joinToString("\n") { "case ${it.condition}: ${it.thenBlock}" }
val defaultStr = if (defaultBlock != null) {
Expand All @@ -84,14 +85,37 @@ sealed class Expression {
class RangeOp(val lhs: ExpressionNode, val rhs: ExpressionNode) : ExpressionNode {
override fun toString() = "$lhs..$rhs"
}

class CaseOp(val condition: ExpressionNode, val thenBlock: ExpressionNode) : ExpressionNode {
override fun toString() = "$condition -> $thenBlock"
}

class Value(val value: Any) : ExpressionNode {
override fun toString() = value.toString()
}

class JumpOp(val op: JumpOpKind) : ExpressionNode {
override fun toString() = op.toString()
}
}

// Throw, Return, Break, Continue
sealed class JumpOpKind {
class Throw(val expr: ExpressionNode) : JumpOpKind() {
override fun toString() = "throw $expr"
}

class Return(val expr: ExpressionNode) : JumpOpKind() {
override fun toString() = "return $expr"
}

class Break(val expr: ExpressionNode) : JumpOpKind() {
override fun toString() = "break $expr"
}

class Continue(val expr: ExpressionNode) : JumpOpKind() {
override fun toString() = "continue $expr"
}
}

sealed class AssignOpKind {
Expand Down Expand Up @@ -146,6 +170,7 @@ sealed class ComparisonOpKind {
object Equal : ComparisonOpKind() {
override fun toString() = "=="
}

object NotEqual : ComparisonOpKind() {
override fun toString() = "!="
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,13 @@ class ExpressionTest {
).toString(), "add(1, 2).sub(3)"
)
}

@Test
fun returnExprOp() {
assertEquals(
Expression.JumpOp(
op = JumpOpKind.Return(Expression.IntValue(1))
).toString(), "return 1"
)
}
}

0 comments on commit 86e948e

Please sign in to comment.