use of buildcraft.lib.expression.api.IExpressionNode in project BuildCraft by BuildCraft.
the class ElementType method createContext.
public static FunctionContext createContext(JsonGuiElement json) {
FunctionContext ctx = json.context;
// if json overrides variables then its ok
ctx = new FunctionContext(ctx);
Set<String> args = new HashSet<>();
// Put in args first
for (String key : json.properties.keySet()) {
if (key.startsWith("args.")) {
String argName = key.substring("args.".length());
args.add(argName);
String value = json.properties.get(key);
try {
IExpressionNode node = InternalCompiler.compileExpression(value, ctx);
ctx.putVariable(argName, NodeTypes.createConstantNode(node));
} catch (InvalidExpressionException e) {
// Ignore the error
BCLog.logger.info("Failed to compile expression for " + key + " because " + e.getMessage());
}
}
}
for (String key : json.properties.keySet()) {
if (key.contains(".") || key.contains("[")) {
continue;
}
String value = json.properties.get(key);
try {
IExpressionNode node = InternalCompiler.compileExpression(value, ctx);
ctx.putVariable(key, NodeTypes.createConstantNode(node));
} catch (InvalidExpressionException e) {
// Ignore the error
// BCLog.logger.info("Failed to compile expression for " + key + " because " + e.getMessage());
}
}
return ctx;
}
use of buildcraft.lib.expression.api.IExpressionNode in project BuildCraft by BuildCraft.
the class JsonVariableObject method putVariables.
protected void putVariables(JsonObject values, FunctionContext fnCtx) {
for (Entry<String, JsonElement> entry : values.entrySet()) {
String name = entry.getKey();
name = name.toLowerCase(Locale.ROOT);
if (fnCtx.hasLocalVariable(name)) {
throw new JsonSyntaxException("Duplicate local variable '" + name + "'");
} else if (fnCtx.getVariable(name) != null) {
// ...what? Doesn't this disallow overriding existing variables?
continue;
}
JsonElement value = entry.getValue();
String type = null, getter = null, rounder = null;
if (value.isJsonObject()) {
JsonObject objValue = value.getAsJsonObject();
value = objValue.get("value");
type = JsonUtils.getString(objValue, "type");
getter = JsonUtils.getString(objValue, "getter");
if (objValue.has("rounder")) {
rounder = JsonUtils.getString(objValue, "rounder");
}
}
if (!value.isJsonPrimitive()) {
throw new JsonSyntaxException("Expected a primitive, got " + value + " for the variable '" + name + "'");
}
NodeStateful stateful = null;
FunctionContext fnCtxValue = new FunctionContext("Value Object", fnCtx);
if (getter != null) {
// stateful node
Class<?> nodeType;
try {
nodeType = NodeTypes.parseType(type);
} catch (InvalidExpressionException iee) {
throw new JsonSyntaxException("Could not parse node type for variable '" + name + "'", iee);
}
IGetterFunc getterFunc = parseGetterFunction(getter, fnCtx);
try {
stateful = new NodeStateful(name, nodeType, getterFunc);
} catch (InvalidExpressionException iee) {
throw new JsonSyntaxException("Could not create a getter for the variable '" + name + "'", iee);
}
fnCtx.putVariable(name, stateful.getter);
fnCtxValue.putVariable(name, stateful.variable);
if (rounder != null) {
FunctionContext fnCtx2 = new FunctionContext("Rounding", fnCtx);
fnCtx2.putVariable("last", stateful.last);
fnCtx2.putVariable("current", stateful.variable);
fnCtx2.putVariable("value", stateful.rounderValue);
try {
IExpressionNode nodeRounder = InternalCompiler.compileExpression(rounder, fnCtx2);
stateful.setRounder(nodeRounder);
} catch (InvalidExpressionException iee) {
throw new JsonSyntaxException("Could not compile a rounder for the variable '" + name + "'", iee);
}
}
}
String expression = value.getAsString();
IExpressionNode node;
try {
node = InternalCompiler.compileExpression(expression, fnCtxValue);
} catch (InvalidExpressionException e) {
throw new JsonSyntaxException("Failed to compile variable " + name, e);
}
if (node instanceof IConstantNode) {
// No point in adding it to variables
fnCtx.putVariable(name, node);
continue;
}
if (variables.containsKey(name)) {
ITickableNode.Source existing = variables.get(name);
existing.setSource(node);
} else if (stateful != null) {
stateful.setSource(node);
variables.put(name, stateful);
} else {
NodeUpdatable nodeUpdatable = new NodeUpdatable(name, node);
variables.put(name, nodeUpdatable);
fnCtx.putVariable(name, nodeUpdatable.variable);
}
}
}
Aggregations