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);
}
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());
}
}
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());
}
}
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!");
}
}
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);
}
Aggregations