Search in sources :

Example 6 with IExpressionNode

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

the class InternalCompiler method compileFunction.

public static INodeFunc compileFunction(String expression, FunctionContext context, Argument... args) throws InvalidExpressionException {
    FunctionContext ctxReal = new FunctionContext(context);
    IVariableNode[] nodes = new IVariableNode[args.length];
    Class<?>[] types = new Class[args.length];
    for (int i = 0; i < nodes.length; i++) {
        types[i] = args[i].type;
        nodes[i] = ctxReal.putVariable(args[i].name, args[i].type);
    }
    IExpressionNode node = compileExpression(expression, ctxReal);
    if (node instanceof INodeLong) {
        return new NodeFuncGenericToLong((INodeLong) node, types, nodes);
    } else if (node instanceof INodeDouble) {
        return new NodeFuncGenericToDouble((INodeDouble) node, types, nodes);
    } else if (node instanceof INodeBoolean) {
        return new NodeFuncGenericToBoolean((INodeBoolean) node, types, nodes);
    } else if (node instanceof INodeObject<?>) {
        return new NodeFuncGenericToObject<>((INodeObject<?>) node, types, nodes);
    } else {
        ExpressionDebugManager.debugNodeClass(node.getClass());
        throw new IllegalStateException("Unknown node " + node.getClass());
    }
}
Also used : NodeFuncGenericToLong(buildcraft.lib.expression.node.func.NodeFuncGenericToLong) NodeFuncGenericToDouble(buildcraft.lib.expression.node.func.NodeFuncGenericToDouble) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) IVariableNode(buildcraft.lib.expression.api.IVariableNode) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeFuncGenericToBoolean(buildcraft.lib.expression.node.func.NodeFuncGenericToBoolean) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 7 with IExpressionNode

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

the class InternalCompiler method pushConditional.

private static void pushConditional(NodeStack stack) throws InvalidExpressionException {
    IExpressionNode right = stack.pop();
    IExpressionNode left = stack.pop();
    IExpressionNode conditional = stack.pop();
    right = convertBinary(right, left);
    left = convertBinary(left, right);
    if (conditional instanceof INodeBoolean) {
        INodeBoolean condition = (INodeBoolean) conditional;
        if (right instanceof INodeBoolean) {
            stack.push(new NodeConditionalBoolean(condition, (INodeBoolean) left, (INodeBoolean) right));
        } else if (right instanceof INodeDouble) {
            stack.push(new NodeConditionalDouble(condition, (INodeDouble) left, (INodeDouble) right));
        } else if (right instanceof INodeObject) {
            stack.push(new NodeConditionalObject(condition, (INodeObject) left, (INodeObject) right));
        } else if (right instanceof INodeLong) {
            stack.push(new NodeConditionalLong(condition, (INodeLong) left, (INodeLong) right));
        } else {
            throw new InvalidExpressionException("Unknown node " + left);
        }
    } else {
        throw new InvalidExpressionException("Required a boolean node, but got '" + conditional + "' of " + conditional.getClass());
    }
}
Also used : NodeConditionalBoolean(buildcraft.lib.expression.node.condition.NodeConditionalBoolean) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) NodeConditionalObject(buildcraft.lib.expression.node.condition.NodeConditionalObject) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeConditionalLong(buildcraft.lib.expression.node.condition.NodeConditionalLong) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject) NodeConditionalDouble(buildcraft.lib.expression.node.condition.NodeConditionalDouble)

Example 8 with IExpressionNode

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

the class NodeStack method popObject.

@Override
public <T> INodeObject<T> popObject(Class<T> type) throws InvalidExpressionException {
    checkTypeMatch(type);
    IExpressionNode node = pop();
    if (node instanceof INodeObject) {
        INodeObject<?> nodeObj = (INodeObject<?>) node;
        if (nodeObj.getType() == type) {
            return (INodeObject<T>) nodeObj;
        } else {
            throw new InvalidExpressionException("Cannot cast " + nodeObj.getType().getSimpleName() + " to " + type.getSimpleName() + "!");
        }
    } else {
        throw new InvalidExpressionException("Cannot cast " + node + " to " + type.getSimpleName() + "!");
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 9 with IExpressionNode

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

the class NodeStack method pop.

public IExpressionNode pop() throws InvalidExpressionException {
    if (stack.isEmpty()) {
        throw new InvalidExpressionException("No more nodes to pop!");
    } else {
        IExpressionNode node = stack.remove(stack.size() - 1);
        ExpressionDebugManager.debugPrintln("Popped " + node);
        return node;
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 10 with IExpressionNode

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

the class NodeStack method popBoolean.

@Override
public INodeBoolean popBoolean() throws InvalidExpressionException {
    checkTypeMatch(boolean.class);
    IExpressionNode node = pop();
    if (node instanceof INodeBoolean) {
        return (INodeBoolean) node;
    } else {
        throw new InvalidExpressionException("Cannot cast " + node + " to a boolean!");
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) 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