Search in sources :

Example 6 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class ElementType method resolvePosition.

public static IGuiPosition resolvePosition(JsonGuiElement json, String name, IGuiPosition parent, FunctionContext ctx) {
    String eqn = json.properties.get(name);
    if (eqn == null) {
        INodeDouble x = getEquationDouble(json, name + "[0]", ctx);
        INodeDouble y = getEquationDouble(json, name + "[1]", ctx);
        return IGuiPosition.create(x, y).offset(parent);
    }
    try {
        return GenericExpressionCompiler.compileExpressionObject(IGuiPosition.class, eqn, ctx).evaluate();
    } catch (InvalidExpressionException e) {
        throw new JsonSyntaxException("Failed to resolve a position for " + json.fullName, e);
    }
}
Also used : JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

Example 7 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class ElementType method resolveArea.

public static IGuiArea resolveArea(JsonGuiElement json, String name, IGuiPosition parent, FunctionContext ctx) {
    String eqn = json.properties.get(name);
    if (eqn == null) {
        INodeDouble x = getEquationDouble(json, name + "[0]", ctx);
        INodeDouble y = getEquationDouble(json, name + "[1]", ctx);
        INodeDouble w = getEquationDouble(json, name + "[2]", ctx);
        INodeDouble h = getEquationDouble(json, name + "[3]", ctx);
        return IGuiArea.create(x, y, w, h).offset(parent);
    }
    try {
        return GenericExpressionCompiler.compileExpressionObject(IGuiArea.class, eqn, ctx).evaluate();
    } catch (InvalidExpressionException e) {
        throw new JsonSyntaxException("Failed to resolve an area for " + json.fullName, e);
    }
}
Also used : IGuiArea(buildcraft.lib.gui.pos.IGuiArea) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble)

Example 8 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class NodeCasting method castToType.

public static IExpressionNode castToType(IExpressionNode node, Class<?> to) throws InvalidExpressionException {
    Class<?> from = NodeTypes.getType(node);
    if (from == to) {
        return node;
    }
    FunctionContext castingContext = new FunctionContext(NodeTypes.getContext(from), NodeTypes.getContext(to));
    INodeFunc caster = castingContext.getFunction("(" + NodeTypes.getName(to) + ")", Collections.singletonList(from));
    if (caster == null) {
        if (to == String.class) {
            return new NodeCastToString(node);
        }
        throw new InvalidExpressionException("Cannot cast from " + NodeTypes.getName(from) + " to " + NodeTypes.getName(to));
    }
    NodeStack stack = new NodeStack(node);
    stack.setRecorder(Collections.singletonList(from), caster);
    IExpressionNode casted = caster.getNode(stack);
    stack.checkAndRemoveRecorder();
    Class<?> actual = NodeTypes.getType(casted);
    if (actual != to) {
        throw new IllegalStateException("The caster " + caster + " didn't produce the correct result! (Expected " + to + ", but got " + actual + ")");
    }
    return casted;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 9 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class NodeCasting method castToDouble.

public static INodeDouble castToDouble(IExpressionNode node) throws InvalidExpressionException {
    if (node instanceof INodeDouble) {
        return (INodeDouble) node;
    }
    Class<?> type = NodeTypes.getType(node);
    FunctionContext ctx = NodeTypes.getContext(type);
    if (ctx == null) {
        throw new InvalidExpressionException("Cannot cast " + node + " to a double!");
    }
    INodeFunc func = ctx.getFunction("(double)", Collections.singletonList(type));
    if (func == null || NodeTypes.getType(func) != double.class) {
        throw new InvalidExpressionException("Cannot cast " + node + " to a double!");
    }
    return (INodeDouble) func.getNode(new NodeStack(node));
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 10 with InvalidExpressionException

use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.

the class NodeCasting method castToObject.

public static <T> INodeFuncObject<T> castToObject(INodeFunc func, Class<T> to) throws InvalidExpressionException {
    Class<?> from = NodeTypes.getType(func);
    if (from == to) {
        return (INodeFuncObject<T>) func;
    }
    FunctionContext castingContext = new FunctionContext(NodeTypes.getContext(from), NodeTypes.getContext(to));
    INodeFunc caster = castingContext.getFunction("(" + NodeTypes.getName(to) + ")", Collections.singletonList(from));
    if (caster == null) {
        if (to == String.class) {
            return (INodeFuncObject<T>) castToString(func);
        }
        throw new InvalidExpressionException("Cannot cast from " + NodeTypes.getName(from) + " to " + NodeTypes.getName(to));
    }
    return new INodeFuncObject<T>() {

        @Override
        public INodeObject<T> getNode(INodeStack stack) throws InvalidExpressionException {
            return (INodeObject<T>) caster.getNode(new NodeStack(func.getNode(stack)));
        }

        @Override
        public Class<T> getType() {
            return to;
        }
    };
}
Also used : INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

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