feat(muscript): IOLib from respackopts
This commit is contained in:
parent
df1572edbd
commit
8d741c5e49
|
@ -1 +1 @@
|
|||
version = "1.4-SNAPSHOT"
|
||||
version = "1.5-SNAPSHOT"
|
|
@ -1,7 +1,7 @@
|
|||
package io.gitlab.jfronny.muscript.gson.test;
|
||||
|
||||
import io.gitlab.jfronny.commons.serialize.gson.api.v2.GsonHolders;
|
||||
import io.gitlab.jfronny.muscript.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.libs.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import io.gitlab.jfronny.muscript.data.Scope;
|
||||
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
||||
|
|
|
@ -1,7 +1,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.libs.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.ast.DynamicExpr;
|
||||
import io.gitlab.jfronny.muscript.ast.Expr;
|
||||
import io.gitlab.jfronny.muscript.compiler.ExprWriter;
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package io.gitlab.jfronny.muscript.libs;
|
||||
|
||||
import io.gitlab.jfronny.muscript.compiler.MuScriptVersion;
|
||||
import io.gitlab.jfronny.muscript.data.Scope;
|
||||
import io.gitlab.jfronny.muscript.data.Script;
|
||||
import io.gitlab.jfronny.muscript.data.dynamic.DList;
|
||||
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
||||
import io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IOLib {
|
||||
public static void addTo(MuScriptVersion version, Scope scope, IOWrapper io) {
|
||||
if (version.contains(MuScriptVersion.V3)) {
|
||||
scope.set("readString", args -> {
|
||||
if (args.size() != 1) throw new IllegalArgumentException("Invalid number of arguments for readString: expected 1 but got " + args.size());
|
||||
return DFinal.of(io.readString(args.get(0).asString().getValue()));
|
||||
}).set("runScript", args -> {
|
||||
if (args.isEmpty()) throw new IllegalArgumentException("Invalid number of arguments for evaluateScript: expected 1 or more but got " + args.size());
|
||||
Script script = io.parseScript(args.get(0).asString().getValue());
|
||||
List<? extends Dynamic> l = args.getValue();
|
||||
DList innerArgs = DFinal.of(l.subList(1, l.size()));
|
||||
return script.bindTo(scope.fork()).call(innerArgs);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
package io.gitlab.jfronny.muscript.libs;
|
||||
|
||||
import io.gitlab.jfronny.muscript.compiler.MuScriptVersion;
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import io.gitlab.jfronny.muscript.compiler.SourceFS;
|
||||
import io.gitlab.jfronny.muscript.data.Script;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
public interface IOWrapper {
|
||||
String readString(String path);
|
||||
|
||||
Script parseScript(String path);
|
||||
|
||||
class Caching implements IOWrapper {
|
||||
private final Map<String, String> cachedStrings = new HashMap<>();
|
||||
private final Map<String, Script> cachedScripts = new HashMap<>();
|
||||
private final IOWrapper inner;
|
||||
|
||||
public Caching(IOWrapper inner) {
|
||||
this.inner = Objects.requireNonNull(inner);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readString(String path) {
|
||||
return cachedStrings.computeIfAbsent(path, inner::readString);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Script parseScript(String path) {
|
||||
return cachedScripts.computeIfAbsent(path, inner::parseScript);
|
||||
}
|
||||
}
|
||||
|
||||
class SourceFSWrapper implements IOWrapper {
|
||||
private final SourceFS inner;
|
||||
private final MuScriptVersion version;
|
||||
|
||||
public SourceFSWrapper(MuScriptVersion version, SourceFS inner) {
|
||||
this.inner = Objects.requireNonNull(inner);
|
||||
this.version = Objects.requireNonNull(version);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String readString(String path) {
|
||||
return inner.read(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Script parseScript(String path) {
|
||||
return Parser.parseMultiScript(version, path, inner);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package io.gitlab.jfronny.muscript;
|
||||
package io.gitlab.jfronny.muscript.libs;
|
||||
|
||||
import io.gitlab.jfronny.muscript.ast.Expr;
|
||||
import io.gitlab.jfronny.muscript.ast.dynamic.Call;
|
|
@ -6,6 +6,7 @@ module io.gitlab.jfronny.commons.muscript {
|
|||
exports io.gitlab.jfronny.muscript.ast; // General purpose expressions (needed to properly interact with parsed)
|
||||
exports io.gitlab.jfronny.muscript.debug;
|
||||
exports io.gitlab.jfronny.muscript.error;
|
||||
exports io.gitlab.jfronny.muscript.libs;
|
||||
exports io.gitlab.jfronny.muscript.data;
|
||||
exports io.gitlab.jfronny.muscript.data.dynamic;
|
||||
exports io.gitlab.jfronny.muscript.data.dynamic.additional;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.gitlab.jfronny.muscript.test;
|
||||
|
||||
import io.gitlab.jfronny.muscript.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.libs.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import io.gitlab.jfronny.muscript.compiler.SourceFS;
|
||||
import io.gitlab.jfronny.muscript.data.Scope;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.gitlab.jfronny.muscript.test;
|
||||
|
||||
import io.gitlab.jfronny.muscript.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.libs.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import io.gitlab.jfronny.muscript.data.Scope;
|
||||
import io.gitlab.jfronny.muscript.error.LocationalException;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package io.gitlab.jfronny.muscript.test;
|
||||
|
||||
import io.gitlab.jfronny.commons.StringFormatter;
|
||||
import io.gitlab.jfronny.muscript.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.libs.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import io.gitlab.jfronny.muscript.error.LocationalException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package io.gitlab.jfronny.muscript.test.util;
|
||||
|
||||
import io.gitlab.jfronny.muscript.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.libs.StandardLib;
|
||||
import io.gitlab.jfronny.muscript.data.Scope;
|
||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||
import io.gitlab.jfronny.muscript.ast.Expr;
|
||||
|
|
Loading…
Reference in New Issue