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

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
1 changed files with 25 additions and 14 deletions

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.Expr;
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.ExprWriter;
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.packedArgs = packedArgs;
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) {
@ -52,7 +54,7 @@ public class ExprGroup extends DynamicExpr {
@Override
public Dynamic get(Scope dataRoot) {
Scope fork = this.fork ? dataRoot.fork() : dataRoot;
if (packedArgs != null) {
if (this.fork && packedArgs != null) {
List<Dynamic> from = new LinkedList<>();
for (Call.Arg arg : packedArgs.from) {
Dynamic data = arg.expr().get(dataRoot);
@ -95,23 +97,32 @@ public class ExprGroup extends DynamicExpr {
@Override
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
public void decompile(ExprWriter writer) throws IOException {
new Call(
location,
new Closure(
location,
packedArgs == null
? List.of()
: packedArgs.to,
stream().toList(),
false
),
packedArgs == null ? List.of() : packedArgs.from
).decompile(writer);
if (!fork) {
// Use string concatenation since the result is ignored either way
stream()
.reduce((left, right) -> new Concatenate(location, left.asStringExpr(), right.asStringExpr()))
.orElseGet(() -> new NullLiteral(location))
.optimize()
.decompile(writer);
} else {
new Call(
location,
new Closure(
location,
packedArgs == null
? List.of()
: packedArgs.to,
stream().toList(),
false
),
packedArgs == null ? List.of() : packedArgs.from
).decompile(writer);
}
}
public Stream<Expr<?>> stream() {