Search in sources :

Example 1 with NodeConstantDouble

use of buildcraft.lib.expression.node.value.NodeConstantDouble in project BuildCraft by BuildCraft.

the class InternalCompiler method makeExpression.

private static IExpressionNode makeExpression(String[] postfix, FunctionContext context) throws InvalidExpressionException {
    NodeStack stack = new NodeStack();
    for (int i = 0; i < postfix.length; i++) {
        String op = postfix[i];
        if (OPERATORS.contains(op) && !"?".equals(op) && !":".equals(op)) {
            boolean isNegation = UNARY_NEGATION.equals(op);
            int count = 2;
            if (isNegation || OPERATORS_SINGLE.contains(op)) {
                op = isNegation ? "-" : op;
                count = 1;
            }
            String function = op + FUNCTION_ARGS + count;
            pushFunctionNode(stack, function, context);
        } else if (// NO-OP, all handled by "?"
        ":".equals(op))
            // NO-OP, all handled by "?"
            continue;
        else if ("?".equals(op))
            pushConditional(stack);
        else if (isValidLong(op)) {
            long val = parseValidLong(op);
            stack.push(new NodeConstantLong(val));
        } else if (isValidDouble(op)) {
            stack.push(new NodeConstantDouble(Double.parseDouble(op)));
        } else if (BOOLEAN_MATCHER.matcher(op).matches()) {
            stack.push(NodeConstantBoolean.of(Boolean.parseBoolean(op)));
        } else if (STRING_MATCHER.matcher(op).matches()) {
            stack.push(new NodeConstantObject<>(String.class, op.substring(1, op.length() - 1)));
        } else if (op.startsWith(FUNCTION_START)) {
            // Its a function
            String function = op.substring(1);
            pushFunctionNode(stack, function, context);
        } else {
            IExpressionNode node = context == null ? null : context.getVariable(op);
            if (node == null && op.contains(".")) {
                int index = op.indexOf('.');
                String type = op.substring(0, index);
                FunctionContext ctx = getContext(type);
                if (ctx != null) {
                    node = ctx.getVariable(op);
                    if (node == null) {
                        node = ctx.getVariable(op.substring(index + 1));
                    }
                }
            }
            if (node != null) {
                stack.push(node);
            } else {
                String vars = getValidVariablesErrorString(context);
                throw new InvalidExpressionException("Unknown variable '" + op + "'" + vars);
            }
        }
    }
    IExpressionNode node = stack.pop().inline();
    if (!stack.isEmpty()) {
        throw new InvalidExpressionException("Tried to make an expression with too many nodes! (" + stack + ")");
    }
    return node;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeStack(buildcraft.lib.expression.api.INodeStack) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) NodeConstantLong(buildcraft.lib.expression.node.value.NodeConstantLong) NodeConstantDouble(buildcraft.lib.expression.node.value.NodeConstantDouble)

Example 2 with NodeConstantDouble

use of buildcraft.lib.expression.node.value.NodeConstantDouble 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)

Example 3 with NodeConstantDouble

use of buildcraft.lib.expression.node.value.NodeConstantDouble in project BuildCraft by BuildCraft.

the class NodeConditionalDouble method inline.

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

Aggregations

NodeConstantDouble (buildcraft.lib.expression.node.value.NodeConstantDouble)3 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)2 FunctionContext (buildcraft.lib.expression.FunctionContext)1 NodeStack (buildcraft.lib.expression.NodeStack)1 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)1 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)1 INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)1 INodeStack (buildcraft.lib.expression.api.INodeStack)1 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)1 NodeConstantBoolean (buildcraft.lib.expression.node.value.NodeConstantBoolean)1 NodeConstantLong (buildcraft.lib.expression.node.value.NodeConstantLong)1 NodeVariableLong (buildcraft.lib.expression.node.value.NodeVariableLong)1 Test (org.junit.Test)1