Search in sources :

Example 21 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException 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

InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)21 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)12 FunctionContext (buildcraft.lib.expression.FunctionContext)8 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)6 INodeFunc (buildcraft.lib.expression.api.INodeFunc)6 INodeStack (buildcraft.lib.expression.api.INodeStack)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)5 NodeStack (buildcraft.lib.expression.NodeStack)4 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)3 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)3 ArrayList (java.util.ArrayList)3 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)2 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)2 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)2 JsonObject (com.google.gson.JsonObject)2 ResourceLoaderContext (buildcraft.lib.client.model.ResourceLoaderContext)1 IConstantNode (buildcraft.lib.expression.api.IConstantNode)1 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)1 NodeTypes (buildcraft.lib.expression.api.NodeTypes)1