Skip to content

Commit

Permalink
Merge pull request #18075 from igfoo/igfoo/comp
Browse files Browse the repository at this point in the history
KE2: Small refactoring
  • Loading branch information
igfoo authored Nov 22, 2024
2 parents 37e950d + d280a41 commit bb50bc0
Showing 1 changed file with 54 additions and 59 deletions.
113 changes: 54 additions & 59 deletions java/kotlin-extractor2/src/main/kotlin/entities/Expression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -385,69 +385,64 @@ private fun KotlinFileExtractor.extractBinaryExpression(
return extractBinaryExpression(expression, callable, parent, trapWriterWriteExpr)
}

when {
op in listOf(KtTokens.LT, KtTokens.GT, KtTokens.LTEQ, KtTokens.GTEQ) -> {
// Extract lowered equivalent call, such as `a.compareTo(b) < 0` instead of `a < b` in the below:
// ```
// fun test(a: Data, b: Data) {
// a < b
// }
//
// class Data(val v: Int) : Comparable<Data> {
// override fun compareTo(other: Data) = v.compareTo(other.v)
// }
// ```

val exprParent = parent.expr(expression, callable)

val trapWriterWriteExpr = when (op) {
KtTokens.LT -> tw::writeExprs_ltexpr
KtTokens.GT -> tw::writeExprs_gtexpr
KtTokens.LTEQ -> tw::writeExprs_leexpr
KtTokens.GTEQ -> tw::writeExprs_geexpr
else -> TODO("error")
}

val id = extractRawBinaryExpression(builtinTypes.boolean, exprParent, trapWriterWriteExpr)
extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt)

extractRawMethodAccess(
target,
tw.getLocation(expression),
target.returnType,
callable,
id,
0,
exprParent.enclosingStmt,
if (target.isExtension) null else expression.left!!,
if (target.isExtension) expression.left!! else null,
listOf(expression.right!!)
)

extractConstantInteger(
"0",
builtinTypes.int,
0,
tw.getLocation(expression),
id,
1,
callable,
exprParent.enclosingStmt,
/*
OLD: KE1
overrideId = overrideId
*/
)
val trapWriterWriteExprComparison = when (op) {
KtTokens.LT -> tw::writeExprs_ltexpr
KtTokens.GT -> tw::writeExprs_gtexpr
KtTokens.LTEQ -> tw::writeExprs_leexpr
KtTokens.GTEQ -> tw::writeExprs_geexpr
else -> null
}

return id
}
if (trapWriterWriteExprComparison != null) {
// Extract lowered equivalent call, such as `a.compareTo(b) < 0` instead of `a < b` in the below:
// ```
// fun test(a: Data, b: Data) {
// a < b
// }
//
// class Data(val v: Int) : Comparable<Data> {
// override fun compareTo(other: Data) = v.compareTo(other.v)
// }
// ```

val exprParent = parent.expr(expression, callable)

val id = extractRawBinaryExpression(builtinTypes.boolean, exprParent, trapWriterWriteExprComparison)
extractExprContext(id, tw.getLocation(expression), callable, exprParent.enclosingStmt)

extractRawMethodAccess(
target,
tw.getLocation(expression),
target.returnType,
callable,
id,
0,
exprParent.enclosingStmt,
if (target.isExtension) null else expression.left!!,
if (target.isExtension) expression.left!! else null,
listOf(expression.right!!)
)

else -> {
// todo: other operators, such as .., ..<, in, !in, =, +=, -=, *=, /=, %=, ==, !=,
TODO("Extract as method call")
}
extractConstantInteger(
"0",
builtinTypes.int,
0,
tw.getLocation(expression),
id,
1,
callable,
exprParent.enclosingStmt,
/*
OLD: KE1
overrideId = overrideId
*/
)

return id
}

// todo: other operators, such as .., ..<, in, !in, =, +=, -=, *=, /=, %=, ==, !=,
TODO("Extract as method call")
}


Expand Down

0 comments on commit bb50bc0

Please sign in to comment.