diff --git a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt index a65303d0..7d82e899 100644 --- a/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt +++ b/chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt @@ -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() } @@ -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() { diff --git a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt index 0be53910..40f79978 100644 --- a/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt +++ b/chapi-domain/src/test/kotlin/chapi/domain/expr/ExpressionTest.kt @@ -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" + ) + } }