From feb378a4435dba130e51e889cd84140d79730704 Mon Sep 17 00:00:00 2001 From: Sergey Loktev Date: Thu, 1 Aug 2024 16:28:38 +0300 Subject: [PATCH] Refactor BinOp --- .../kotlin/org/jacodb/ets/base/EtsExpr.kt | 2 +- .../main/kotlin/org/jacodb/ets/base/Ops.kt | 171 +++++++----------- .../main/kotlin/org/jacodb/ets/dto/Convert.kt | 34 +--- 3 files changed, 68 insertions(+), 139 deletions(-) diff --git a/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsExpr.kt b/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsExpr.kt index 83b33d2ed..cc334d4fc 100644 --- a/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsExpr.kt +++ b/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsExpr.kt @@ -220,7 +220,7 @@ interface EtsConditionExpr : EtsBinaryExpr { } data class EtsRelationOperation( - val relop: String, + val relop: RelationOp, override val left: EtsEntity, override val right: EtsEntity, ) : EtsConditionExpr { diff --git a/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt b/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt index ece9e5097..065217893 100644 --- a/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt +++ b/jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt @@ -65,131 +65,88 @@ enum class UpdateOp { Dec, } -enum class BinaryOp { - /** - * `==` - */ - EqEq, - - /** - * `!=` - */ - NotEq, - - /** - * `===` - */ - EqEqEq, +interface BinaryOp { + + companion object { + fun fromString(value: String): BinaryOp { + return RelationOp.fromString(value) + ?: ArithOp.fromString(value) + ?: LogicalOp.fromString(value) + ?: BitOp.fromString(value) + ?: NullishCoalescing.fromString(value) + ?: error("Unknown BinaryOp: $value") + } + } +} - /** - * `!==` - */ - NotEqEq, +enum class RelationOp(private val str: String) : BinaryOp { - /** - * `<` - */ - Lt, + EqEq("=="), + NotEq("!="), + EqEqEq("==="), + NotEqEq("!=="), + Lt("<"), + LtEq("<="), + Gt(">"), + GtEq(">="); - /** - * `<=` - */ - LtEq, + companion object { + fun fromString(value: String): RelationOp? = RelationOp.values().firstOrNull {it.str == value} + } - /** - * `>` - */ - Gt, + override fun toString(): String = str +} - /** - * `>=` - */ - GtEq, +enum class ArithOp(private val str: String) : BinaryOp { - /** - * `<<` - */ - LShift, + Add("+"), + Sub("-"), + Mul("*"), + Div("/"), + Mod("%"), + Exp("**"); - /** - * `>>` - */ - RShift, + companion object { + fun fromString(value: String): ArithOp? = ArithOp.values().firstOrNull {it.str == value} + } - /** - * `>>>` - */ - ZeroFillRShift, - - /** - * `+` - */ - Add, - - /** - * `-` - */ - Sub, + override fun toString(): String = str +} - /** - * `*` - */ - Mul, +enum class BitOp(private val str: String) : BinaryOp { - /** - * '/' - */ - Div, + LShift("<<"), + RShift(">>"), + ZeroFillRShift(">>>"), + BitOr("|"), + BitXor("^"), + BitAnd("&"); - /** - * `%` - */ - Mod, + companion object { + fun fromString(value: String): BitOp? = BitOp.values().firstOrNull {it.str == value} + } - /** - * `|` - */ - BitOr, - - /** - * `^` - */ - BitXor, - - /** - * `&` - */ - BitAnd, + override fun toString(): String = str +} - /** - * `||` - */ - LogicalOr, +enum class LogicalOp(private val str: String) : BinaryOp { - /** - * `&&` - */ - LogicalAnd, + LogicalOr("||"), + LogicalAnd("&&"), + In("in"), + InstanceOf("instanceof"); - /** - * `in` - */ - In, + companion object { + fun fromString(value: String): LogicalOp? = LogicalOp.values().firstOrNull {it.str == value} + } - /** - * `instanceof` - */ - InstanceOf, + override fun toString(): String = str +} - /** - * `**` - */ - Exp, +object NullishCoalescing : BinaryOp { + override fun toString(): String = "??" - /** - * `??` - */ - NullishCoalescing, + fun fromString(value: String): NullishCoalescing? = NullishCoalescing.takeIf { value == "??" } } enum class AssignOp { diff --git a/jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt b/jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt index 70d19f68b..b24ea903c 100644 --- a/jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt +++ b/jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt @@ -75,6 +75,7 @@ import org.jacodb.ets.base.EtsUnionType import org.jacodb.ets.base.EtsUnknownType import org.jacodb.ets.base.EtsValue import org.jacodb.ets.base.EtsVoidType +import org.jacodb.ets.base.RelationOp import org.jacodb.ets.base.UnaryOp import org.jacodb.ets.graph.EtsCfg import org.jacodb.ets.model.EtsClass @@ -270,7 +271,7 @@ class EtsMethodBuilder( ) is RelationOperationDto -> EtsRelationOperation( - relop = value.op, + relop = RelationOp.fromString(value.op) ?: error("Unknown RelationOp: ${value.op}"), left = convertToEtsEntity(value.left), right = convertToEtsEntity(value.right), ) @@ -598,36 +599,7 @@ fun convertToEtsUnaryOp(op: String): UnaryOp { } } -fun convertToEtsBinaryOp(op: String): BinaryOp { - return when (op) { - "+" -> BinaryOp.Add - "-" -> BinaryOp.Sub - "*" -> BinaryOp.Mul - "/" -> BinaryOp.Div - "%" -> BinaryOp.Mod - "==" -> BinaryOp.EqEq - "!=" -> BinaryOp.NotEq - "===" -> BinaryOp.EqEqEq - "!==" -> BinaryOp.NotEqEq - "<" -> BinaryOp.Lt - "<=" -> BinaryOp.LtEq - ">" -> BinaryOp.Gt - ">=" -> BinaryOp.GtEq - "<<" -> BinaryOp.LShift - ">>" -> BinaryOp.RShift - ">>>" -> BinaryOp.ZeroFillRShift - "&" -> BinaryOp.BitAnd - "|" -> BinaryOp.BitOr - "^" -> BinaryOp.BitXor - "&&" -> BinaryOp.LogicalAnd - "||" -> BinaryOp.LogicalOr - "in" -> BinaryOp.In - "instanceof" -> BinaryOp.InstanceOf - "**" -> BinaryOp.Exp - "??" -> BinaryOp.NullishCoalescing - else -> error("Unknown BinaryOp: $op") - } -} +fun convertToEtsBinaryOp(op: String): BinaryOp = BinaryOp.fromString(op) fun convertToEtsClassSignature(clazz: ClassSignatureDto): EtsClassSignature { return EtsClassSignature(