Skip to content

Commit

Permalink
Subclass statement types instead of a custom one in its entirety
Browse files Browse the repository at this point in the history
  • Loading branch information
sschr15 committed Aug 29, 2024
1 parent ec7b475 commit 0981559
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 406 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,31 @@
import org.vineflower.kotlin.expr.KVarExprent;
import org.vineflower.kotlin.util.KUtils;

public class KDoStatement extends KStatement<DoStatement> {
public class KDoStatement extends DoStatement {
public KDoStatement(DoStatement statement) {
super(statement);
super();
setLooptype(statement.getLooptype());
setInitExprent(statement.getInitExprent());
setConditionExprent(statement.getConditionExprent());
setIncExprent(statement.getIncExprent());

setFirst(statement.getFirst());
stats.addAllWithKey(statement.getStats(), statement.getStats().getLstKeys());
parent = statement.getParent();
first = statement.getFirst();
exprents = statement.getExprents();
labelEdges.addAll(statement.getLabelEdges());
varDefinitions.addAll(statement.getVarDefinitions());
post = statement.getPost();
lastBasicType = statement.getLastBasicType();

isMonitorEnter = statement.isMonitorEnter();
containsMonitorExit = statement.containsMonitorExit();
isLastAthrow = statement.containsMonitorExitOrAthrow() && !containsMonitorExit;

continueSet = statement.getContinueSet();
}

private static boolean isIntegerType(VarType type) {
return VarType.VARTYPE_INT.equals(type) ||
VarType.VARTYPE_BYTE.equals(type) ||
Expand All @@ -25,30 +45,34 @@ private static boolean isIntegerType(VarType type) {
@Override
public TextBuffer toJava(int indent) {
TextBuffer buf = new TextBuffer();
boolean labeled = statement.isLabeled();
boolean labeled = isLabeled();

if (getLooptype() != Type.FOR) {
buf.append(ExprProcessor.listToJava(varDefinitions, indent));

buf.appendIndent(indent);
buf.appendIndent(indent);

if (labeled) {
buf.append("label")
.append(this.id)
.append("@ ");
if (labeled) {
buf.append("label")
.append(this.id)
.append("@ ");
}
}

switch (statement.getLooptype()) {
switch (getLooptype()) {
case INFINITE -> {
buf.append("while (true) {").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, false));
buf.appendIndent(indent).append("}").appendLineSeparator();
}
case DO_WHILE -> {
Exprent expr = KUtils.replaceExprent(statement.getConditionExprent());
Exprent expr = KUtils.replaceExprent(getConditionExprent());
if (expr == null) {
expr = statement.getConditionExprent();
expr = getConditionExprent();
}

buf.append("do {").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, false));
buf.appendIndent(indent).append("} while (");
buf.pushNewlineGroup(indent, 1);
buf.appendPossibleNewline();
Expand All @@ -58,9 +82,9 @@ public TextBuffer toJava(int indent) {
buf.append(")").appendLineSeparator();
}
case WHILE -> {
Exprent expr = KUtils.replaceExprent(statement.getConditionExprent());
Exprent expr = KUtils.replaceExprent(getConditionExprent());
if (expr == null) {
expr = statement.getConditionExprent();
expr = getConditionExprent();
}

buf.append("while (");
Expand All @@ -70,36 +94,38 @@ public TextBuffer toJava(int indent) {
buf.appendPossibleNewline("", true);
buf.popNewlineGroup();
buf.append(") {").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, false));
buf.appendIndent(indent).append("}").appendLineSeparator();
}
case FOR_EACH -> {
KVarExprent init = new KVarExprent((VarExprent) statement.getInitExprent());
KVarExprent init = new KVarExprent((VarExprent) getInitExprent());
init.setExcludeVarVal(true);

Exprent inc = KUtils.replaceExprent(statement.getIncExprent());
Exprent inc = KUtils.replaceExprent(getIncExprent());
if (inc == null) {
inc = statement.getIncExprent();
inc = 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.append(ExprProcessor.jmpWrapper(first, indent + 1, false));
buf.appendIndent(indent).append("}").appendLineSeparator();
}
case FOR -> {
buf.appendIndent(indent);

// This is a hard one as Kotlin has no one-to-one equivalent to Java's for loop
resugar: if (
statement.getInitExprent() instanceof AssignmentExprent init &&
getInitExprent() instanceof AssignmentExprent init &&
init.getLeft() instanceof VarExprent varExpr &&
isIntegerType(varExpr.getExprType()) &&
init.getRight() instanceof ConstExprent constExpr &&

statement.getIncExprent() instanceof FunctionExprent inc &&
getIncExprent() instanceof FunctionExprent inc &&
inc.getFuncType().isPPMM() &&

statement.getConditionExprent() instanceof FunctionExprent condition &&
getConditionExprent() instanceof FunctionExprent condition &&
condition.getFuncType() == FunctionExprent.FunctionType.LT
) {
// Turn for loop into range
Expand Down Expand Up @@ -136,7 +162,15 @@ public TextBuffer toJava(int indent) {
if (constExpr.getValue() instanceof Integer i && i == 0) {
buf.append("repeat(")
.append(conditionExpr.toJava())
.append(") {");
.append(") ");

if (labeled) {
buf.append("label")
.append(id)
.append("@");
}

buf.append("{");

if (!"it".equals(varExpr.getName())) {
buf.append(" ")
Expand All @@ -145,6 +179,12 @@ public TextBuffer toJava(int indent) {
}
buf.appendLineSeparator();
} else {
if (labeled) {
buf.append("label")
.append(id)
.append("@ ");
}

buf.append("for (")
.append(varExpr.toJava(indent))
.append(" in ")
Expand All @@ -155,23 +195,23 @@ public TextBuffer toJava(int indent) {
.appendLineSeparator();
}

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

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

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

if (labeled) {
Expand Down Expand Up @@ -200,7 +240,7 @@ public TextBuffer toJava(int indent) {
buf.append("if (");
buf.append(condition.toJava(indent + 1));
buf.append(") break").appendLineSeparator();
buf.append(ExprProcessor.jmpWrapper(statement.getFirst(), indent + 1, false));
buf.append(ExprProcessor.jmpWrapper(first, indent + 1, false));
buf.appendLineSeparator();
buf.appendIndent(indent + 1).append(inc.toJava(indent + 1)).appendLineSeparator();
buf.appendIndent(indent).append("}").appendLineSeparator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,45 @@
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.util.TextBuffer;

public class KSequenceStatement extends KStatement<SequenceStatement> {
public class KSequenceStatement extends SequenceStatement {
public KSequenceStatement(SequenceStatement statement) {
super(statement);
super();

setFirst(statement.getFirst());
stats.addAllWithKey(statement.getStats(), statement.getStats().getLstKeys());
parent = statement.getParent();
first = statement.getFirst();
exprents = statement.getExprents();
labelEdges.addAll(statement.getLabelEdges());
varDefinitions.addAll(statement.getVarDefinitions());
post = statement.getPost();
lastBasicType = statement.getLastBasicType();

isMonitorEnter = statement.isMonitorEnter();
containsMonitorExit = statement.containsMonitorExit();
isLastAthrow = statement.containsMonitorExitOrAthrow() && !containsMonitorExit;

continueSet = statement.getContinueSet();
}

@Override
public TextBuffer toJava(int indent) {
TextBuffer buf = new TextBuffer();
boolean labeled = statement.isLabeled();
boolean labeled = isLabeled();

buf.append(ExprProcessor.listToJava(statement.getVarDefinitions(), indent));
buf.append(ExprProcessor.listToJava(varDefinitions, indent));

if (labeled) {
buf.appendIndent(indent++)
.append("run label")
.append(statement.id)
.append(id)
.append("@{")
.appendLineSeparator();
}

boolean notEmpty = false;
for (int i = 0; i < statement.getStats().size(); i++) {
Statement st = statement.getStats().get(i);
for (int i = 0; i < stats.size(); i++) {
Statement st = stats.get(i);
TextBuffer str = ExprProcessor.jmpWrapper(st, indent, false);

if (i > 0 && !str.containsOnlyWhitespaces() && notEmpty) {
Expand Down
Loading

0 comments on commit 0981559

Please sign in to comment.