Don't duplicate decompile logic to Dynamic, provide toExpr()

This commit is contained in:
Johannes Frohnmeyer 2023-04-18 14:07:28 +02:00
parent e652bfe6ba
commit 7ef8dee78c
Signed by: Johannes
GPG Key ID: E76429612C2929F4
15 changed files with 97 additions and 87 deletions

View File

@ -22,14 +22,14 @@ public class Bind extends DynamicExpr {
@Override
public Dynamic<?> get(Scope dataRoot) {
return DFinal.of(args -> {
return DFinal.of("<bind>", args -> {
List<Dynamic<?>> argsWithParameter = new LinkedList<>(args.getValue());
argsWithParameter.add(0, parameter.get(dataRoot));
return callable.get(dataRoot)
.asCallable()
.getValue()
.apply(DFinal.of(argsWithParameter));
}, this::toString, "<bind>");
}, () -> this);
}
@Override

View File

@ -44,10 +44,10 @@ public class Script extends Decompilable {
}
public DCallable bindTo(Scope scope) {
return DFinal.of(args -> {
return DFinal.of("<root>", args -> {
scope.set("args", args);
return run(scope);
}, () -> "{->\n" + this + "\n}()", "<root>");
}, this::asExpr);
}
public DynamicExpr asExpr() {

View File

@ -1,12 +1,12 @@
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import java.io.IOException;
import io.gitlab.jfronny.muscript.ast.BoolExpr;
import io.gitlab.jfronny.muscript.ast.literal.BoolLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
public non-sealed interface DBool extends Dynamic<Boolean> {
@Override
default void serialize(ExprWriter writer) throws IOException {
writer.append(getValue().toString());
default BoolExpr toExpr() {
return new BoolLiteral(CodeLocation.NONE, getValue());
}
}

View File

@ -1,8 +1,10 @@
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
import io.gitlab.jfronny.muscript.ast.dynamic.Variable;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import java.io.IOException;
import java.util.List;
public non-sealed interface DList extends Dynamic<List<Dynamic<?>>> {
@ -19,14 +21,7 @@ public non-sealed interface DList extends Dynamic<List<Dynamic<?>>> {
}
@Override
default void serialize(ExprWriter writer) throws IOException {
writer.append("listOf(");
boolean first = true;
for (Dynamic<?> dynamic : getValue()) {
if (!first) writer.append(", ");
first = false;
dynamic.serialize(writer);
}
writer.append(')');
default DynamicExpr toExpr() {
return new Call(CodeLocation.NONE, new Variable(CodeLocation.NONE, "listOf"), getValue().stream().map(s -> new Call.Arg(s.toExpr().asDynamicExpr(), false)).toList());
}
}

View File

@ -1,8 +1,7 @@
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import java.io.IOException;
import io.gitlab.jfronny.muscript.ast.NullLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
public final class DNull implements Dynamic<Object> {
@Override
@ -11,8 +10,8 @@ public final class DNull implements Dynamic<Object> {
}
@Override
public void serialize(ExprWriter writer) throws IOException {
writer.append(toString());
public NullLiteral toExpr() {
return new NullLiteral(CodeLocation.NONE);
}
@Override

View File

@ -1,13 +1,12 @@
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import java.io.IOException;
import io.gitlab.jfronny.muscript.ast.NumberExpr;
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
public non-sealed interface DNumber extends Dynamic<Double> {
@Override
default void serialize(ExprWriter writer) throws IOException {
writer.append(StringFormatter.toString(getValue()));
default NumberExpr toExpr() {
return new NumberLiteral(CodeLocation.NONE, getValue());
}
}

View File

@ -1,9 +1,11 @@
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.ObjectLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
public non-sealed interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
default Dynamic<?> get(String key) {
@ -15,15 +17,7 @@ public non-sealed interface DObject extends Dynamic<Map<String, Dynamic<?>>> {
}
@Override
default void serialize(ExprWriter writer) throws IOException {
writer.append('{');
boolean first = true;
for (Map.Entry<String, Dynamic<?>> entry : getValue().entrySet()) {
if (!first) writer.append(", ");
first = false;
writer.appendLiteral(entry.getKey()).append(" = ");
entry.getValue().serialize(writer);
}
writer.append('}');
default DynamicExpr toExpr() {
return new ObjectLiteral(CodeLocation.NONE, getValue().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, s -> s.getValue().toExpr().asDynamicExpr())));
}
}

View File

@ -1,13 +1,12 @@
package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.muscript.compiler.Decompilable;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import java.io.IOException;
import io.gitlab.jfronny.muscript.ast.StringExpr;
import io.gitlab.jfronny.muscript.ast.literal.StringLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
public non-sealed interface DString extends Dynamic<String> {
@Override
default void serialize(ExprWriter writer) throws IOException {
writer.append(Decompilable.enquote(getValue()));
default StringExpr toExpr() {
return new StringLiteral(CodeLocation.NONE, getValue());
}
}

View File

@ -3,6 +3,7 @@ package io.gitlab.jfronny.muscript.data.dynamic;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.muscript.StandardLib;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.Expr;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.compiler.Parser;
import io.gitlab.jfronny.muscript.data.dynamic.additional.*;
@ -35,7 +36,11 @@ public sealed interface Dynamic<T> permits DBool, DNumber, DString, DObject, DLi
return sb.toString();
}
void serialize(ExprWriter writer) throws IOException;
default void serialize(ExprWriter writer) throws IOException {
toExpr().decompile(writer);
}
Expr<?> toExpr();
T getValue();
default DBool asBool() {

View File

@ -1,9 +1,13 @@
package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.*;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
public record DCallableObject(Map<String, Dynamic<?>> value, DCallable callable) implements DObject {
@ -18,11 +22,8 @@ public record DCallableObject(Map<String, Dynamic<?>> value, DCallable callable)
}
@Override
public void serialize(ExprWriter writer) throws IOException {
DObject.super.serialize(writer);
writer.append("::callableObject(");
callable.serialize(writer);
writer.append(')');
public DynamicExpr toExpr() {
return new Call(CodeLocation.NONE, new Bind(CodeLocation.NONE, new Variable(CodeLocation.NONE, "callableObject"), DObject.super.toExpr()), List.of(new Call.Arg(callable.toExpr().asDynamicExpr(), false)));
}
@Override

View File

@ -1,10 +1,16 @@
package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
import io.gitlab.jfronny.muscript.ast.dynamic.Variable;
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
@ -29,12 +35,12 @@ public record DDate(Supplier<LocalDate> date) implements DObject {
}
@Override
public void serialize(ExprWriter writer) throws IOException {
writer.append("date(")
.append(String.valueOf(date.get().getYear())).append(", ")
.append(String.valueOf(date.get().getMonthValue())).append(", ")
.append(String.valueOf(date.get().getDayOfMonth()))
.append(")");
public DynamicExpr toExpr() {
return new Call(CodeLocation.NONE, new Variable(CodeLocation.NONE, "date"), List.of(
new Call.Arg(new NumberLiteral(CodeLocation.NONE, date.get().getYear()).asDynamicExpr(), false),
new Call.Arg(new NumberLiteral(CodeLocation.NONE, date.get().getMonthValue()).asDynamicExpr(), false),
new Call.Arg(new NumberLiteral(CodeLocation.NONE, date.get().getDayOfMonth()).asDynamicExpr(), false)
));
}
@Override

View File

@ -1,8 +1,11 @@
package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.muscript.compiler.Decompilable;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
import io.gitlab.jfronny.muscript.ast.dynamic.Variable;
import io.gitlab.jfronny.muscript.ast.literal.StringLiteral;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import org.jetbrains.annotations.Nullable;
@ -51,13 +54,11 @@ public record DEnum(Map<String, Dynamic<?>> values, @Nullable DEnumEntry value)
}
@Override
public void serialize(ExprWriter writer) throws IOException {
writer.append("enum(");
DObject.super.serialize(writer);
if (value != null) {
writer.append(", ").append(Decompilable.enquote(value.value));
}
writer.append(')');
public DynamicExpr toExpr() {
List<Call.Arg> args = new LinkedList<>();
args.add(new Call.Arg(DObject.super.toExpr(), false));
if (value != null) args.add(new Call.Arg(new StringLiteral(CodeLocation.NONE, value.value).asDynamicExpr(), false));
return new Call(CodeLocation.NONE, new Variable(CodeLocation.NONE, "enum"), args);
}
private static Map<String, Dynamic<?>> createMap(List<String> values, String value) {

View File

@ -3,7 +3,9 @@ package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.commons.LazySupplier;
import io.gitlab.jfronny.commons.StringFormatter;
import io.gitlab.jfronny.commons.data.ImmCollection;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.ast.Expr;
import io.gitlab.jfronny.muscript.ast.dynamic.Variable;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import java.io.IOException;
@ -42,10 +44,14 @@ public class DFinal {
}
public static DCallable of(Function<DList, ? extends Dynamic<?>> b, String name) {
return of(b, () -> name, name);
return of(name, b, () -> new Variable(CodeLocation.NONE, name));
}
public static DCallable of(Function<DList, ? extends Dynamic<?>> b, Supplier<String> serialized, String name) {
return of(name, b, () -> Parser.parse(serialized.get()));
}
public static DCallable of(String name, Function<DList, ? extends Dynamic<?>> b, Supplier<Expr<?>> serialized) {
return new FCallable((Function<DList, Dynamic<?>>) b, new LazySupplier<>(serialized), name);
}
@ -115,10 +121,10 @@ public class DFinal {
}
}
private record FCallable(Function<DList, Dynamic<?>> value, Supplier<String> string, String name) implements DCallable, FImpl {
private record FCallable(Function<DList, Dynamic<?>> value, Supplier<Expr<?>> gen, String name) implements DCallable, FImpl {
@Override
public void serialize(ExprWriter writer) throws IOException {
writer.append(toString());
public Expr<?> toExpr() {
return gen.get();
}
@Override
@ -128,7 +134,7 @@ public class DFinal {
@Override
public String toString() {
return string.get();
return toExpr().toString();
}
@Override
@ -138,7 +144,7 @@ public class DFinal {
@Override
public DCallable named(String name) {
return new FCallable(value, string, name);
return new FCallable(value, gen, name);
}
}
}

View File

@ -1,10 +1,16 @@
package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
import io.gitlab.jfronny.muscript.ast.dynamic.Variable;
import io.gitlab.jfronny.muscript.ast.literal.NumberLiteral;
import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import java.io.IOException;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
@ -29,12 +35,12 @@ public record DTime(Supplier<LocalTime> time) implements DObject {
}
@Override
public void serialize(ExprWriter writer) throws IOException {
writer.append("time(")
.append(String.valueOf(time.get().getHour())).append(", ")
.append(String.valueOf(time.get().getMinute())).append(", ")
.append(String.valueOf(time.get().getSecond()))
.append(")");
public DynamicExpr toExpr() {
return new Call(CodeLocation.NONE, new Variable(CodeLocation.NONE, "time"), List.of(
new Call.Arg(new NumberLiteral(CodeLocation.NONE, time.get().getHour()).asDynamicExpr(), false),
new Call.Arg(new NumberLiteral(CodeLocation.NONE, time.get().getMinute()).asDynamicExpr(), false),
new Call.Arg(new NumberLiteral(CodeLocation.NONE, time.get().getSecond()).asDynamicExpr(), false)
));
}
@Override

View File

@ -1,9 +1,8 @@
package io.gitlab.jfronny.muscript.data.dynamic.additional;
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.ast.Expr;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import java.io.IOException;
import java.util.function.Function;
public record NamedDCallable(DCallable inner, String name) implements DCallable {
@ -18,8 +17,8 @@ public record NamedDCallable(DCallable inner, String name) implements DCallable
}
@Override
public void serialize(ExprWriter writer) throws IOException {
inner.serialize(writer);
public Expr<?> toExpr() {
return inner.toExpr();
}
@Override