Skip to content

Commit 33ee8b9

Browse files
committed
feat: try if else packages
1 parent 8382a41 commit 33ee8b9

2 files changed

Lines changed: 50 additions & 7 deletions

File tree

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@ package chapi.domain.expr
22

33
// todo: mapping to pratt parser ?
44
// mini sample <https://github.com/segeljakt/pratt>
5-
//
6-
// enum class TokenTree {
7-
// Primary,
8-
// Prefix,
9-
// Infix,
10-
// Postfix;
11-
// }
125
sealed class Expression {
136
class BinOp(val lhs: ExpressionNode, val op: BinOpKind, val rhs: ExpressionNode) : ExpressionNode {
147
override fun toString() = "$lhs $op $rhs"
@@ -22,10 +15,26 @@ sealed class Expression {
2215
override fun toString() = value.toString()
2316
}
2417

18+
class FloatValue(val value: Float) : ExpressionNode {
19+
override fun toString() = value.toString()
20+
}
21+
22+
class StringValue(val value: String) : ExpressionNode {
23+
override fun toString() = value
24+
}
25+
2526
class Variable(val name: String) : ExpressionNode {
2627
override fun toString() = name
2728
}
2829

30+
class TryCatch(val tryBlock: ExpressionNode, val catchBlock: ExpressionNode) : ExpressionNode {
31+
override fun toString() = "try { $tryBlock } catch { $catchBlock }"
32+
}
33+
34+
class IfElse(val condition: ExpressionNode, val thenBlock: ExpressionNode, val elseBlock: ExpressionNode) : ExpressionNode {
35+
override fun toString() = "if ($condition) { $thenBlock } else { $elseBlock }"
36+
}
37+
2938
class Identifier(val name: String) : ExpressionNode {
3039
override fun toString() = name
3140
}
@@ -45,6 +54,10 @@ sealed class Expression {
4554
override fun toString() = args.joinToString(", ")
4655
}
4756

57+
class ArrayLiteral(val args: kotlin.Array<ExpressionNode>) : ExpressionNode {
58+
override fun toString() = "[${args.joinToString(", ")}]"
59+
}
60+
4861
class CustomValueType(val value: ValueType) : ExpressionNode
4962

5063
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,34 @@ class ExpressionTest {
7070
).toString(), "add(1, 2)"
7171
)
7272
}
73+
74+
@Test
75+
fun arrayLiteral() {
76+
assertEquals(Expression.ArrayLiteral(
77+
args = arrayOf(
78+
Expression.IntValue(1),
79+
Expression.IntValue(2)
80+
)
81+
).toString(), "[1, 2]"
82+
)
83+
}
84+
85+
@Test
86+
fun tryCatch() {
87+
assertEquals(Expression.TryCatch(
88+
tryBlock = Expression.IntValue(1),
89+
catchBlock = Expression.IntValue(2)
90+
).toString(), "try { 1 } catch { 2 }"
91+
)
92+
}
93+
94+
@Test
95+
fun ifElse() {
96+
assertEquals(Expression.IfElse(
97+
condition = Expression.IntValue(1),
98+
thenBlock = Expression.IntValue(2),
99+
elseBlock = Expression.IntValue(3)
100+
).toString(), "if (1) { 2 } else { 3 }"
101+
)
102+
}
73103
}

0 commit comments

Comments
 (0)