Skip to content

Commit

Permalink
feat: add function call op #22
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Nov 9, 2022
1 parent 06ed9aa commit 29a195f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 19 deletions.
63 changes: 44 additions & 19 deletions chapi-domain/src/main/kotlin/chapi/domain/expr/Expression.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package chapi.domain.expr

import java.util.function.BinaryOperator
import java.util.function.IntBinaryOperator

// todo: mapping to pratt parser ?
// mini sample <https://github.com/segeljakt/pratt>
//
Expand All @@ -19,11 +16,34 @@ sealed class Expression {
}
}

class UnOp(lhs: ExpressionNode, op: UnOpKind) : ExpressionNode
class UnaryOp(lhs: ExpressionNode, op: UnaryOpKind) : ExpressionNode
class IntValue(val value: kotlin.Int) : ExpressionNode {
override fun toString() = value.toString()
}

class Variable(val name: kotlin.String) : ExpressionNode {
override fun toString() = name
}

class Identifier(val name: kotlin.String) : ExpressionNode {
override fun toString() = name
}

// lhs: identifier, rhs: expression
class MethodCall(val functionName: String, val args: Array<ExpressionNode>, val className: String = "") : ExpressionNode {
override fun toString(): String {
return if (className == "") {
"$functionName(${args.joinToString(", ")})"
} else {
"$className.$functionName(${args.joinToString(", ")})"
}
}
}

class Arguments(val args: kotlin.Array<ExpressionNode>) : ExpressionNode {
override fun toString() = args.joinToString(", ")
}

class CustomValueType(val value: ValueType) : ExpressionNode

}
Expand All @@ -48,12 +68,28 @@ sealed class BinOpKind {
object Pow : BinOpKind() {
override fun toString() = "^"
}

object Mod : BinOpKind() {
override fun toString() = "%"
}
}

sealed class UnOpKind {
object Not : UnOpKind() // !
object Neg : UnOpKind() // -
object Try : UnOpKind() // ?
sealed class UnaryOpKind {
object Not : UnaryOpKind() {
override fun toString() = "!"
}

object Neg : UnaryOpKind() {
override fun toString() = "-"
}

object Try : UnaryOpKind() {
override fun toString() = "?"
}

class Custom(val symbol: String = "") : UnaryOpKind() {
override fun toString() = symbol
}
}

interface ExpressionType {
Expand Down Expand Up @@ -82,14 +118,3 @@ interface ValueType {
}

interface Operator : ExpressionNode

enum class Arithmetics : BinaryOperator<Int>, IntBinaryOperator {
PLUS {
override fun apply(t: Int, u: Int): Int {
return t + u
}
}
;

override fun applyAsInt(t: Int, u: Int) = apply(t, u)
}
55 changes: 55 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 @@ -15,4 +15,59 @@ class ExpressionTest {

assertEquals("1 + 2", binOp.toString())
}

@Test
fun shouldAddWithSub() {
val add = Expression.BinOp(lhs = Expression.IntValue(1), op = BinOpKind.Add, rhs = Expression.IntValue(2))
val binOp = Expression.BinOp(
lhs = add,
op = BinOpKind.Sub,
rhs = Expression.IntValue(2)
)

assertEquals("1 + 2 - 2", binOp.toString())
}

@Test
fun shouldUnaryOp() {
val binOp = Expression.UnaryOp(
lhs = Expression.IntValue(1),
op = UnaryOpKind.Neg
)

assertEquals("-1", binOp.toString())
}

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

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

@Test
fun functionCall() {
assertEquals(Expression.MethodCall(
functionName = "add",
className = "math",
args = arrayOf(
Expression.IntValue(1),
Expression.IntValue(2)
)
).toString(), "math.add(1, 2)"
)

assertEquals(Expression.MethodCall(
functionName = "add",
args = arrayOf(
Expression.IntValue(1),
Expression.IntValue(2)
)
).toString(), "add(1, 2)"
)
}
}

0 comments on commit 29a195f

Please sign in to comment.