Merge remote-tracking branch 'origin/asmparser' into asmparser

This commit is contained in:
dfsek 2021-02-12 14:29:50 -07:00
commit c4e641069d
4 changed files with 17 additions and 20 deletions

View File

@ -39,12 +39,7 @@ public class ExpressionSampler implements NoiseSampler {
});
for(Map.Entry<String, FunctionTemplate> entry : definedFunctions.entrySet()) {
String id = entry.getKey();
FunctionTemplate fun = entry.getValue();
Scope functionScope = new Scope().withParent(parent);
parser.registerFunction(id, new UserDefinedFunction(parser.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
parser.registerFunction(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), parser, parent));
}
this.expression = parser.parse(equation, scope);

View File

@ -1,14 +1,18 @@
package com.dfsek.terra.api.math.parsii.defined;
import com.dfsek.paralithic.Expression;
import com.dfsek.paralithic.eval.parser.Parser;
import com.dfsek.paralithic.eval.parser.Scope;
import com.dfsek.paralithic.eval.tokenizer.ParseException;
import com.dfsek.paralithic.functions.dynamic.DynamicFunction;
import com.dfsek.terra.config.loaders.config.function.FunctionTemplate;
public class UserDefinedFunction implements DynamicFunction {
private final Expression expression;
private final int args;
public UserDefinedFunction(Expression expression, int args) {
protected UserDefinedFunction(Expression expression, int args) {
this.expression = expression;
this.args = args;
}
@ -28,4 +32,13 @@ public class UserDefinedFunction implements DynamicFunction {
public int getArgNumber() {
return args;
}
public static UserDefinedFunction newInstance(FunctionTemplate template, Parser parser, Scope parent) throws ParseException {
Scope functionScope = new Scope().withParent(parent);
template.getArgs().forEach(functionScope::addInvocationVariable);
return new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
}
}

View File

@ -70,12 +70,7 @@ public class UserDefinedCarver extends Carver {
});
for(Map.Entry<String, FunctionTemplate> entry : definedFunctions.entrySet()) {
String id = entry.getKey();
FunctionTemplate fun = entry.getValue();
Scope functionScope = new Scope().withParent(parent);
p.registerFunction(id, new UserDefinedFunction(p.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
p.registerFunction(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), p, parent));
}
Scope s = new Scope().withParent(parent);

View File

@ -354,14 +354,8 @@ public class BiomeTemplate extends AbstractableTemplate implements ValidatedConf
Map<String, FunctionTemplate> testFunctions = new LinkedHashMap<>(pack.getTemplate().getFunctions());
testFunctions.putAll(functions);
for(Map.Entry<String, FunctionTemplate> entry : testFunctions.entrySet()) {
String id = entry.getKey();
FunctionTemplate fun = entry.getValue();
Scope functionScope = new Scope().withParent(testScope);
fun.getArgs().forEach(functionScope::addInvocationVariable);
try {
tester.registerFunction(id, new UserDefinedFunction(tester.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
tester.registerFunction(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), tester, testScope));
} catch(ParseException e) {
throw new ValidationException("Invalid function: ", e);
}