Skip to content

Commit 86e948e

Browse files
committed
feat: add jumpop #22
1 parent c31435a commit 86e948e

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ sealed class Expression {
6969
override fun toString() = "[${args.joinToString(", ")}]"
7070
}
7171

72-
class SwitchCases(val lhs: ExpressionNode, val cases: Array<CaseOp>, val defaultBlock: ExpressionNode?) : ExpressionNode {
72+
class SwitchCases(val lhs: ExpressionNode, val cases: Array<CaseOp>, val defaultBlock: ExpressionNode?) :
73+
ExpressionNode {
7374
override fun toString(): String {
7475
val casesStr = cases.joinToString("\n") { "case ${it.condition}: ${it.thenBlock}" }
7576
val defaultStr = if (defaultBlock != null) {
@@ -84,14 +85,37 @@ sealed class Expression {
8485
class RangeOp(val lhs: ExpressionNode, val rhs: ExpressionNode) : ExpressionNode {
8586
override fun toString() = "$lhs..$rhs"
8687
}
87-
88+
8889
class CaseOp(val condition: ExpressionNode, val thenBlock: ExpressionNode) : ExpressionNode {
8990
override fun toString() = "$condition -> $thenBlock"
9091
}
91-
92+
9293
class Value(val value: Any) : ExpressionNode {
9394
override fun toString() = value.toString()
9495
}
96+
97+
class JumpOp(val op: JumpOpKind) : ExpressionNode {
98+
override fun toString() = op.toString()
99+
}
100+
}
101+
102+
// Throw, Return, Break, Continue
103+
sealed class JumpOpKind {
104+
class Throw(val expr: ExpressionNode) : JumpOpKind() {
105+
override fun toString() = "throw $expr"
106+
}
107+
108+
class Return(val expr: ExpressionNode) : JumpOpKind() {
109+
override fun toString() = "return $expr"
110+
}
111+
112+
class Break(val expr: ExpressionNode) : JumpOpKind() {
113+
override fun toString() = "break $expr"
114+
}
115+
116+
class Continue(val expr: ExpressionNode) : JumpOpKind() {
117+
override fun toString() = "continue $expr"
118+
}
95119
}
96120

97121
sealed class AssignOpKind {
@@ -146,6 +170,7 @@ sealed class ComparisonOpKind {
146170
object Equal : ComparisonOpKind() {
147171
override fun toString() = "=="
148172
}
173+
149174
object NotEqual : ComparisonOpKind() {
150175
override fun toString() = "!="
151176
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,13 @@ class ExpressionTest {
195195
).toString(), "add(1, 2).sub(3)"
196196
)
197197
}
198+
199+
@Test
200+
fun returnExprOp() {
201+
assertEquals(
202+
Expression.JumpOp(
203+
op = JumpOpKind.Return(Expression.IntValue(1))
204+
).toString(), "return 1"
205+
)
206+
}
198207
}

0 commit comments

Comments
 (0)