Skip to content

Commit df144dc

Browse files
committed
feat: add assign op #22
1 parent ddc0416 commit df144dc

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ sealed class Expression {
1515
override fun toString(): String = "$lhs $op $rhs"
1616
}
1717

18+
class AssignOp(val lhs: ExpressionNode, val op: AssignOpKind, val rhs: ExpressionNode) : ExpressionNode {
19+
override fun toString(): String = "$lhs $op $rhs"
20+
}
21+
1822
class IntValue(val value: Int) : ExpressionNode {
1923
override fun toString() = value.toString()
2024
}
@@ -69,6 +73,53 @@ sealed class Expression {
6973
}
7074
}
7175

76+
sealed class AssignOpKind {
77+
object Assign : AssignOpKind() {
78+
override fun toString() = "="
79+
}
80+
81+
object PlusAssign : AssignOpKind() {
82+
override fun toString() = "+="
83+
}
84+
85+
object MinusAssign : AssignOpKind() {
86+
override fun toString() = "-="
87+
}
88+
89+
object MultiplyAssign : AssignOpKind() {
90+
override fun toString() = "*="
91+
}
92+
93+
object DivideAssign : AssignOpKind() {
94+
override fun toString() = "/="
95+
}
96+
97+
object ModuloAssign : AssignOpKind() {
98+
override fun toString() = "%="
99+
}
100+
101+
object BitwiseAndAssign : AssignOpKind() {
102+
override fun toString() = "&="
103+
}
104+
105+
object BitwiseOrAssign : AssignOpKind() {
106+
override fun toString() = "|="
107+
}
108+
109+
object BitwiseXorAssign : AssignOpKind() {
110+
override fun toString() = "^="
111+
}
112+
113+
object LeftShiftAssign : AssignOpKind() {
114+
override fun toString() = "<<="
115+
}
116+
117+
object RightShiftAssign : AssignOpKind() {
118+
override fun toString() = ">>="
119+
}
120+
121+
}
122+
72123

73124
sealed class ComparisonOpKind {
74125
object Equal : ComparisonOpKind() {

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,29 @@ class ExpressionTest {
110110
).toString(), "1 > 2"
111111
)
112112
}
113+
114+
@Test
115+
fun shouldPresentationIdentSubWithParen() {
116+
val binOp = Expression.BinOp(
117+
lhs = Expression.Variable("a"),
118+
op = BinOpKind.Add,
119+
rhs = Expression.BinOp(
120+
lhs = Expression.Variable("b"),
121+
op = BinOpKind.Sub,
122+
rhs = Expression.Variable("c")
123+
)
124+
)
125+
126+
assertEquals("a + (b - c)", binOp.toString())
127+
}
128+
129+
@Test
130+
fun assignment() {
131+
assertEquals(Expression.AssignOp(
132+
lhs = Expression.Variable("a"),
133+
op = AssignOpKind.Assign,
134+
rhs = Expression.IntValue(1)
135+
).toString(), "a = 1"
136+
)
137+
}
113138
}

0 commit comments

Comments
 (0)