59 lines
2.2 KiB
Java
59 lines
2.2 KiB
Java
package io.gitlab.jfronny.muscript.test.util;
|
|
|
|
import io.gitlab.jfronny.muscript.StandardLib;
|
|
import io.gitlab.jfronny.muscript.data.Scope;
|
|
import io.gitlab.jfronny.muscript.compiler.Parser;
|
|
import io.gitlab.jfronny.muscript.ast.Expr;
|
|
import io.gitlab.jfronny.muscript.debug.ObjectGraphPrinter;
|
|
|
|
import java.util.Map;
|
|
|
|
import static io.gitlab.jfronny.muscript.data.dynamic.additional.DFinal.of;
|
|
|
|
public class MuTestUtil {
|
|
public static double number(String source) {
|
|
return Parser.parse(source).asNumberExpr().get(makeArgs());
|
|
}
|
|
|
|
public static boolean bool(String source) {
|
|
Expr<?> tree = Parser.parse(source);
|
|
try {
|
|
return tree.asBoolExpr().get(makeArgs());
|
|
} catch (RuntimeException e) {
|
|
try {
|
|
System.out.println("Caught error with tree:\n" + ObjectGraphPrinter.printGraph(tree));
|
|
} catch (IllegalAccessException ex) {
|
|
throw new RuntimeException(ex);
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
public static String string(String source) {
|
|
return Parser.parse(source).asStringExpr().get(makeArgs());
|
|
}
|
|
|
|
public static Scope makeArgs() {
|
|
return StandardLib.createScope()
|
|
.set("boolean", true)
|
|
.set("number", 15)
|
|
.set("string", "Value")
|
|
.set("object", Map.of(
|
|
"subvalue", of(1024),
|
|
"subfunc", of(v -> of(v.get(1).asNumber().getValue() * v.size()), () -> "object.subfunc"),
|
|
"1", of("One")
|
|
))
|
|
.set("object2", Map.of(
|
|
"valuename", of("subvalue"),
|
|
"sub", of(Map.of(
|
|
"val", of(10)
|
|
)),
|
|
"stringfunc", of(v -> of(v.get(0).asString().getValue()), () -> "object2.stringfunc")
|
|
))
|
|
.set("list", of(of(true), of(2), of("3")))
|
|
.set("numbers", of(of(1), of(2), of(3), of(4), of(5), of(6), of(7), of(8), of(9), of(10)))
|
|
.set("function", v -> of(Math.pow(v.get(0).asNumber().getValue(), v.get(1).asNumber().getValue())))
|
|
.set("repeatArgs", v -> makeArgs());
|
|
}
|
|
}
|