fix(muscript): restore old convenience methods for decompiling
This commit is contained in:
parent
3d4c5a06ff
commit
f04d5ce407
|
@ -8,7 +8,18 @@ import java.util.ServiceLoader;
|
|||
public interface IExprSerializer {
|
||||
IExprSerializer INSTANCE = ServiceLoader.load(IExprSerializer.class)
|
||||
.findFirst()
|
||||
.orElseGet(() -> expr -> null);
|
||||
.orElseGet(() -> new IExprSerializer() {
|
||||
@Override
|
||||
public @Nullable String serialize(Expr expr) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable String serialize(Script script) {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
@Nullable String serialize(Expr expr);
|
||||
@Nullable String serialize(Script script);
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import io.gitlab.jfronny.muscript.ast.*;
|
|||
import io.gitlab.jfronny.muscript.ast.bool.*;
|
||||
import io.gitlab.jfronny.muscript.ast.context.Script;
|
||||
import io.gitlab.jfronny.muscript.ast.dynamic.*;
|
||||
import io.gitlab.jfronny.muscript.ast.extensible.ExtensibleExpr;
|
||||
import io.gitlab.jfronny.muscript.ast.extensible.*;
|
||||
import io.gitlab.jfronny.muscript.ast.number.*;
|
||||
import io.gitlab.jfronny.muscript.ast.string.*;
|
||||
import io.gitlab.jfronny.muscript.core.ExprWriter;
|
||||
|
@ -21,6 +21,14 @@ import java.util.stream.Stream;
|
|||
import static io.gitlab.jfronny.muscript.ast.context.ExprUtils.asString;
|
||||
|
||||
public abstract class Decompiler {
|
||||
public static String decompile(Expr expr) {
|
||||
return ExprWriter.write(writer -> decompile(writer, expr), false);
|
||||
}
|
||||
|
||||
public static String decompile(Script script) {
|
||||
return ExprWriter.write(writer -> decompile(writer, script), false);
|
||||
}
|
||||
|
||||
public static void decompile(ExprWriter writer, Script script) throws IOException {
|
||||
for (Expr expr : script.stream().toList()) {
|
||||
decompile(writer, expr);
|
||||
|
@ -30,6 +38,7 @@ public abstract class Decompiler {
|
|||
|
||||
public static void decompile(ExprWriter writer, Expr expr) throws IOException {
|
||||
switch (expr) {
|
||||
case null -> throw new NullPointerException();
|
||||
case DynamicCoerce(var inner) -> decompile(writer, inner);
|
||||
case StringCoerce(var inner) -> decompile(writer, inner);
|
||||
case BoolUnpack(var inner) -> decompile(writer, inner);
|
||||
|
@ -62,7 +71,7 @@ public abstract class Decompiler {
|
|||
}
|
||||
case And(var location, var left, var right) -> {
|
||||
parenthesize(expr, left, writer, false);
|
||||
writer.append(" && ");
|
||||
writer.append(" & ");
|
||||
parenthesize(expr, right, writer, true);
|
||||
}
|
||||
case Not(var location, var inner) -> {
|
||||
|
@ -181,21 +190,15 @@ public abstract class Decompiler {
|
|||
if (i == boundArgs.size() - 1 && variadic) writer.append("...");
|
||||
}
|
||||
writer.append(" ->");
|
||||
if (steps.isEmpty()) {
|
||||
writer.append(" ");
|
||||
decompile(writer, finish);
|
||||
writer.append(" }");
|
||||
} else {
|
||||
writer.increaseIndent();
|
||||
for (Expr step : steps) {
|
||||
writer.append("\n");
|
||||
decompile(writer, step);
|
||||
writer.append(";");
|
||||
}
|
||||
writer.increaseIndent();
|
||||
for (Expr step : steps) {
|
||||
writer.append("\n");
|
||||
decompile(writer, finish);
|
||||
writer.append(";").decreaseIndent().append("\n}");
|
||||
decompile(writer, step);
|
||||
writer.append(";");
|
||||
}
|
||||
writer.append("\n");
|
||||
decompile(writer, finish);
|
||||
writer.append(";").decreaseIndent().append("\n}");
|
||||
}
|
||||
case ExprGroup(var location, var steps, var finish, var packedArgs, var fork) -> decompile(writer, fork
|
||||
? new Call(
|
||||
|
|
|
@ -2,12 +2,17 @@ package io.gitlab.jfronny.muscript.serialize.impl;
|
|||
|
||||
import io.gitlab.jfronny.muscript.ast.Expr;
|
||||
import io.gitlab.jfronny.muscript.ast.context.IExprSerializer;
|
||||
import io.gitlab.jfronny.muscript.core.ExprWriter;
|
||||
import io.gitlab.jfronny.muscript.ast.context.Script;
|
||||
import io.gitlab.jfronny.muscript.serialize.Decompiler;
|
||||
|
||||
public class ExprSerializerImpl implements IExprSerializer {
|
||||
@Override
|
||||
public String serialize(Expr expr) {
|
||||
return ExprWriter.write(writer -> Decompiler.decompile(writer, expr), false);
|
||||
return Decompiler.decompile(expr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize(Script script) {
|
||||
return Decompiler.decompile(script);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue