Search in sources :

Example 11 with IExpressionNode

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;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) HashSet(java.util.HashSet)

Example 12 with IExpressionNode

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);
        }
    }
}
Also used : NodeStateful(buildcraft.lib.expression.node.value.NodeStateful) ITickableNode(buildcraft.lib.expression.node.value.ITickableNode) JsonObject(com.google.gson.JsonObject) IGetterFunc(buildcraft.lib.expression.node.value.NodeStateful.IGetterFunc) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeUpdatable(buildcraft.lib.expression.node.value.NodeUpdatable) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) JsonElement(com.google.gson.JsonElement) IConstantNode(buildcraft.lib.expression.api.IConstantNode) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Aggregations

IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)12 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)11 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)4 FunctionContext (buildcraft.lib.expression.FunctionContext)3 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)3 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)3 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 INodeFunc (buildcraft.lib.expression.api.INodeFunc)3 INodeStack (buildcraft.lib.expression.api.INodeStack)3 NodeStack (buildcraft.lib.expression.NodeStack)1 IConstantNode (buildcraft.lib.expression.api.IConstantNode)1 IVariableNode (buildcraft.lib.expression.api.IVariableNode)1 NodeCastLongToDouble (buildcraft.lib.expression.node.cast.NodeCastLongToDouble)1 NodeConditionalBoolean (buildcraft.lib.expression.node.condition.NodeConditionalBoolean)1 NodeConditionalDouble (buildcraft.lib.expression.node.condition.NodeConditionalDouble)1 NodeConditionalLong (buildcraft.lib.expression.node.condition.NodeConditionalLong)1 NodeConditionalObject (buildcraft.lib.expression.node.condition.NodeConditionalObject)1 NodeFuncGenericToBoolean (buildcraft.lib.expression.node.func.NodeFuncGenericToBoolean)1 NodeFuncGenericToDouble (buildcraft.lib.expression.node.func.NodeFuncGenericToDouble)1 NodeFuncGenericToLong (buildcraft.lib.expression.node.func.NodeFuncGenericToLong)1