muScript: two fixes for decompiling
ci/woodpecker/push/woodpecker Pipeline was successful Details

This commit is contained in:
Johannes Frohnmeyer 2023-03-12 19:22:45 +01:00
parent 3968ac45a9
commit 993f0bc6b7
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 50 additions and 9 deletions

View File

@ -68,7 +68,7 @@ public class Call extends DynamicExpr {
writer.decreaseIndent();
writer.append("\n)");
} else {
writer.append("(");
writer.append('(');
boolean first = true;
for (Arg arg : args) {
if (!first) writer.append(", ");
@ -76,6 +76,7 @@ public class Call extends DynamicExpr {
arg.expr.decompile(writer);
if (arg.variadic) writer.append("...");
}
writer.append(')');
}
}

View File

@ -14,7 +14,7 @@ public abstract class Decompilable {
public abstract void decompile(ExprWriter writer) throws IOException;
protected void parenthesize(Decompilable val, ExprWriter writer, boolean parenEqualOrder) throws IOException {
boolean wrap = !parenEqualOrder ? val.order.ordinal() > this.order.ordinal() : val.order.ordinal() >= this.order.ordinal();
boolean wrap = !parenEqualOrder ? val.order.ordinal() < this.order.ordinal() : val.order.ordinal() <= this.order.ordinal();
if (wrap) writer.append('(');
val.decompile(writer);
if (wrap) writer.append(')');
@ -33,7 +33,7 @@ public abstract class Decompilable {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
try (ExprWriter ew = new ExprWriter(sb)) {
try (ExprWriter ew = new ExprWriter(sb, false)) {
decompile(ew);
} catch (IOException e) {
throw new RuntimeException("Could not decompile", e);

View File

@ -2,19 +2,20 @@ package io.gitlab.jfronny.muscript.compiler;
import java.io.Closeable;
import java.io.IOException;
import java.util.stream.Collectors;
public class ExprWriter implements Appendable, Closeable {
private final Appendable target;
private final boolean compact;
private int indent = 0;
public ExprWriter(Appendable target) {
public ExprWriter(Appendable target, boolean compact) {
this.target = target;
this.compact = compact;
}
@Override
public ExprWriter append(CharSequence csq) throws IOException {
target.append(csq.toString().lines().collect(Collectors.joining("\n" + indent())));
target.append(csq.toString().replace("\r", "").replace("\n", compact ? "" : "\n" + indent()));
return this;
}
@ -25,8 +26,13 @@ public class ExprWriter implements Appendable, Closeable {
@Override
public ExprWriter append(char c) throws IOException {
if (c == '\n' || c == '\r') target.append("\n").append(indent());
else target.append(c);
switch (c) {
case '\r' -> {}
case '\n' -> {
if (!compact) target.append("\n").append(indent());
}
default -> target.append(c);
}
return this;
}

View File

@ -27,7 +27,7 @@ public sealed interface Dynamic<T> permits DBool, DNumber, DString, DObject, DLi
static String serialize(Dynamic<?> dynamic) {
StringBuilder sb = new StringBuilder();
try (ExprWriter ew = new ExprWriter(sb)) {
try (ExprWriter ew = new ExprWriter(sb, true)) {
dynamic.serialize(ew);
} catch (IOException e) {
throw new RuntimeException("Could not stringify", e);

View File

@ -0,0 +1,34 @@
package io.gitlab.jfronny.muscript.test;
import io.gitlab.jfronny.muscript.compiler.Parser;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DecompileTest {
private static final String script = """
clientVersion = challenge({ ->
mod('better-whitelist').version;
});
assert(mod('better-whitelist').version == clientVersion, 'You have the wrong mod version');
assert(challenge({ arg ->
allMatch(arg, { v ->
anyMatch(mods, { m ->
v.name == m.name & v.version == m.version;
});
});
}, map(filter(mods, { v ->
v.environment != 'server';
}), { v ->
{
name = v.name,
version = v.version
};
})));
""";
@Test
void testDecompile() {
assertEquals(script, Parser.parseScript(script).toString());
}
}