71 lines
2.1 KiB
Java
71 lines
2.1 KiB
Java
package io.gitlab.jfronny.muscript.data;
|
|
|
|
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
|
import io.gitlab.jfronny.muscript.ast.Expr;
|
|
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
|
|
import io.gitlab.jfronny.muscript.ast.dynamic.Closure;
|
|
import io.gitlab.jfronny.muscript.compiler.*;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
|
import io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal;
|
|
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.stream.Stream;
|
|
|
|
public class Script extends Decompilable {
|
|
private final List<Expr<?>> steps;
|
|
private final DynamicExpr fin;
|
|
|
|
public Script(List<Expr<?>> expressions) {
|
|
this(expressions.subList(0, expressions.size() - 1), expressions.get(expressions.size() - 1).asDynamicExpr());
|
|
}
|
|
|
|
private Script(List<Expr<?>> steps, DynamicExpr fin) {
|
|
super(Order.Script);
|
|
this.steps = steps;
|
|
this.fin = fin;
|
|
}
|
|
|
|
public Dynamic<?> run(DObject scope) {
|
|
return run(new Scope(scope));
|
|
}
|
|
|
|
public Dynamic<?> run(Scope scope) {
|
|
for (Expr<?> expression : steps) {
|
|
expression.get(scope);
|
|
}
|
|
return fin.get(scope);
|
|
}
|
|
|
|
public DCallable bindTo(Scope scope) {
|
|
return DFinal.of(args -> {
|
|
scope.set("args", args);
|
|
return run(scope);
|
|
}, () -> "{->\n" + this + "\n}()");
|
|
}
|
|
|
|
public DynamicExpr asExpr() {
|
|
return new Call(-1, -1, new Closure(-1, -1, List.of(), stream().toList(), false), List.of());
|
|
}
|
|
|
|
public Script optimize() {
|
|
return new Script(steps.stream().<Expr<?>>map(Expr::optimize).toList(), fin.optimize());
|
|
}
|
|
|
|
public Script concat(Script other) {
|
|
return new Script(Stream.concat(this.stream(), other.stream()).toList());
|
|
}
|
|
|
|
public Stream<Expr<?>> stream() {
|
|
return Stream.concat(steps.stream(), Stream.of(fin));
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
for (Expr<?> expr : stream().toList()) {
|
|
expr.decompile(writer);
|
|
writer.append(";\n");
|
|
}
|
|
}
|
|
}
|