Skip to content

Commit

Permalink
feat: add assign op #22
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent ddc0416 commit df144dc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
51 changes: 51 additions & 0 deletions chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ sealed class Expression {
override fun toString(): String = "$lhs $op $rhs"
}

class AssignOp(val lhs: ExpressionNode, val op: AssignOpKind, val rhs: ExpressionNode) : ExpressionNode {
override fun toString(): String = "$lhs $op $rhs"
}

class IntValue(val value: Int) : ExpressionNode {
override fun toString() = value.toString()
}
Expand Down Expand Up @@ -69,6 +73,53 @@ sealed class Expression {
}
}

sealed class AssignOpKind {
object Assign : AssignOpKind() {
override fun toString() = "="
}

object PlusAssign : AssignOpKind() {
override fun toString() = "+="
}

object MinusAssign : AssignOpKind() {
override fun toString() = "-="
}

object MultiplyAssign : AssignOpKind() {
override fun toString() = "*="
}

object DivideAssign : AssignOpKind() {
override fun toString() = "/="
}

object ModuloAssign : AssignOpKind() {
override fun toString() = "%="
}

object BitwiseAndAssign : AssignOpKind() {
override fun toString() = "&="
}

object BitwiseOrAssign : AssignOpKind() {
override fun toString() = "|="
}

object BitwiseXorAssign : AssignOpKind() {
override fun toString() = "^="
}

object LeftShiftAssign : AssignOpKind() {
override fun toString() = "<<="
}

object RightShiftAssign : AssignOpKind() {
override fun toString() = ">>="
}

}


sealed class ComparisonOpKind {
object Equal : ComparisonOpKind() {
Expand Down
25 changes: 25 additions & 0 deletions chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,29 @@ class ExpressionTest {
).toString(), "1 > 2"
)
}

@Test
fun shouldPresentationIdentSubWithParen() {
val binOp = Expression.BinOp(
lhs = Expression.Variable("a"),
op = BinOpKind.Add,
rhs = Expression.BinOp(
lhs = Expression.Variable("b"),
op = BinOpKind.Sub,
rhs = Expression.Variable("c")
)
)

assertEquals("a + (b - c)", binOp.toString())
}

@Test
fun assignment() {
assertEquals(Expression.AssignOp(
lhs = Expression.Variable("a"),
op = AssignOpKind.Assign,
rhs = Expression.IntValue(1)
).toString(), "a = 1"
)
}
}

0 comments on commit df144dc

Please sign in to comment.