Skip to content

Commit

Permalink
String interpolation initial run
Browse files Browse the repository at this point in the history
  • Loading branch information
sschr15 committed Aug 6, 2024
1 parent f18b01c commit 89ab540
Show file tree
Hide file tree
Showing 17 changed files with 693 additions and 429 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private static Pass makePass() {
// TODO: preference for this pass
.addPass("ResugarMethods", new ResugarKotlinMethodsPass())
.addPass("ReplaceContinue", ctx -> LabelHelper.replaceContinueWithBreak(ctx.getRoot()))
.addPass("CollapseStringConcat", new CollapseStringConcatPass())

.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.vineflower.kotlin.expr;

import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.modules.decompiler.exps.*;
import org.jetbrains.java.decompiler.modules.decompiler.vars.CheckTypesResult;
import org.jetbrains.java.decompiler.struct.gen.CodeType;
Expand Down Expand Up @@ -196,6 +198,22 @@ public TextBuffer toJava(int indent) {
.append(wrapOperandString(lstOperands.get(1), true, indent));
return buf;
}
case STR_CONCAT -> {
buf.append('"');
for (Exprent expr : lstOperands) {
if (expr instanceof ConstExprent constExpr && VarType.VARTYPE_STRING.equals(constExpr.getExprType())) {
boolean ascii = DecompilerContext.getOption(IFernflowerPreferences.ASCII_STRING_CHARACTERS);
String value = ConstExprent.convertStringToJava((String) constExpr.getValue(), ascii);
buf.append(value.replace("$", "\\$"));
} else if (expr instanceof VarExprent var) {
buf.append("$").append(var.toJava(indent));
} else {
buf.append("${").append(expr.toJava(indent)).append("}");
}
}
buf.append('"');
return buf;
}
}

return buf.append(super.toJava(indent));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.vineflower.kotlin.pass;

import org.jetbrains.java.decompiler.api.plugin.pass.Pass;
import org.jetbrains.java.decompiler.api.plugin.pass.PassContext;
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.FunctionExprent;
import org.jetbrains.java.decompiler.modules.decompiler.stats.BasicBlockStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.IfStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.vineflower.kotlin.expr.KFunctionExprent;

import java.util.ArrayList;
import java.util.List;

public class CollapseStringConcatPass implements Pass {
@Override
public boolean run(PassContext ctx) {
return run(ctx.getRoot());
}

private static boolean run(Statement stat) {
boolean res = false;

for (Statement st : stat.getStats()) {
res |= run(st);
}

List<Exprent> exprs = List.of();
if (stat instanceof BasicBlockStatement) {
exprs = stat.getExprents();
} else if (stat instanceof IfStatement) {
exprs = ((IfStatement)stat).getHeadexprentList();
}

for (Exprent ex : exprs) {
res |= run(ex);
}

return res;
}

private static boolean run(Exprent ex) {
boolean res = false;

for (Exprent e : ex.getAllExprents()) {
res |= run(e);
}

if (ex instanceof KFunctionExprent kex && kex.getFuncType() == FunctionExprent.FunctionType.STR_CONCAT) {
List<Exprent> operands = new ArrayList<>(kex.getLstOperands());
List<Exprent> lstOperands = kex.getLstOperands();
for (Exprent child : operands) {
if (child instanceof KFunctionExprent childKex && childKex.getFuncType() == FunctionExprent.FunctionType.STR_CONCAT) {
lstOperands.addAll(lstOperands.indexOf(child), childKex.getLstOperands());
lstOperands.remove(child);
res = true;
}
}
}

return res;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ private void registerKotlinTests() {
register(KOTLIN, "TestReflection");
register(KOTLIN, "TestConstructors");
register(KOTLIN, "TestContracts");
register(KOTLIN, "TestStringInterpolation");
}
}
4 changes: 2 additions & 2 deletions plugins/kotlin/testData/results/pkg/TestConstructors.dec
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package pkg

class TestConstructors private constructor() {
public constructor(a: Int) : this() {// 4
System.out.println("a = " + a);// 5
System.out.println("a = $a");// 5
}// 6

public constructor(a: Int, b: Int) : this(a) {// 8
System.out.println("b = " + b);// 9
System.out.println("b = $b");// 9
}// 10
}

Expand Down
Loading

0 comments on commit 89ab540

Please sign in to comment.