fix user defined functions

This commit is contained in:
dfsek
2021-02-11 08:35:13 -07:00
parent 7323b051db
commit e41587dbd9
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()) { for(Map.Entry<String, FunctionTemplate> entry : definedFunctions.entrySet()) {
String id = entry.getKey(); parser.registerFunction(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), parser, parent));
FunctionTemplate fun = entry.getValue();
Scope functionScope = new Scope().withParent(parent);
parser.registerFunction(id, new UserDefinedFunction(parser.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
} }
this.expression = parser.parse(equation, scope); this.expression = parser.parse(equation, scope);

View File

@@ -1,14 +1,18 @@
package com.dfsek.terra.api.math.parsii.defined; package com.dfsek.terra.api.math.parsii.defined;
import com.dfsek.paralithic.Expression; 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.paralithic.functions.dynamic.DynamicFunction;
import com.dfsek.terra.config.loaders.config.function.FunctionTemplate;
public class UserDefinedFunction implements DynamicFunction { public class UserDefinedFunction implements DynamicFunction {
private final Expression expression; private final Expression expression;
private final int args; private final int args;
public UserDefinedFunction(Expression expression, int args) { protected UserDefinedFunction(Expression expression, int args) {
this.expression = expression; this.expression = expression;
this.args = args; this.args = args;
} }
@@ -28,4 +32,13 @@ public class UserDefinedFunction implements DynamicFunction {
public int getArgNumber() { public int getArgNumber() {
return args; 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()) { for(Map.Entry<String, FunctionTemplate> entry : definedFunctions.entrySet()) {
String id = entry.getKey(); p.registerFunction(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), p, parent));
FunctionTemplate fun = entry.getValue();
Scope functionScope = new Scope().withParent(parent);
p.registerFunction(id, new UserDefinedFunction(p.parse(fun.getFunction(), functionScope), fun.getArgs().size()));
} }
Scope s = new Scope().withParent(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()); Map<String, FunctionTemplate> testFunctions = new LinkedHashMap<>(pack.getTemplate().getFunctions());
testFunctions.putAll(functions); testFunctions.putAll(functions);
for(Map.Entry<String, FunctionTemplate> entry : testFunctions.entrySet()) { 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 { 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) { } catch(ParseException e) {
throw new ValidationException("Invalid function: ", e); throw new ValidationException("Invalid function: ", e);
} }