Search in sources :

Example 1 with INodeFuncDouble

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

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

the class NodeCasting method castToDouble.

public static INodeFuncDouble castToDouble(INodeFunc func) throws InvalidExpressionException {
    if (func instanceof INodeFuncDouble) {
        return (INodeFuncDouble) func;
    }
    Class<?> type = NodeTypes.getType(func);
    FunctionContext ctx = NodeTypes.getContext(type);
    if (ctx == null) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    INodeFunc caster = ctx.getFunction("(double)", Collections.singletonList(type));
    if (caster == null || NodeTypes.getType(caster) != double.class) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    return (stack) -> (INodeDouble) caster.getNode(new NodeStack(func.getNode(stack)));
}
Also used : INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject) NodeTypes(buildcraft.lib.expression.api.NodeTypes) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeStack(buildcraft.lib.expression.NodeStack) Collections(java.util.Collections) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Aggregations

INodeFunc (buildcraft.lib.expression.api.INodeFunc)2 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)2 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)2 FunctionContext (buildcraft.lib.expression.FunctionContext)1 NodeStack (buildcraft.lib.expression.NodeStack)1 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)1 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)1 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)1 INodeFuncBoolean (buildcraft.lib.expression.api.INodeFunc.INodeFuncBoolean)1 INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)1 INodeStack (buildcraft.lib.expression.api.INodeStack)1 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)1 NodeTypes (buildcraft.lib.expression.api.NodeTypes)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1