Update muScript, brings stricter error checking

This commit is contained in:
Johannes Frohnmeyer 2023-02-23 18:14:25 +01:00
parent 7ef0001ece
commit c84ef9e15a
Signed by: Johannes
GPG Key ID: E76429612C2929F4
22 changed files with 65 additions and 67 deletions

View File

@ -38,7 +38,6 @@ dependencies {
// clientRuntimeOnly("dev.quantumfusion.dashloader:dashloader-definitions:3.0-rc14-1.18")
}
//TODO re-enable once minecraft classes can be used in tests again
//test {
// useJUnitPlatform()
//}
tasks.test {
useJUnitPlatform()
}

View File

@ -15,4 +15,4 @@ curseforge_optional_dependencies=modmenu
fabric_version=0.70.0+1.19.3
libjf_version=3.4.1
muscript_version=1.0-SNAPSHOT
muscript_version=1.1-SNAPSHOT

View File

@ -1,5 +1,5 @@
{
"conditions": "lumi.subcategoryTest.enableLang !subcategoryTest.enableLangForceDisable & version('respackopts', '*')",
"conditions": "lumi.subcategoryTest.enableLang & (!subcategoryTest.enableLangForceDisable) & version('respackopts', '*')",
"fallback": "assets/minecraft/lang/en_us_joke.json",
"expansions": {
"Lights": "lumi.subcategoryTest.enableLang",

View File

@ -4,8 +4,9 @@ import io.gitlab.jfronny.commons.log.Logger;
import io.gitlab.jfronny.gson.Gson;
import io.gitlab.jfronny.gson.GsonBuilder;
import io.gitlab.jfronny.libjf.config.api.v1.ConfigHolder;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.respackopts.filters.*;
import io.gitlab.jfronny.muscript.ast.*;
import io.gitlab.jfronny.respackopts.filters.DirFilterEvents;
import io.gitlab.jfronny.respackopts.filters.FileFilterEvents;
import io.gitlab.jfronny.respackopts.gson.*;
import io.gitlab.jfronny.respackopts.gson.entry.*;
import io.gitlab.jfronny.respackopts.integration.SaveHook;
@ -15,8 +16,6 @@ import io.gitlab.jfronny.respackopts.server.ServerInstanceHolder;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.resource.InputSupplier;
import net.minecraft.util.Util;
import java.io.IOException;
import java.nio.file.Files;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.respackopts.filters.util;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.StringExpr;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.ast.StringExpr;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.RespackoptsConfig;
import io.gitlab.jfronny.respackopts.util.MetaCache;

View File

@ -2,11 +2,11 @@ package io.gitlab.jfronny.respackopts.gson;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.gson.reflect.*;
import io.gitlab.jfronny.muscript.ast.compare.Equal;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.compiler.expr.bool.*;
import io.gitlab.jfronny.muscript.compiler.expr.common.*;
import io.gitlab.jfronny.muscript.compiler.expr.dynamic.*;
import io.gitlab.jfronny.muscript.ast.*;
import io.gitlab.jfronny.muscript.ast.bool.*;
import io.gitlab.jfronny.muscript.ast.dynamic.*;
import java.lang.reflect.Type;
import java.util.*;
@ -29,7 +29,7 @@ public class BoolExprDeserializer implements JsonDeserializer<BoolExpr> {
return switch (entry.getKey().toLowerCase(Locale.ROOT)) {
case "and", "add", "&" -> merge(context.deserialize(entry.getValue(), conditionListType), Token.And);
case "==", "=", "equal", "eq" -> merge(context.deserialize(entry.getValue(), conditionListType), Token.EqualEqual);
case "not", "nor", "!" -> new Not(0, merge(context.deserialize(entry.getValue(), conditionListType), Token.Or));
case "not", "nor", "!" -> new Not(-1, -1, merge(context.deserialize(entry.getValue(), conditionListType), Token.Or));
case "or", "|" -> merge(context.deserialize(entry.getValue(), conditionListType), Token.Or);
case "^", "xor" -> merge(context.deserialize(entry.getValue(), conditionListType), Token.BangEqual);
default -> throw new JsonParseException("Unknown condition type: " + entry.getKey());
@ -44,13 +44,13 @@ public class BoolExprDeserializer implements JsonDeserializer<BoolExpr> {
if (pr.isString()) {
String name = pr.getAsString();
if (name.toLowerCase(Locale.ROOT).equals("true"))
return Expr.literal(0, true);
return Expr.literal(-1, -1, true);
if (name.toLowerCase(Locale.ROOT).equals("false"))
return Expr.literal(0, false);
return Expr.literal(-1, -1, false);
return rpoBooleanCondition(name);
}
else if (pr.isBoolean()) {
return Expr.literal(0, pr.getAsBoolean());
return Expr.literal(-1, -1, pr.getAsBoolean());
}
}
throw new JsonParseException("Invalid data type for condition");
@ -58,13 +58,13 @@ public class BoolExprDeserializer implements JsonDeserializer<BoolExpr> {
private BoolExpr merge(List<BoolExpr> expressions, Token token) {
if (token == Token.BangEqual)
return new Not(0, merge(expressions, Token.EqualEqual));
return new Not(-1, -1, merge(expressions, Token.EqualEqual));
BoolExpr current = expressions.get(0);
for (BoolExpr expr : expressions.subList(1, expressions.size())) {
current = switch (token) {
case EqualEqual -> new Equal(0, current, expr);
case And -> new And(0, current, expr);
case Or -> new Or(0, current, expr);
case EqualEqual -> new Equal(-1, -1, current, expr);
case And -> new And(-1, -1, current, expr);
case Or -> new Or(-1, -1, current, expr);
default -> throw new IllegalArgumentException();
};
}
@ -76,16 +76,16 @@ public class BoolExprDeserializer implements JsonDeserializer<BoolExpr> {
String code = name.substring("modversion:".length());
String mod = code.substring(0, code.indexOf(':'));
String predicate = code.substring(code.indexOf(':') + 1);
return new Call(0, new Variable(0, "version"), List.of(
Expr.literal(0, mod).asDynamicExpr(),
Expr.literal(0, predicate).asDynamicExpr()
return new Call(-1, -1, new Variable(-1, -1, "version"), List.of(
new Call.Arg(Expr.literal(-1, -1, mod).asDynamicExpr(), false),
new Call.Arg(Expr.literal(-1, -1, predicate).asDynamicExpr(), false)
)).asBoolExpr();
}
DynamicExpr e = null;
String[] arr = name.split("[:.]");
for (int i = 0; i < arr.length; i++) {
if (i == 0) e = new Variable(0, arr[i]);
else e = new Get(0, e, Expr.literal(0, arr[i]));
if (i == 0) e = new Variable(-1, -1, arr[i]);
else e = new Get(-1, -1, e, Expr.literal(arr[i]));
}
if (e == null) throw new JsonParseException("Invalid RPO condition: \"" + name + "\"");
return e.asBoolExpr();

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.respackopts.gson;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.ast.*;
import io.gitlab.jfronny.respackopts.model.*;
import java.lang.reflect.*;

View File

@ -3,7 +3,7 @@ package io.gitlab.jfronny.respackopts.gson;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.muscript.*;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.ast.*;
import java.lang.reflect.Type;
import java.util.*;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.respackopts.gson;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.ast.*;
import java.lang.reflect.*;

View File

@ -1,11 +1,12 @@
package io.gitlab.jfronny.respackopts.model;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.error.*;
import io.gitlab.jfronny.muscript.ast.BoolExpr;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.muscript.error.LocationalError;
import io.gitlab.jfronny.muscript.error.LocationalException;
public record Condition(String source, BoolExpr expr) {
public boolean get(Dynamic<?> dataRoot) throws ConditionException {
public boolean get(Scope dataRoot) throws ConditionException {
try {
return expr.get(dataRoot);
} catch (LocationalException e) {

View File

@ -2,7 +2,7 @@ package io.gitlab.jfronny.respackopts.model;
import io.gitlab.jfronny.commons.serialize.gson.api.*;
import io.gitlab.jfronny.gson.annotations.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.ast.*;
import java.util.*;

View File

@ -1,9 +1,7 @@
package io.gitlab.jfronny.respackopts.model.cache;
import io.gitlab.jfronny.muscript.ExpressionParameter;
import io.gitlab.jfronny.respackopts.model.DirRpo;
import io.gitlab.jfronny.respackopts.model.FileRpo;
import io.gitlab.jfronny.respackopts.model.PackMeta;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.respackopts.model.*;
import io.gitlab.jfronny.respackopts.model.tree.ConfigBranch;
import java.util.HashMap;
@ -17,7 +15,7 @@ public record CachedPackState(
PackMeta metadata,
Map<String, FileRpo> cachedFileRPOs,
Map<String, DirRpo> cachedDirRPOs,
ExpressionParameter expressionParameter
Scope executionScope
) {
public CachedPackState(CacheKey key, PackMeta meta, ConfigBranch branch) {
this(meta.id,
@ -27,6 +25,6 @@ public record CachedPackState(
meta,
new HashMap<>(),
new HashMap<>(),
new ExpressionParameter(new HashMap<>(branch.getDynamic().getValue())));
new Scope(branch.getDynamic()));
}
}

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.respackopts.model.tree;
import io.gitlab.jfronny.commons.data.dynamic.DBool;
import io.gitlab.jfronny.muscript.data.dynamic.DBool;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.RespackoptsConfig;

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.respackopts.model.tree;
import com.google.common.collect.ImmutableMap;
import io.gitlab.jfronny.commons.data.dynamic.*;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import io.gitlab.jfronny.gson.reflect.TypeToken;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.ConfigBuilder;

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.respackopts.model.tree;
import io.gitlab.jfronny.commons.data.dynamic.Dynamic;
import io.gitlab.jfronny.muscript.data.dynamic.Dynamic;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.model.enums.ConfigSyncMode;

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.respackopts.model.tree;
import io.gitlab.jfronny.commons.data.dynamic.DEnum;
import io.gitlab.jfronny.muscript.data.dynamic.DEnum;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.RespackoptsConfig;

View File

@ -1,6 +1,6 @@
package io.gitlab.jfronny.respackopts.model.tree;
import io.gitlab.jfronny.commons.data.dynamic.DNumber;
import io.gitlab.jfronny.muscript.data.dynamic.DNumber;
import io.gitlab.jfronny.libjf.config.api.v1.dsl.CategoryBuilder;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.RespackoptsConfig;

View File

@ -1,10 +1,10 @@
package io.gitlab.jfronny.respackopts.util;
import io.gitlab.jfronny.commons.throwable.ThrowingBiConsumer;
import io.gitlab.jfronny.muscript.ExpressionParameter;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.respackopts.Respackopts;
import io.gitlab.jfronny.respackopts.integration.SaveHook;
import io.gitlab.jfronny.respackopts.RespackoptsConfig;
import io.gitlab.jfronny.respackopts.integration.SaveHook;
import io.gitlab.jfronny.respackopts.model.PackMeta;
import io.gitlab.jfronny.respackopts.model.cache.CacheKey;
import io.gitlab.jfronny.respackopts.model.cache.CachedPackState;
@ -150,8 +150,8 @@ public class MetaCache {
return PACK_STATES.get(key);
}
public static ExpressionParameter getParameter(@Nullable CacheKey key) {
ExpressionParameter parameter = key == null ? new ExpressionParameter() : MetaCache.getState(key).expressionParameter();
public static Scope getParameter(@Nullable CacheKey key) {
Scope parameter = key == null ? new Scope() : MetaCache.getState(key).executionScope();
MetaCache.forEach((id, state) -> {
String packId = Respackopts.sanitizeString(state.packId());
if (!parameter.has(packId))

View File

@ -1,8 +1,8 @@
package io.gitlab.jfronny.respackopts.util;
import io.gitlab.jfronny.commons.data.dynamic.*;
import io.gitlab.jfronny.muscript.ExpressionParameter;
import io.gitlab.jfronny.muscript.data.dynamic.*;
import io.gitlab.jfronny.muscript.StandardLib;
import io.gitlab.jfronny.muscript.data.Scope;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.VersionParsingException;
import net.fabricmc.loader.api.metadata.version.VersionPredicate;
@ -13,9 +13,9 @@ import java.util.Map;
import java.util.function.Supplier;
public class MuUtils {
public static ExpressionParameter addDefault(ExpressionParameter parameter) {
public static Scope addDefault(Scope parameter) {
StandardLib.addTo(parameter);
parameter.set("version", DFinal.of(args -> {
parameter.set("version", args -> {
if (args.size() != 2) throw new IllegalArgumentException("Expected 2 arguments on version but got " + args.size());
VersionPredicate predicate;
try {
@ -26,7 +26,7 @@ public class MuUtils {
return DFinal.of(FabricLoader.getInstance().getModContainer(args.get(0).asString().getValue())
.map(c -> predicate.test(c.getMetadata().getVersion()))
.orElse(false));
}));
});
parameter.set("date", new DCallableObject(Map.of(
"today", new DDate(LocalDate::now)
), DFinal.of(args -> {

View File

@ -1,8 +1,8 @@
package io.gitlab.jfronny.respackopts.test;
import io.gitlab.jfronny.gson.reflect.TypeToken;
import io.gitlab.jfronny.muscript.ExpressionParameter;
import io.gitlab.jfronny.muscript.compiler.expr.BoolExpr;
import io.gitlab.jfronny.muscript.ast.BoolExpr;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.respackopts.gson.AttachmentHolder;
import io.gitlab.jfronny.respackopts.util.MuUtils;
import org.junit.jupiter.api.Test;
@ -95,6 +95,6 @@ class ConditionJsonSerializationTest {
private boolean evaluateCondition(String json) {
BoolExpr condition = AttachmentHolder.attach(7, () -> GSON.fromJson(json, BoolExpr.class));
return condition.get(MuUtils.addDefault(new ExpressionParameter()));
return condition.get(MuUtils.addDefault(new Scope()));
}
}

View File

@ -1,16 +1,17 @@
package io.gitlab.jfronny.respackopts.test;
import io.gitlab.jfronny.muscript.*;
import io.gitlab.jfronny.muscript.compiler.*;
import io.gitlab.jfronny.respackopts.util.*;
import io.gitlab.jfronny.muscript.compiler.Parser;
import io.gitlab.jfronny.muscript.data.Scope;
import io.gitlab.jfronny.respackopts.util.MuUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TemporalExpressionTest {
@Test
void componentTest() {
ExpressionParameter param = MuUtils.addDefault(new ExpressionParameter());
Scope param = MuUtils.addDefault(new Scope());
assertEquals(10, Parser.parse("date(2020, 5, 10).day").asNumberExpr().get(param));
assertEquals(5, Parser.parse("date(2020, 5, 10).month").asNumberExpr().get(param));
@ -23,7 +24,7 @@ public class TemporalExpressionTest {
@Test
void compareTest() {
ExpressionParameter param = MuUtils.addDefault(new ExpressionParameter());
Scope param = MuUtils.addDefault(new Scope());
assertTrue(Parser.parse("date(2020, 5, 10) > date(2020, 5, 9)").asBoolExpr().get(param));
assertTrue(Parser.parse("date(2020, 5, 10) > date(2020, 4, 10)").asBoolExpr().get(param));

View File

@ -1,7 +1,7 @@
package io.gitlab.jfronny.respackopts.test;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.muscript.compiler.expr.*;
import io.gitlab.jfronny.muscript.ast.*;
import io.gitlab.jfronny.respackopts.gson.*;
import io.gitlab.jfronny.respackopts.gson.entry.*;
import io.gitlab.jfronny.respackopts.model.Condition;