Skip to content

Commit

Permalink
Refactor BinOp
Browse files Browse the repository at this point in the history
  • Loading branch information
zishkaz committed Aug 1, 2024
1 parent 133474f commit feb378a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 139 deletions.
2 changes: 1 addition & 1 deletion jacodb-ets/src/main/kotlin/org/jacodb/ets/base/EtsExpr.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
171 changes: 64 additions & 107 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Check warning on line 77 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L74-L77

Added lines #L74 - L77 were not covered by tests
}
}
}

/**
* `!==`
*/
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

Check warning on line 97 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L97

Added line #L97 was not covered by tests
}

/**
* `>=`
*/
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

Check warning on line 113 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L113

Added line #L113 was not covered by tests
}

/**
* `*`
*/
Mul,
enum class BitOp(private val str: String) : BinaryOp {

Check warning on line 116 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L116

Added line #L116 was not covered by tests

/**
* '/'
*/
Div,
LShift("<<"),
RShift(">>"),
ZeroFillRShift(">>>"),
BitOr("|"),
BitXor("^"),
BitAnd("&");

Check warning on line 123 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L118-L123

Added lines #L118 - L123 were not covered by tests

/**
* `%`
*/
Mod,
companion object {
fun fromString(value: String): BitOp? = BitOp.values().firstOrNull {it.str == value}

Check warning on line 126 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L126

Added line #L126 was not covered by tests
}

/**
* `|`
*/
BitOr,

/**
* `^`
*/
BitXor,

/**
* `&`
*/
BitAnd,
override fun toString(): String = str

Check warning on line 129 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L129

Added line #L129 was not covered by tests
}

/**
* `||`
*/
LogicalOr,
enum class LogicalOp(private val str: String) : BinaryOp {

Check warning on line 132 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L132

Added line #L132 was not covered by tests

/**
* `&&`
*/
LogicalAnd,
LogicalOr("||"),
LogicalAnd("&&"),
In("in"),
InstanceOf("instanceof");

Check warning on line 137 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L134-L137

Added lines #L134 - L137 were not covered by tests

/**
* `in`
*/
In,
companion object {
fun fromString(value: String): LogicalOp? = LogicalOp.values().firstOrNull {it.str == value}

Check warning on line 140 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L140

Added line #L140 was not covered by tests
}

/**
* `instanceof`
*/
InstanceOf,
override fun toString(): String = str

Check warning on line 143 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L143

Added line #L143 was not covered by tests
}

/**
* `**`
*/
Exp,
object NullishCoalescing : BinaryOp {
override fun toString(): String = "??"

Check warning on line 147 in jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt

View check run for this annotation

Codecov / codecov/patch

jacodb-ets/src/main/kotlin/org/jacodb/ets/base/Ops.kt#L147

Added line #L147 was not covered by tests

/**
* `??`
*/
NullishCoalescing,
fun fromString(value: String): NullishCoalescing? = NullishCoalescing.takeIf { value == "??" }
}

enum class AssignOp {
Expand Down
34 changes: 3 additions & 31 deletions jacodb-ets/src/main/kotlin/org/jacodb/ets/dto/Convert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit feb378a

Please sign in to comment.