74 lines
2.0 KiB
Java
74 lines
2.0 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.ast.dynamic.ExprGroup;
|
|
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.Objects;
|
|
import java.util.stream.Stream;
|
|
|
|
public class Script extends Decompilable {
|
|
private final DynamicExpr content;
|
|
|
|
public Script(List<Expr<?>> expressions) {
|
|
this(expressions, null);
|
|
}
|
|
|
|
public Script(List<Expr<?>> expressions, String file) {
|
|
this(new ExprGroup(CodeLocation.NONE.withFile(file), expressions));
|
|
}
|
|
|
|
private Script(DynamicExpr content) {
|
|
super(Order.Script);
|
|
this.content = Objects.requireNonNull(content);
|
|
}
|
|
|
|
public Dynamic run(DObject scope) {
|
|
return content.get(scope);
|
|
}
|
|
|
|
public Dynamic run(Scope scope) {
|
|
return content.get(scope);
|
|
}
|
|
|
|
public DCallable bindTo(Scope scope) {
|
|
return DFinal.of("<root>", args -> {
|
|
scope.set("args", args);
|
|
return run(scope);
|
|
}, this::asExpr);
|
|
}
|
|
|
|
public DynamicExpr asExpr() {
|
|
return content;
|
|
}
|
|
|
|
public Script optimize() {
|
|
return new Script(content.optimize());
|
|
}
|
|
|
|
public Script concat(Script other) {
|
|
return new Script(Stream.concat(this.stream(), other.stream()).toList());
|
|
}
|
|
|
|
public Stream<Expr<?>> stream() {
|
|
return content instanceof ExprGroup g
|
|
? g.stream()
|
|
: Stream.of(content);
|
|
}
|
|
|
|
@Override
|
|
public void decompile(ExprWriter writer) throws IOException {
|
|
for (Expr<?> expr : stream().toList()) {
|
|
expr.decompile(writer);
|
|
writer.append(";\n");
|
|
}
|
|
}
|
|
}
|