Skip to content

Commit

Permalink
[Kotlin] Add support for downTo, fix non-KExprent instances in other …
Browse files Browse the repository at this point in the history
…"do" statements
  • Loading branch information
sschr15 committed Aug 16, 2024
1 parent 8deb31f commit 9b3711e
Show file tree
Hide file tree
Showing 4 changed files with 443 additions and 401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.java.decompiler.util.TextBuffer;
import org.vineflower.kotlin.expr.KConstExprent;
import org.vineflower.kotlin.expr.KVarExprent;
import org.vineflower.kotlin.util.KUtils;

public class KDoStatement extends KStatement<DoStatement> {
public KDoStatement(DoStatement statement) {
Expand Down Expand Up @@ -41,31 +42,49 @@ public TextBuffer toJava(int indent) {
buf.appendIndent(indent).append("}").appendLineSeparator();
}
case DO_WHILE -> {
Exprent expr = KUtils.replaceExprent(statement.getConditionExprent());
if (expr == null) {
expr = statement.getConditionExprent();
}

buf.append("do {").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.appendIndent(indent).append("} while (");
buf.pushNewlineGroup(indent, 1);
buf.appendPossibleNewline();
buf.append(statement.getConditionExprent().toJava(indent));
buf.append(expr.toJava(indent));
buf.appendPossibleNewline("", true);
buf.popNewlineGroup();
buf.append(")").appendLineSeparator();
}
case WHILE -> {
Exprent expr = KUtils.replaceExprent(statement.getConditionExprent());
if (expr == null) {
expr = statement.getConditionExprent();
}

buf.append("while (");
buf.pushNewlineGroup(indent, 1);
buf.appendPossibleNewline();
buf.append(statement.getConditionExprent().toJava(indent));
buf.append(expr.toJava(indent));
buf.appendPossibleNewline("", true);
buf.popNewlineGroup();
buf.append(") {").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.appendIndent(indent).append("}").appendLineSeparator();
}
case FOR_EACH -> {
buf.append("for (").append(statement.getInitExprent().toJava(indent));
statement.getIncExprent().getInferredExprType(null); //TODO: see DoStatement
buf.append(" in ").append(statement.getIncExprent().toJava(indent)).append(") {").appendLineSeparator();
KVarExprent init = new KVarExprent((VarExprent) statement.getInitExprent());
init.setExcludeVarVal(true);

Exprent inc = KUtils.replaceExprent(statement.getIncExprent());
if (inc == null) {
inc = statement.getIncExprent();
}

buf.append("for (").append(init.toJava(indent));
inc.getInferredExprType(null); //TODO: see DoStatement
buf.append(" in ").append(inc.toJava(indent)).append(") {").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.appendIndent(indent).append("}").appendLineSeparator();
}
Expand All @@ -78,36 +97,71 @@ public TextBuffer toJava(int indent) {
init.getRight() instanceof ConstExprent constExpr &&

statement.getIncExprent() instanceof FunctionExprent inc &&
inc.getFuncType() == FunctionExprent.FunctionType.IPP &&
inc.getFuncType().isPPMM() &&

statement.getConditionExprent() instanceof FunctionExprent condition &&
condition.getFuncType() == FunctionExprent.FunctionType.LT &&
condition.getLstOperands().get(condition.getLstOperands().size() - 1) instanceof ConstExprent conditionConst
condition.getFuncType() == FunctionExprent.FunctionType.LT
) {
// Turn for loop into range
varExpr = new KVarExprent(varExpr);
((KVarExprent) varExpr).setExcludeVarVal(true);

constExpr.setConstType(varExpr.getExprType());
KConstExprent conditionConst;
if (condition.getLstOperands().get(1) instanceof ConstExprent c) {
conditionConst = new KConstExprent(c);
} else {
conditionConst = new KConstExprent((ConstExprent) condition.getLstOperands().get(0));
}

conditionConst.setConstType(varExpr.getExprType());
if (conditionConst.getValue() instanceof Integer i) {
conditionConst = new ConstExprent(varExpr.getExprType(), i - 1, conditionConst.bytecode);
conditionConst = new KConstExprent(conditionConst);
int newValue = inc.getFuncType().isPP() ? i - 1 : i + 1;
conditionConst = new KConstExprent(new ConstExprent(varExpr.getExprType(), newValue, conditionConst.bytecode));
}

buf.append("for (")
constExpr.setConstType(varExpr.getExprType());

if (constExpr.getValue() instanceof Integer i && i == 0) {
buf.append("repeat(")
.append(conditionConst.toJava())
.append(") {");

if (!"it".equals(varExpr.getName())) {
buf.append(" ")
.append(varExpr.toJava(indent))
.append(" ->");
}
buf.appendLineSeparator();
} else {
buf.append("for (")
.append(varExpr.toJava(indent))
.append(" in ")
.append(constExpr.toJava())
.append("..")
.append(inc.getFuncType().isPP() ? ".." : " downTo ")
.append(conditionConst.toJava())
.append(") {")
.appendLineSeparator();
}

buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.appendIndent(indent).append("}").appendLineSeparator();
} else {
//TODO other cases
Exprent init = KUtils.replaceExprent(statement.getInitExprent());
if (init == null) {
init = statement.getInitExprent();
}

Exprent condition = KUtils.replaceExprent(statement.getConditionExprent());
if (condition == null) {
condition = statement.getConditionExprent();
}

Exprent inc = KUtils.replaceExprent(statement.getIncExprent());
if (inc == null) {
inc = statement.getIncExprent();
}

if (labeled) {
buf.setLength(0); // Clear buffer, label needs to be treated differently
buf.appendIndent(indent);
Expand All @@ -125,18 +179,18 @@ public TextBuffer toJava(int indent) {
.appendIndent(++indent);
}

if (statement.getInitExprent() != null) {
buf.append(statement.getInitExprent().toJava(indent)).appendLineSeparator().appendIndent(indent);
if (init != null) {
buf.append(init.toJava(indent)).appendLineSeparator().appendIndent(indent);
}

buf.append("while (true) {").appendLineSeparator();
buf.appendIndent(indent + 1);
buf.append("if (");
buf.append(statement.getConditionExprent().toJava(indent + 1));
buf.append(condition.toJava(indent + 1));
buf.append(") break").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.appendLineSeparator();
buf.appendIndent(indent + 1).append(statement.getIncExprent().toJava(indent + 1)).appendLineSeparator();
buf.appendIndent(indent + 1).append(inc.toJava(indent + 1)).appendLineSeparator();
buf.appendIndent(indent).append("}").appendLineSeparator();

if (labeled) {
Expand Down
Loading

0 comments on commit 9b3711e

Please sign in to comment.