2022-06-03 19:54:31 +02:00
|
|
|
# μScript
|
2022-11-24 19:05:51 +01:00
|
|
|
|
2022-06-13 13:31:54 +02:00
|
|
|
μScript was created to allow respackopts pack authors to specify conditions for loading resources
|
2022-06-03 19:54:31 +02:00
|
|
|
in a more human-friendly manner than the previous json tree-based system.
|
2022-11-24 19:05:51 +01:00
|
|
|
|
2023-03-11 17:03:17 +01:00
|
|
|
It is intended to be vaguely like java in its syntax, though it deviates in some aspects.
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-03-11 17:03:17 +01:00
|
|
|
The language is parsed into an AST representation which is executed in-place, no compilation is performed.
|
|
|
|
μScript supports outputting data using various types, not just strings or booleans.
|
2022-06-03 19:54:31 +02:00
|
|
|
|
2023-03-11 17:03:17 +01:00
|
|
|
The purpose of this document is to be a guide on embedding μScript in your application or library.
|
2023-08-25 18:53:04 +02:00
|
|
|
|
2023-03-11 17:03:17 +01:00
|
|
|
For details on how to use the language, look [here](src/test/resources/example.md).
|
2022-06-13 13:31:54 +02:00
|
|
|
|
2023-08-25 18:53:04 +02:00
|
|
|
For details on the standard library provided by μScript, look [here](StandardLib.md).
|
|
|
|
|
2022-06-13 13:31:54 +02:00
|
|
|
## Embedding μScript
|
2022-11-24 19:05:51 +01:00
|
|
|
|
2023-01-20 21:52:25 +01:00
|
|
|
μScript is available as a [maven package](https://maven.frohnmeyer-wds.de/#/artifacts/io/gitlab/jfronny/muscript) which you can add to your
|
2022-11-24 19:05:51 +01:00
|
|
|
project.
|
2023-01-20 21:52:25 +01:00
|
|
|
To use it, first parse an expression via `Parser.parse(String script)` and convert the returned generic expression to a typed
|
|
|
|
one by calling `as(Bool|String|Number|Dynamic)Expr`.
|
2022-06-13 13:31:54 +02:00
|
|
|
This process may throw a ParseException.
|
2023-01-20 21:52:25 +01:00
|
|
|
You may also use `Parser.parseScript(String script)` for multi-expression scripts.
|
2023-01-20 22:05:18 +01:00
|
|
|
You can call `get(Dynamic<?> dataRoot)` on the result to execute the script on the provided data, which should be a
|
2023-01-24 14:05:04 +01:00
|
|
|
`Scope` which you created with `StandardLib.createScope()` to add standard methods.
|
2022-06-13 13:31:54 +02:00
|
|
|
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
|
|
|
|
using the source of the expression if available.
|
|
|
|
You may also call `StarScriptIngester.starScriptToMu()` to generate μScript code from StarScript code.
|
|
|
|
|
|
|
|
A full example could look as follows:
|
2022-11-24 19:05:51 +01:00
|
|
|
|
2022-06-13 13:31:54 +02:00
|
|
|
```java
|
2023-01-24 14:05:04 +01:00
|
|
|
public class Example {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
String source = String.join(" ", args);
|
|
|
|
Expr<?> parsed;
|
|
|
|
try {
|
|
|
|
parsed = Parser.parse(source); // or Parser.parse(StarScriptIngester.starScriptToMu(source))
|
|
|
|
} 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 {
|
2023-01-24 14:10:42 +01:00
|
|
|
result = typed.get(scope);
|
2023-01-24 14:05:04 +01:00
|
|
|
} catch (LocationalException e) {
|
|
|
|
System.err.println(e.asPrintable(source));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
System.out.println("Result: " + result);
|
|
|
|
}
|
2022-06-13 13:31:54 +02:00
|
|
|
}
|
|
|
|
```
|