Add support for Paralithic let expressions

This commit is contained in:
Astrash
2024-10-30 12:34:47 +11:00
parent 02fdcee705
commit 5e43f0afef
18 changed files with 131 additions and 24 deletions
@@ -2,6 +2,7 @@ package com.dfsek.terra.addons.numberpredicate;
import com.dfsek.paralithic.Expression;
import com.dfsek.paralithic.eval.parser.Parser;
import com.dfsek.paralithic.eval.parser.Parser.ParseOptions;
import com.dfsek.paralithic.eval.parser.Scope;
import com.dfsek.paralithic.eval.tokenizer.ParseException;
import com.dfsek.tectonic.api.depth.DepthTracker;
@@ -15,6 +16,13 @@ import java.util.function.DoublePredicate;
public class DoublePredicateLoader implements TypeLoader<DoublePredicate> {
private final ParseOptions parseOptions;
public DoublePredicateLoader(ParseOptions parseOptions) {
this.parseOptions = parseOptions;
}
@Override
public DoublePredicate load(@NotNull AnnotatedType annotatedType, @NotNull Object o, @NotNull ConfigLoader configLoader,
DepthTracker depthTracker) throws LoadException {
@@ -22,7 +30,7 @@ public class DoublePredicateLoader implements TypeLoader<DoublePredicate> {
Scope scope = new Scope();
scope.addInvocationVariable("value");
try {
Expression expression = new Parser().parse(expressionString, scope);
Expression expression = new Parser(parseOptions).parse(expressionString, scope);
return d -> expression.evaluate(d) != 0; // Paralithic expressions treat '!= 0' as true
} catch(ParseException e) {
throw new LoadException("Failed to parse double predicate expression", e, depthTracker);
@@ -30,7 +30,7 @@ public class NumberPredicateAddon implements AddonInitializer {
plugin.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.then(event -> event.getPack().applyLoader(DoublePredicate.class, new DoublePredicateLoader()))
.then(event -> event.getPack().applyLoader(DoublePredicate.class, new DoublePredicateLoader(event.getPack().getExpressionParseOptions())))
.priority(50)
.failThrough();
}