Search in sources :

Example 1 with INodeLong

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

the class NodeStack method popLong.

@Override
public INodeLong popLong() throws InvalidExpressionException {
    checkTypeMatch(long.class);
    IExpressionNode node = pop();
    if (node instanceof INodeLong) {
        return (INodeLong) node;
    } else {
        throw new InvalidExpressionException("Cannot cast " + node + " to a long!");
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 2 with INodeLong

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

the class NodeConditionalLong method inline.

@Override
public INodeLong inline() {
    INodeBoolean c = condition.inline();
    INodeLong t = ifTrue.inline();
    INodeLong f = ifFalse.inline();
    if (c instanceof NodeConstantBoolean && t instanceof NodeConstantLong && f instanceof NodeConstantLong) {
        return new NodeConstantLong(((NodeConstantBoolean) c).value ? ((NodeConstantLong) t).value : ((NodeConstantLong) f).value);
    } else if (c != condition || t != ifTrue || f != ifFalse) {
        return new NodeConditionalLong(c, t, f);
    } else {
        return this;
    }
}
Also used : INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeConstantBoolean(buildcraft.lib.expression.node.value.NodeConstantBoolean) NodeConstantLong(buildcraft.lib.expression.node.value.NodeConstantLong)

Example 3 with INodeLong

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

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

the class ExpressionTester method bakeAndCallLong.

private static void bakeAndCallLong(String function, long expected, FunctionContext ctx) {
    ExpressionDebugManager.debugPrintln("Testing \"" + function + "\", expecting " + expected);
    INodeLong node = bakeFunctionLong(function, ctx);
    ExpressionDebugManager.debugPrintln("To " + node);
    long got = node.evaluate();
    Assert.assertEquals(expected, got);
}
Also used : INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong)

Example 5 with INodeLong

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

the class ExpressionTester method testMath.

@Test
public void testMath() throws InvalidExpressionException {
    FunctionContext ctx2 = DefaultContexts.createWithAll();
    List<Class<?>> list_d = Collections.singletonList(double.class);
    List<Class<?>> list_l = Collections.singletonList(long.class);
    List<Class<?>> list_ll = Arrays.asList(long.class, long.class);
    System.out.println(ctx2.getFunctions("sin"));
    System.out.println(ctx2.getFunction("sin", list_d));
    System.out.println(ctx2.getFunction("cosh", list_d));
    System.out.println(ctx2.getFunction("round", list_d));
    System.out.println(ctx2.getFunction("ceil", list_d));
    System.out.println(ctx2.getFunction("max", list_d));
    System.out.println(ctx2.getFunction("max", list_l));
    System.out.println(ctx2.getFunction("max", list_ll));
    NodeStack stack4 = new NodeStack();
    stack4.push(new NodeConstantDouble(0.4));
    INodeLong out = (INodeLong) ctx2.getFunction("ceil", list_d).getNode(stack4);
    System.out.println(out + " = " + out.evaluate());
    stack4.push(new NodeConstantDouble(0.4));
    out = (INodeLong) ctx2.getFunction("floor", list_d).getNode(stack4);
    System.out.println(out + " = " + out.evaluate());
    INodeDouble nd = (INodeDouble) ctx2.getVariable("pi");
    System.out.println(nd + " = " + nd.evaluate());
    nd = (INodeDouble) ctx2.getVariable("e");
    System.out.println(nd + " = " + nd.evaluate());
    INodeFuncLong func3 = GenericExpressionCompiler.compileFunctionLong("input * 2 + 1", ctx2, Argument.argLong("input"));
    NodeStack stack3 = new NodeStack();
    NodeVariableLong input = stack3.push(new NodeVariableLong("input"));
    INodeLong node3 = func3.getNode(stack3);
    input.value = 1;
    System.out.println(node3 + " = " + node3.evaluate());
    input.value = 30;
    System.out.println(node3 + " = " + node3.evaluate());
    ctx2.put_ll_l("sub", (a, b) -> a - b);
    testExpr("floor(ceil(0.5)+0.5)", ctx2);
    testExpr("sub(5, 6)", ctx2);
    testExpr("5.sub(6.4.round()) + 0.5.ceil()", ctx2);
    testExpr("5.sub(6) + 0.5.ceil() & ' -- ' & 45 + 2", ctx2);
    testExpr("165 + 15 - 6 * 46.sub(10)", ctx2);
    testExpr("log(10)", ctx2);
    testExpr("log10(10)", ctx2);
    testExpr("cos(radians(90))", ctx2);
    testExpr("cos(radians(90)).round_float()", ctx2);
    testExpr("cos(radians(91)).round_float()", ctx2);
    testExpr("cos(radians(92)).round_float()", ctx2);
    testExpr("cos(radians(93)).round_float()", ctx2);
    testExpr("cos(radians(94)).round_float()", ctx2);
    testExpr("floor(ceil(0.5)+0.5)", ctx2);
    testExpr("sub(5, 6)", ctx2);
    testExpr("5.sub(6.4.round()) + 0.5.ceil()", ctx2);
    testExpr("5.sub(6) + 0.5.ceil() & ' -- ' & 45 + 2", ctx2);
    testExpr("165 + 15 - 6 * 46.sub(10)", ctx2);
    testExpr("log(10)", ctx2);
    testExpr("log10(10)", ctx2);
    testExpr("cos(radians(90))", ctx2);
    testExpr("cos(radians(90)).round_float()", ctx2);
    testExpr("cos(radians(91)).round_float()", ctx2);
    testExpr("cos(radians(92)).round_float()", ctx2);
    testExpr("cos(radians(93)).round_float()", ctx2);
    testExpr("cos(radians(94)).round_float()", ctx2);
}
Also used : INodeFuncLong(buildcraft.lib.expression.api.INodeFunc.INodeFuncLong) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) NodeStack(buildcraft.lib.expression.NodeStack) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeVariableLong(buildcraft.lib.expression.node.value.NodeVariableLong) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeConstantDouble(buildcraft.lib.expression.node.value.NodeConstantDouble) Test(org.junit.Test)

Aggregations

INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)8 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)4 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)4 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)3 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)3 FunctionContext (buildcraft.lib.expression.FunctionContext)2 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)2 Test (org.junit.Test)2 NodeStack (buildcraft.lib.expression.NodeStack)1 INodeFunc (buildcraft.lib.expression.api.INodeFunc)1 INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)1 INodeStack (buildcraft.lib.expression.api.INodeStack)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