Simplify Scope creation
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
9eb316c4a0
commit
d9467314ef
@ -89,7 +89,7 @@ one by calling `as(Bool|String|Number|Dynamic)Expr`.
|
|||||||
This process may throw a ParseException.
|
This process may throw a ParseException.
|
||||||
You may also use `Parser.parseScript(String script)` for multi-expression scripts.
|
You may also use `Parser.parseScript(String script)` for multi-expression scripts.
|
||||||
You can call `get(Dynamic<?> dataRoot)` on the result to execute the script on the provided data, which should be a
|
You can call `get(Dynamic<?> dataRoot)` on the result to execute the script on the provided data, which should be a
|
||||||
`Scope` on which you called `StandardLib.addTo()` to add standard methods.
|
`Scope` which you created with `StandardLib.createScope()` to add standard methods.
|
||||||
This is also where you can add custom data to be accessed by your script.
|
This is also where you can add custom data to be accessed by your script.
|
||||||
The execution of a script can throw a LocationalException which may be converted to a LocationalError for printing
|
The execution of a script can throw a LocationalException which may be converted to a LocationalError for printing
|
||||||
using the source of the expression if available.
|
using the source of the expression if available.
|
||||||
@ -98,32 +98,36 @@ You may also call `StarScriptIngester.starScriptToMu()` to generate μScript cod
|
|||||||
A full example could look as follows:
|
A full example could look as follows:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
String source = args[0];
|
public class Example {
|
||||||
Expr<?> parsed;
|
public static void main(String[] args) {
|
||||||
try {
|
String source = String.join(" ", args);
|
||||||
parsed = Parser.parse(source); // or Parser.parse(StarScriptIngester.starScriptToMu(source))
|
Expr<?> parsed;
|
||||||
} catch (Parser.ParseException e) { // Could not parse
|
try {
|
||||||
System.err.println(e.error);
|
parsed = Parser.parse(source); // or Parser.parse(StarScriptIngester.starScriptToMu(source))
|
||||||
return;
|
} catch (Parser.ParseException e) { // Could not parse
|
||||||
|
System.err.println(e.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BoolExpr typed;
|
||||||
|
try {
|
||||||
|
typed = parsed.asBoolExpr();
|
||||||
|
} catch (LocationalException e) {
|
||||||
|
System.err.println(e.asPrintable(source));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Scope scope = StandardLib.createScope()
|
||||||
|
.set("someValue", 15)
|
||||||
|
.set("someOther", Map.of(
|
||||||
|
"subValue", DFinal.of(true)
|
||||||
|
));
|
||||||
|
boolean result;
|
||||||
|
try {
|
||||||
|
result = typed.get(parameter);
|
||||||
|
} catch (LocationalException e) {
|
||||||
|
System.err.println(e.asPrintable(source));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
System.out.println("Result: " + result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
BoolExpr typed;
|
|
||||||
try {
|
|
||||||
typed = parsed.asBoolExpr();
|
|
||||||
} catch (LocationalException e) {
|
|
||||||
System.err.println(e.asPrintable(source));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Scope scope = StandardLib.addTo(new Scope())
|
|
||||||
.set("someValue", DFinal.of(15))
|
|
||||||
.set("someOther", DFinal.of(Map.of(
|
|
||||||
"subValue", DFinal.of(true)
|
|
||||||
)));
|
|
||||||
boolean result;
|
|
||||||
try {
|
|
||||||
result = typed.get(parameter);
|
|
||||||
} catch (LocationalException e) {
|
|
||||||
System.err.println(e.asPrintable(source));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
System.out.println("Result: " + result);
|
|
||||||
```
|
```
|
||||||
|
@ -15,24 +15,28 @@ public class StandardLib {
|
|||||||
public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
|
public static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
|
||||||
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy");
|
public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd. MM. yyyy");
|
||||||
|
|
||||||
|
public static Scope createScope() {
|
||||||
|
return addTo(new Scope());
|
||||||
|
}
|
||||||
|
|
||||||
public static Scope addTo(Scope scope) {
|
public static Scope addTo(Scope scope) {
|
||||||
return scope
|
return scope
|
||||||
.set("PI", of(Math.PI))
|
.set("PI", Math.PI)
|
||||||
.set("time", of(timeFormat.format(new Date())))
|
.set("time", timeFormat.format(new Date()))
|
||||||
.set("date", of(dateFormat.format(new Date())))
|
.set("date", dateFormat.format(new Date()))
|
||||||
|
|
||||||
.set("round", of(StandardLib::round))
|
.set("round", StandardLib::round)
|
||||||
.set("floor", of(StandardLib::floor))
|
.set("floor", StandardLib::floor)
|
||||||
.set("ceil", of(StandardLib::ceil))
|
.set("ceil", StandardLib::ceil)
|
||||||
.set("abs", of(StandardLib::abs))
|
.set("abs", StandardLib::abs)
|
||||||
.set("random", of(StandardLib::random))
|
.set("random", StandardLib::random)
|
||||||
|
|
||||||
.set("toUpper", of(StandardLib::toUpper))
|
.set("toUpper", StandardLib::toUpper)
|
||||||
.set("toLower", of(StandardLib::toLower))
|
.set("toLower", StandardLib::toLower)
|
||||||
.set("contains", of(StandardLib::contains))
|
.set("contains", StandardLib::contains)
|
||||||
.set("replace", of(StandardLib::replace))
|
.set("replace", StandardLib::replace)
|
||||||
|
|
||||||
.set("listOf", of(StandardLib::listOf));
|
.set("listOf", StandardLib::listOf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Numbers
|
// Numbers
|
||||||
|
@ -39,9 +39,9 @@ public class Closure extends DynamicExpr {
|
|||||||
Scope fork = dataRoot.fork();
|
Scope fork = dataRoot.fork();
|
||||||
for (int i = 0; i < ae; i++) fork.set(boundArgs.get(i), args.get(i));
|
for (int i = 0; i < ae; i++) fork.set(boundArgs.get(i), args.get(i));
|
||||||
if (variadic) {
|
if (variadic) {
|
||||||
fork.set(boundArgs.get(boundArgs.size() - 1), DFinal.of(IntStream.range(ae, ac)
|
fork.set(boundArgs.get(boundArgs.size() - 1), IntStream.range(ae, ac)
|
||||||
.mapToObj(args::get)
|
.<Dynamic<?>>mapToObj(args::get)
|
||||||
.toArray(Dynamic[]::new)));
|
.toList());
|
||||||
}
|
}
|
||||||
for (Expr<?> step : steps) {
|
for (Expr<?> step : steps) {
|
||||||
step.get(fork);
|
step.get(fork);
|
||||||
|
@ -3,8 +3,8 @@ package io.gitlab.jfronny.muscript.data;
|
|||||||
import io.gitlab.jfronny.commons.data.ImmCollection;
|
import io.gitlab.jfronny.commons.data.ImmCollection;
|
||||||
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
import io.gitlab.jfronny.muscript.data.dynamic.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class Scope implements DObject {
|
public class Scope implements DObject {
|
||||||
private final DObject source;
|
private final DObject source;
|
||||||
@ -25,11 +25,39 @@ public class Scope implements DObject {
|
|||||||
return ImmCollection.of(map);
|
return ImmCollection.of(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Using the convenience methods is recommended wherever possible.
|
||||||
|
*/
|
||||||
|
@Deprecated(forRemoval = false)
|
||||||
public Scope set(String key, Dynamic<?> value) {
|
public Scope set(String key, Dynamic<?> value) {
|
||||||
override.put(key, value);
|
override.put(key, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Scope set(String key, boolean value) {
|
||||||
|
return set(key, DFinal.of(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scope set(String key, double value) {
|
||||||
|
return set(key, DFinal.of(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scope set(String key, String value) {
|
||||||
|
return set(key, DFinal.of(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scope set(String key, Map<String, Dynamic<?>> value) {
|
||||||
|
return set(key, DFinal.of(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scope set(String key, List<Dynamic<?>> value) {
|
||||||
|
return set(key, DFinal.of(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Scope set(String key, Function<DList, Dynamic<?>> value) {
|
||||||
|
return set(key, DFinal.of(value));
|
||||||
|
}
|
||||||
|
|
||||||
public Scope fork() {
|
public Scope fork() {
|
||||||
return new Scope(this);
|
return new Scope(this);
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@ package io.gitlab.jfronny.muscript.test.util;
|
|||||||
|
|
||||||
import io.gitlab.jfronny.muscript.StandardLib;
|
import io.gitlab.jfronny.muscript.StandardLib;
|
||||||
import io.gitlab.jfronny.muscript.data.Scope;
|
import io.gitlab.jfronny.muscript.data.Scope;
|
||||||
import io.gitlab.jfronny.muscript.data.dynamic.DObject;
|
|
||||||
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
|
|
||||||
import io.gitlab.jfronny.muscript.compiler.Parser;
|
import io.gitlab.jfronny.muscript.compiler.Parser;
|
||||||
import io.gitlab.jfronny.muscript.ast.Expr;
|
import io.gitlab.jfronny.muscript.ast.Expr;
|
||||||
import io.gitlab.jfronny.muscript.debug.ObjectGraphPrinter;
|
import io.gitlab.jfronny.muscript.debug.ObjectGraphPrinter;
|
||||||
@ -36,25 +34,24 @@ public class MuTestUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Scope makeArgs() {
|
public static Scope makeArgs() {
|
||||||
return StandardLib.addTo(new Scope(of(Map.of(
|
return StandardLib.createScope()
|
||||||
"boolean", of(true),
|
.set("boolean", true)
|
||||||
"number", of(15),
|
.set("number", 15)
|
||||||
"string", of("Value"),
|
.set("string", "Value")
|
||||||
"object", of(Map.of(
|
.set("object", Map.of(
|
||||||
"subvalue", of(1024),
|
"subvalue", of(1024),
|
||||||
"subfunc", of(v -> of(v.get(1).asNumber().getValue() * v.size())),
|
"subfunc", of(v -> of(v.get(1).asNumber().getValue() * v.size())),
|
||||||
"1", of("One")
|
"1", of("One")
|
||||||
)),
|
))
|
||||||
"object2", of(Map.of(
|
.set("object2", Map.of(
|
||||||
"valuename", of("subvalue"),
|
"valuename", of("subvalue"),
|
||||||
"sub", of(Map.of(
|
"sub", of(Map.of(
|
||||||
"val", of(10)
|
"val", of(10)
|
||||||
)),
|
)),
|
||||||
"stringfunc", of(v -> of(v.get(0).asString().getValue()))
|
"stringfunc", of(v -> of(v.get(0).asString().getValue()))
|
||||||
)),
|
))
|
||||||
"list", of(of(true), of(2), of("3")),
|
.set("list", of(of(true), of(2), of("3")))
|
||||||
"function", of(v -> of(Math.pow(v.get(0).asNumber().getValue(), v.get(1).asNumber().getValue()))),
|
.set("function", v -> of(Math.pow(v.get(0).asNumber().getValue(), v.get(1).asNumber().getValue())))
|
||||||
"repeatArgs", of(v -> makeArgs())
|
.set("repeatArgs", v -> makeArgs());
|
||||||
))));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user