Skip to content

Commit 29a195f

Browse files
committed
feat: add function call op #22
1 parent 06ed9aa commit 29a195f

2 files changed

Lines changed: 99 additions & 19 deletions

File tree

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package chapi.domain.expr
22

3-
import java.util.function.BinaryOperator
4-
import java.util.function.IntBinaryOperator
5-
63
// todo: mapping to pratt parser ?
74
// mini sample <https://github.com/segeljakt/pratt>
85
//
@@ -19,11 +16,34 @@ sealed class Expression {
1916
}
2017
}
2118

22-
class UnOp(lhs: ExpressionNode, op: UnOpKind) : ExpressionNode
19+
class UnaryOp(lhs: ExpressionNode, op: UnaryOpKind) : ExpressionNode
2320
class IntValue(val value: kotlin.Int) : ExpressionNode {
2421
override fun toString() = value.toString()
2522
}
2623

24+
class Variable(val name: kotlin.String) : ExpressionNode {
25+
override fun toString() = name
26+
}
27+
28+
class Identifier(val name: kotlin.String) : ExpressionNode {
29+
override fun toString() = name
30+
}
31+
32+
// lhs: identifier, rhs: expression
33+
class MethodCall(val functionName: String, val args: Array<ExpressionNode>, val className: String = "") : ExpressionNode {
34+
override fun toString(): String {
35+
return if (className == "") {
36+
"$functionName(${args.joinToString(", ")})"
37+
} else {
38+
"$className.$functionName(${args.joinToString(", ")})"
39+
}
40+
}
41+
}
42+
43+
class Arguments(val args: kotlin.Array<ExpressionNode>) : ExpressionNode {
44+
override fun toString() = args.joinToString(", ")
45+
}
46+
2747
class CustomValueType(val value: ValueType) : ExpressionNode
2848

2949
}
@@ -48,12 +68,28 @@ sealed class BinOpKind {
4868
object Pow : BinOpKind() {
4969
override fun toString() = "^"
5070
}
71+
72+
object Mod : BinOpKind() {
73+
override fun toString() = "%"
74+
}
5175
}
5276

53-
sealed class UnOpKind {
54-
object Not : UnOpKind() // !
55-
object Neg : UnOpKind() // -
56-
object Try : UnOpKind() // ?
77+
sealed class UnaryOpKind {
78+
object Not : UnaryOpKind() {
79+
override fun toString() = "!"
80+
}
81+
82+
object Neg : UnaryOpKind() {
83+
override fun toString() = "-"
84+
}
85+
86+
object Try : UnaryOpKind() {
87+
override fun toString() = "?"
88+
}
89+
90+
class Custom(val symbol: String = "") : UnaryOpKind() {
91+
override fun toString() = symbol
92+
}
5793
}
5894

5995
interface ExpressionType {
@@ -82,14 +118,3 @@ interface ValueType {
82118
}
83119

84120
interface Operator : ExpressionNode
85-
86-
enum class Arithmetics : BinaryOperator<Int>, IntBinaryOperator {
87-
PLUS {
88-
override fun apply(t: Int, u: Int): Int {
89-
return t + u
90-
}
91-
}
92-
;
93-
94-
override fun applyAsInt(t: Int, u: Int) = apply(t, u)
95-
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,59 @@ class ExpressionTest {
1515

1616
assertEquals("1 + 2", binOp.toString())
1717
}
18+
19+
@Test
20+
fun shouldAddWithSub() {
21+
val add = Expression.BinOp(lhs = Expression.IntValue(1), op = BinOpKind.Add, rhs = Expression.IntValue(2))
22+
val binOp = Expression.BinOp(
23+
lhs = add,
24+
op = BinOpKind.Sub,
25+
rhs = Expression.IntValue(2)
26+
)
27+
28+
assertEquals("1 + 2 - 2", binOp.toString())
29+
}
30+
31+
@Test
32+
fun shouldUnaryOp() {
33+
val binOp = Expression.UnaryOp(
34+
lhs = Expression.IntValue(1),
35+
op = UnaryOpKind.Neg
36+
)
37+
38+
assertEquals("-1", binOp.toString())
39+
}
40+
41+
@Test
42+
fun shouldPresentationIdentSub() {
43+
val binOp = Expression.BinOp(
44+
lhs = Expression.Variable("a"),
45+
op = BinOpKind.Add,
46+
rhs = Expression.Variable("b")
47+
)
48+
49+
assertEquals("a + b", binOp.toString())
50+
}
51+
52+
@Test
53+
fun functionCall() {
54+
assertEquals(Expression.MethodCall(
55+
functionName = "add",
56+
className = "math",
57+
args = arrayOf(
58+
Expression.IntValue(1),
59+
Expression.IntValue(2)
60+
)
61+
).toString(), "math.add(1, 2)"
62+
)
63+
64+
assertEquals(Expression.MethodCall(
65+
functionName = "add",
66+
args = arrayOf(
67+
Expression.IntValue(1),
68+
Expression.IntValue(2)
69+
)
70+
).toString(), "add(1, 2)"
71+
)
72+
}
1873
}

0 commit comments

Comments
 (0)