nested user defined functions

This commit is contained in:
dfsek 2021-12-04 19:34:24 -07:00
parent 4083597f23
commit 1eb515e751
3 changed files with 23 additions and 3 deletions

View File

@ -7,9 +7,11 @@
package com.dfsek.terra.addons.noise.config.templates; package com.dfsek.terra.addons.noise.config.templates;
import com.dfsek.tectonic.annotations.Default;
import com.dfsek.tectonic.annotations.Value; import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.loading.object.ObjectTemplate; import com.dfsek.tectonic.loading.object.ObjectTemplate;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import com.dfsek.terra.api.config.meta.Meta; import com.dfsek.terra.api.config.meta.Meta;
@ -23,6 +25,10 @@ public class FunctionTemplate implements ObjectTemplate<FunctionTemplate> {
@Value("expression") @Value("expression")
private @Meta String function; private @Meta String function;
@Value("functions")
@Default
private @Meta LinkedHashMap<String, @Meta FunctionTemplate> functions = new LinkedHashMap<>();
@Override @Override
public FunctionTemplate get() { public FunctionTemplate get() {
return this; return this;
@ -35,4 +41,8 @@ public class FunctionTemplate implements ObjectTemplate<FunctionTemplate> {
public String getFunction() { public String getFunction() {
return function; return function;
} }
public LinkedHashMap<String, FunctionTemplate> getFunctions() {
return functions;
}
} }

View File

@ -76,11 +76,11 @@ public class ExpressionFunctionTemplate extends SamplerTemplate<ExpressionFuncti
Map<String, Function> noiseFunctionMap = new HashMap<>(); Map<String, Function> noiseFunctionMap = new HashMap<>();
for(Map.Entry<String, FunctionTemplate> entry : globalFunctions.entrySet()) { for(Map.Entry<String, FunctionTemplate> entry : globalFunctions.entrySet()) {
noiseFunctionMap.put(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), new Parser(), new Scope())); noiseFunctionMap.put(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue()));
} }
for(Map.Entry<String, FunctionTemplate> entry : functions.entrySet()) { for(Map.Entry<String, FunctionTemplate> entry : functions.entrySet()) {
noiseFunctionMap.put(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue(), new Parser(), new Scope())); noiseFunctionMap.put(entry.getKey(), UserDefinedFunction.newInstance(entry.getValue()));
} }
otherFunctions.forEach((id, function) -> { otherFunctions.forEach((id, function) -> {

View File

@ -17,6 +17,8 @@ import com.dfsek.paralithic.node.Statefulness;
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate; import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
import java.util.Map.Entry;
public class UserDefinedFunction implements DynamicFunction { public class UserDefinedFunction implements DynamicFunction {
private final Expression expression; private final Expression expression;
@ -27,12 +29,20 @@ public class UserDefinedFunction implements DynamicFunction {
this.args = args; this.args = args;
} }
public static UserDefinedFunction newInstance(FunctionTemplate template, Parser parser, Scope parent) throws ParseException { public static UserDefinedFunction newInstance(FunctionTemplate template) throws ParseException {
Parser parser = new Parser();
Scope parent = new Scope();
Scope functionScope = new Scope().withParent(parent); Scope functionScope = new Scope().withParent(parent);
template.getArgs().forEach(functionScope::addInvocationVariable); template.getArgs().forEach(functionScope::addInvocationVariable);
for(Entry<String, FunctionTemplate> entry : template.getFunctions().entrySet()) {
String id = entry.getKey();
FunctionTemplate nest = entry.getValue();
parser.registerFunction(id, newInstance(nest));
}
return new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size()); return new UserDefinedFunction(parser.parse(template.getFunction(), functionScope), template.getArgs().size());
} }