Search in sources :

Example 6 with INodeBoolean

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

the class ExpressionTester method testVariables.

@Test
public void testVariables() {
    FunctionContext ctx = new FunctionContext(DefaultContexts.createWithAll());
    NodeVariableDouble someVariable = ctx.putVariableDouble("something");
    someVariable.value = 0;
    bakeAndCallDouble("something", 0, ctx);
    someVariable.value = 1;
    bakeAndCallDouble("something", 1, ctx);
    NodeVariableObject<String> variant = ctx.putVariableString("variant");
    String exp = "variant == 'gold'";
    INodeBoolean expBool = bakeFunctionBoolean(exp, ctx);
    variant.value = "nether_brick";
    Assert.assertFalse(expBool.evaluate());
    variant.value = "gold";
    Assert.assertTrue(expBool.evaluate());
    variant.value = "iron";
    Assert.assertFalse(expBool.evaluate());
    exp = "variant == 'wood' ? 0 : variant == 'steel' ? 1 : variant == 'obsidian' ? 2 : 3";
    INodeLong expLong = bakeFunctionLong(exp, ctx);
    variant.value = "wood";
    Assert.assertEquals(expLong.evaluate(), 0);
    variant.value = "steel";
    Assert.assertEquals(expLong.evaluate(), 1);
    variant.value = "obsidian";
    Assert.assertEquals(expLong.evaluate(), 2);
    variant.value = "some_other_value";
    Assert.assertEquals(expLong.evaluate(), 3);
}
Also used : NodeVariableDouble(buildcraft.lib.expression.node.value.NodeVariableDouble) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) FunctionContext(buildcraft.lib.expression.FunctionContext) Test(org.junit.Test)

Example 7 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean 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 8 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean 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 9 with INodeBoolean

use of buildcraft.lib.expression.api.IExpressionNode.INodeBoolean 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)

Example 10 with INodeBoolean

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

the class ElementTypeDrawnStack method deserialize0.

@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    boolean foreground = resolveEquationBool(json, "foreground", ctx, false);
    Item item = JsonUtils.getItem(json.json, "id");
    int meta = resolveEquationInt(json, "meta", ctx);
    ItemStack stack = new ItemStack(item, 1, meta);
    ISimpleDrawable icon = new GuiStack(stack);
    IGuiArea area = IGuiArea.create(pos, 16, 16);
    return new GuiElementDrawable(gui, area, icon, foreground, visible);
}
Also used : Item(net.minecraft.item.Item) IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ISimpleDrawable(buildcraft.lib.gui.ISimpleDrawable) GuiElementDrawable(buildcraft.lib.gui.elem.GuiElementDrawable) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) ItemStack(net.minecraft.item.ItemStack) GuiStack(buildcraft.lib.gui.GuiStack) FunctionContext(buildcraft.lib.expression.FunctionContext) IGuiPosition(buildcraft.lib.gui.pos.IGuiPosition)

Aggregations

INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)11 FunctionContext (buildcraft.lib.expression.FunctionContext)6 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)4 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)3 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)3 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)3 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)3 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 ISimpleDrawable (buildcraft.lib.gui.ISimpleDrawable)2 GuiElementDrawable (buildcraft.lib.gui.elem.GuiElementDrawable)2 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)2 ISprite (buildcraft.api.core.render.ISprite)1 SpriteRaw (buildcraft.lib.client.sprite.SpriteRaw)1 SubSpriteChanging (buildcraft.lib.client.sprite.SubSpriteChanging)1 IVariableNode (buildcraft.lib.expression.api.IVariableNode)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