forked from openrs2/openrs2
parent
fc00d69539
commit
130a05956e
@ -1,104 +0,0 @@ |
|||||||
package dev.openrs2.deob.ast.util; |
|
||||||
|
|
||||||
import com.github.javaparser.ast.expr.BinaryExpr; |
|
||||||
import com.github.javaparser.ast.expr.BooleanLiteralExpr; |
|
||||||
import com.github.javaparser.ast.expr.Expression; |
|
||||||
import com.github.javaparser.ast.expr.IntegerLiteralExpr; |
|
||||||
import com.github.javaparser.ast.expr.LongLiteralExpr; |
|
||||||
import com.github.javaparser.ast.expr.UnaryExpr; |
|
||||||
|
|
||||||
public final class ExprUtils { |
|
||||||
public static boolean isIntegerOrLongLiteral(Expression expr) { |
|
||||||
return expr.isIntegerLiteralExpr() || expr.isLongLiteralExpr(); |
|
||||||
} |
|
||||||
|
|
||||||
public static LongLiteralExpr createLong(long value) { |
|
||||||
return new LongLiteralExpr(Long.toString(value).concat("L")); |
|
||||||
} |
|
||||||
|
|
||||||
public static Expression negate(Expression expr) { |
|
||||||
if (expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.MINUS) { |
|
||||||
return expr.asUnaryExpr().getExpression().clone(); |
|
||||||
} else if (expr.isIntegerLiteralExpr()) { |
|
||||||
return new IntegerLiteralExpr(-expr.asIntegerLiteralExpr().asInt()); |
|
||||||
} else if (expr.isLongLiteralExpr()) { |
|
||||||
return createLong(-expr.asLongLiteralExpr().asLong()); |
|
||||||
} else { |
|
||||||
throw new IllegalArgumentException(); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
public static Expression not(Expression expr) { |
|
||||||
if (expr.isUnaryExpr()) { |
|
||||||
var unary = expr.asUnaryExpr(); |
|
||||||
if (unary.getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT) { |
|
||||||
return unary.getExpression().clone(); |
|
||||||
} |
|
||||||
} else if (expr.isBinaryExpr()) { |
|
||||||
var binary = expr.asBinaryExpr(); |
|
||||||
|
|
||||||
var left = binary.getLeft(); |
|
||||||
var right = binary.getRight(); |
|
||||||
|
|
||||||
switch (binary.getOperator()) { |
|
||||||
case EQUALS: |
|
||||||
return new BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.NOT_EQUALS); |
|
||||||
case NOT_EQUALS: |
|
||||||
return new BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.EQUALS); |
|
||||||
case GREATER: |
|
||||||
return new BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.LESS_EQUALS); |
|
||||||
case GREATER_EQUALS: |
|
||||||
return new BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.LESS); |
|
||||||
case LESS: |
|
||||||
return new BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.GREATER_EQUALS); |
|
||||||
case LESS_EQUALS: |
|
||||||
return new BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.GREATER); |
|
||||||
case AND: |
|
||||||
return new BinaryExpr(not(left), not(right), BinaryExpr.Operator.OR); |
|
||||||
case OR: |
|
||||||
return new BinaryExpr(not(left), not(right), BinaryExpr.Operator.AND); |
|
||||||
} |
|
||||||
} else if (expr.isBooleanLiteralExpr()) { |
|
||||||
return new BooleanLiteralExpr(!expr.asBooleanLiteralExpr().getValue()); |
|
||||||
} |
|
||||||
return new UnaryExpr(expr.clone(), UnaryExpr.Operator.LOGICAL_COMPLEMENT); |
|
||||||
} |
|
||||||
|
|
||||||
public static int countNots(Expression expr) { |
|
||||||
int count = 0; |
|
||||||
|
|
||||||
if (expr.isUnaryExpr() && expr.asUnaryExpr().getOperator() == UnaryExpr.Operator.LOGICAL_COMPLEMENT) { |
|
||||||
count++; |
|
||||||
} else if (expr.isBinaryExpr() && expr.asBinaryExpr().getOperator() == BinaryExpr.Operator.NOT_EQUALS) { |
|
||||||
count++; |
|
||||||
} |
|
||||||
|
|
||||||
for (Expression child : expr.findAll(Expression.class)) { |
|
||||||
if (child != expr) { |
|
||||||
count += countNots(child); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return count; |
|
||||||
} |
|
||||||
|
|
||||||
public static boolean hasSideEffects(Expression expr) { |
|
||||||
if (expr.isLiteralExpr() || expr.isNameExpr() | expr.isFieldAccessExpr()) { |
|
||||||
return false; |
|
||||||
} else if (expr.isUnaryExpr()) { |
|
||||||
return hasSideEffects(expr.asUnaryExpr().getExpression()); |
|
||||||
} else if (expr.isBinaryExpr()) { |
|
||||||
var binary = expr.asBinaryExpr(); |
|
||||||
return hasSideEffects(binary.getLeft()) || hasSideEffects(binary.getRight()); |
|
||||||
} else if (expr.isArrayAccessExpr()) { |
|
||||||
var access = expr.asArrayAccessExpr(); |
|
||||||
return hasSideEffects(access.getName()) || hasSideEffects(access.getIndex()); |
|
||||||
} |
|
||||||
// TODO(gpe): more cases
|
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
private ExprUtils() { |
|
||||||
/* empty */ |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,94 @@ |
|||||||
|
package dev.openrs2.deob.ast.util |
||||||
|
|
||||||
|
import com.github.javaparser.ast.expr.* |
||||||
|
|
||||||
|
fun Expression.isIntegerOrLongLiteral(): Boolean { |
||||||
|
return isIntegerLiteralExpr || isLongLiteralExpr |
||||||
|
} |
||||||
|
|
||||||
|
fun createLong(value: Long): LongLiteralExpr { |
||||||
|
return LongLiteralExpr(java.lang.Long.toString(value) + "L") |
||||||
|
} |
||||||
|
|
||||||
|
fun Expression.negate(): Expression { |
||||||
|
return if (isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.MINUS) { |
||||||
|
asUnaryExpr().expression.clone() |
||||||
|
} else if (isIntegerLiteralExpr) { |
||||||
|
IntegerLiteralExpr(-asIntegerLiteralExpr().asInt()) |
||||||
|
} else if (isLongLiteralExpr) { |
||||||
|
createLong(-asLongLiteralExpr().asLong()) |
||||||
|
} else { |
||||||
|
throw IllegalArgumentException() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
fun Expression.not(): Expression { |
||||||
|
if (isUnaryExpr) { |
||||||
|
val unary = asUnaryExpr() |
||||||
|
if (unary.operator == UnaryExpr.Operator.LOGICAL_COMPLEMENT) { |
||||||
|
return unary.expression.clone() |
||||||
|
} |
||||||
|
} else if (isBinaryExpr) { |
||||||
|
val binary = asBinaryExpr() |
||||||
|
|
||||||
|
val left = binary.left |
||||||
|
val right = binary.right |
||||||
|
|
||||||
|
@Suppress("NON_EXHAUSTIVE_WHEN") |
||||||
|
when (binary.operator) { |
||||||
|
BinaryExpr.Operator.EQUALS -> |
||||||
|
return BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.NOT_EQUALS) |
||||||
|
BinaryExpr.Operator.NOT_EQUALS -> |
||||||
|
return BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.EQUALS) |
||||||
|
BinaryExpr.Operator.GREATER -> |
||||||
|
return BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.LESS_EQUALS) |
||||||
|
BinaryExpr.Operator.GREATER_EQUALS -> |
||||||
|
return BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.LESS) |
||||||
|
BinaryExpr.Operator.LESS -> |
||||||
|
return BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.GREATER_EQUALS) |
||||||
|
BinaryExpr.Operator.LESS_EQUALS -> |
||||||
|
return BinaryExpr(left.clone(), right.clone(), BinaryExpr.Operator.GREATER) |
||||||
|
BinaryExpr.Operator.AND -> |
||||||
|
return BinaryExpr(left.not(), right.not(), BinaryExpr.Operator.OR) |
||||||
|
BinaryExpr.Operator.OR -> |
||||||
|
return BinaryExpr(left.not(), right.not(), BinaryExpr.Operator.AND) |
||||||
|
} |
||||||
|
} else if (isBooleanLiteralExpr) { |
||||||
|
return BooleanLiteralExpr(!asBooleanLiteralExpr().value) |
||||||
|
} |
||||||
|
return UnaryExpr(clone(), UnaryExpr.Operator.LOGICAL_COMPLEMENT) |
||||||
|
} |
||||||
|
|
||||||
|
fun Expression.countNots(): Int { |
||||||
|
var count = 0 |
||||||
|
|
||||||
|
if (isUnaryExpr && asUnaryExpr().operator == UnaryExpr.Operator.LOGICAL_COMPLEMENT) { |
||||||
|
count++ |
||||||
|
} else if (isBinaryExpr && asBinaryExpr().operator == BinaryExpr.Operator.NOT_EQUALS) { |
||||||
|
count++ |
||||||
|
} |
||||||
|
|
||||||
|
for (child in findAll(Expression::class.java)) { |
||||||
|
if (child !== this) { |
||||||
|
count += child.countNots() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return count |
||||||
|
} |
||||||
|
|
||||||
|
fun Expression.hasSideEffects(): Boolean { |
||||||
|
if (isLiteralExpr || isNameExpr || isFieldAccessExpr) { |
||||||
|
return false |
||||||
|
} else if (isUnaryExpr) { |
||||||
|
return asUnaryExpr().expression.hasSideEffects() |
||||||
|
} else if (isBinaryExpr) { |
||||||
|
val binary = asBinaryExpr() |
||||||
|
return binary.left.hasSideEffects() || binary.right.hasSideEffects() |
||||||
|
} else if (isArrayAccessExpr) { |
||||||
|
val access = asArrayAccessExpr() |
||||||
|
return access.name.hasSideEffects() || access.index.hasSideEffects() |
||||||
|
} |
||||||
|
// TODO(gpe): more cases |
||||||
|
return true |
||||||
|
} |
Loading…
Reference in new issue