fix(muscript): clean up ExprGroup side effect handling for extractSideEffects and decompile
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Johannes Frohnmeyer 2023-09-22 18:20:52 +02:00
parent 8aebf179d4
commit 26bd73d87e
Signed by: Johannes
GPG Key ID: E76429612C2929F4

View File

@ -3,6 +3,7 @@ package io.gitlab.jfronny.muscript.ast.dynamic;
import io.gitlab.jfronny.muscript.ast.DynamicExpr; import io.gitlab.jfronny.muscript.ast.DynamicExpr;
import io.gitlab.jfronny.muscript.ast.Expr; import io.gitlab.jfronny.muscript.ast.Expr;
import io.gitlab.jfronny.muscript.ast.NullLiteral; import io.gitlab.jfronny.muscript.ast.NullLiteral;
import io.gitlab.jfronny.muscript.ast.string.Concatenate;
import io.gitlab.jfronny.muscript.compiler.CodeLocation; import io.gitlab.jfronny.muscript.compiler.CodeLocation;
import io.gitlab.jfronny.muscript.compiler.ExprWriter; import io.gitlab.jfronny.muscript.compiler.ExprWriter;
import io.gitlab.jfronny.muscript.compiler.Order; import io.gitlab.jfronny.muscript.compiler.Order;
@ -37,6 +38,7 @@ public class ExprGroup extends DynamicExpr {
this.fin = expressions.get(expressions.size() - 1).asDynamicExpr(); this.fin = expressions.get(expressions.size() - 1).asDynamicExpr();
this.packedArgs = packedArgs; this.packedArgs = packedArgs;
this.fork = fork; this.fork = fork;
if (!fork && packedArgs != null) throw new UnsupportedOperationException("packedArgs may only be used for forking ExprGroups");
} }
public static Expr<?> of(CodeLocation location, List<Expr<?>> expressions) { public static Expr<?> of(CodeLocation location, List<Expr<?>> expressions) {
@ -52,7 +54,7 @@ public class ExprGroup extends DynamicExpr {
@Override @Override
public Dynamic get(Scope dataRoot) { public Dynamic get(Scope dataRoot) {
Scope fork = this.fork ? dataRoot.fork() : dataRoot; Scope fork = this.fork ? dataRoot.fork() : dataRoot;
if (packedArgs != null) { if (this.fork && packedArgs != null) {
List<Dynamic> from = new LinkedList<>(); List<Dynamic> from = new LinkedList<>();
for (Call.Arg arg : packedArgs.from) { for (Call.Arg arg : packedArgs.from) {
Dynamic data = arg.expr().get(dataRoot); Dynamic data = arg.expr().get(dataRoot);
@ -95,23 +97,32 @@ public class ExprGroup extends DynamicExpr {
@Override @Override
public Stream<Expr<?>> extractSideEffects() { public Stream<Expr<?>> extractSideEffects() {
return Stream.of(of(location, stream().flatMap(Expr::extractSideEffects).toList())); return fork ? Stream.of(this) : Stream.of(of(location, stream().flatMap(Expr::extractSideEffects).toList()));
} }
@Override @Override
public void decompile(ExprWriter writer) throws IOException { public void decompile(ExprWriter writer) throws IOException {
new Call( if (!fork) {
location, // Use string concatenation since the result is ignored either way
new Closure( stream()
location, .reduce((left, right) -> new Concatenate(location, left.asStringExpr(), right.asStringExpr()))
packedArgs == null .orElseGet(() -> new NullLiteral(location))
? List.of() .optimize()
: packedArgs.to, .decompile(writer);
stream().toList(), } else {
false new Call(
), location,
packedArgs == null ? List.of() : packedArgs.from new Closure(
).decompile(writer); location,
packedArgs == null
? List.of()
: packedArgs.to,
stream().toList(),
false
),
packedArgs == null ? List.of() : packedArgs.from
).decompile(writer);
}
} }
public Stream<Expr<?>> stream() { public Stream<Expr<?>> stream() {