Search in sources :

Example 1 with INodeFuncLong

use of buildcraft.lib.expression.api.INodeFunc.INodeFuncLong in project BuildCraft by BuildCraft.

the class InternalCompiler method addParentFunctions.

private static String addParentFunctions(FunctionContext context) {
    String vars = "";
    List<String> allFunctions = new ArrayList<>();
    allFunctions.addAll(context.getAllFunctions().keySet());
    allFunctions.sort(Comparator.naturalOrder());
    if (!allFunctions.isEmpty()) {
        if (!context.name.isEmpty()) {
            vars += "\n" + context.name + ":";
        }
        for (String fnName : allFunctions) {
            Map<List<Class<?>>, INodeFunc> functions = context.getFunctions(fnName);
            for (Map.Entry<List<Class<?>>, INodeFunc> entry : functions.entrySet()) {
                String args = "";
                for (Class<?> arg : entry.getKey()) {
                    if (args.length() > 0) {
                        args += ", ";
                    }
                    args += NodeTypes.getName(arg);
                }
                INodeFunc function = entry.getValue();
                String ret;
                if (function instanceof INodeFuncBoolean) {
                    ret = NodeTypes.getName(boolean.class);
                } else if (function instanceof INodeFuncDouble) {
                    ret = NodeTypes.getName(double.class);
                } else if (function instanceof INodeFuncLong) {
                    ret = NodeTypes.getName(long.class);
                } else {
                    ret = NodeTypes.getName(((INodeFuncObject<?>) function).getType());
                }
                vars += "\n  " + fnName + "(" + args + ") -> " + ret;
            }
        }
    }
    for (FunctionContext parent : context.getParents()) {
        vars += addParentFunctions(parent);
    }
    return vars;
}
Also used : INodeFuncLong(buildcraft.lib.expression.api.INodeFunc.INodeFuncLong) INodeFunc(buildcraft.lib.expression.api.INodeFunc) ArrayList(java.util.ArrayList) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) INodeFuncBoolean(buildcraft.lib.expression.api.INodeFunc.INodeFuncBoolean) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with INodeFuncLong

use of buildcraft.lib.expression.api.INodeFunc.INodeFuncLong 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

INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)2 FunctionContext (buildcraft.lib.expression.FunctionContext)1 NodeStack (buildcraft.lib.expression.NodeStack)1 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)1 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)1 INodeFunc (buildcraft.lib.expression.api.INodeFunc)1 INodeFuncBoolean (buildcraft.lib.expression.api.INodeFunc.INodeFuncBoolean)1 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)1 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)1 NodeConstantDouble (buildcraft.lib.expression.node.value.NodeConstantDouble)1 NodeVariableLong (buildcraft.lib.expression.node.value.NodeVariableLong)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1