11package 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
5995interface ExpressionType {
@@ -82,14 +118,3 @@ interface ValueType {
82118}
83119
84120interface 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- }
0 commit comments