Search in sources :

Example 1 with INodeFunc

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

the class FunctionContext method getVariable.

// Variable getter/setters
public IExpressionNode getVariable(String name) {
    name = name.toLowerCase(Locale.ROOT);
    IExpressionNode current = variables.get(name);
    if (current != null) {
        return current;
    }
    for (FunctionContext parent : parents) {
        IExpressionNode node = parent.getVariable(name);
        if (node != null)
            return node;
    }
    INodeFunc func = getFunction(name, Collections.emptyList());
    if (func != null) {
        try {
            return func.getNode(new NodeStack());
        } catch (InvalidExpressionException e) {
            throw new IllegalStateException("Found a 0-args function that didn't allow us to get a node for it!", e);
        }
    }
    return null;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 2 with INodeFunc

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

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

the class NodeCasting method castToType.

public static IExpressionNode castToType(IExpressionNode node, Class<?> to) throws InvalidExpressionException {
    Class<?> from = NodeTypes.getType(node);
    if (from == to) {
        return node;
    }
    FunctionContext castingContext = new FunctionContext(NodeTypes.getContext(from), NodeTypes.getContext(to));
    INodeFunc caster = castingContext.getFunction("(" + NodeTypes.getName(to) + ")", Collections.singletonList(from));
    if (caster == null) {
        if (to == String.class) {
            return new NodeCastToString(node);
        }
        throw new InvalidExpressionException("Cannot cast from " + NodeTypes.getName(from) + " to " + NodeTypes.getName(to));
    }
    NodeStack stack = new NodeStack(node);
    stack.setRecorder(Collections.singletonList(from), caster);
    IExpressionNode casted = caster.getNode(stack);
    stack.checkAndRemoveRecorder();
    Class<?> actual = NodeTypes.getType(casted);
    if (actual != to) {
        throw new IllegalStateException("The caster " + caster + " didn't produce the correct result! (Expected " + to + ", but got " + actual + ")");
    }
    return casted;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 4 with INodeFunc

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

the class NodeCasting method castToDouble.

public static INodeDouble castToDouble(IExpressionNode node) throws InvalidExpressionException {
    if (node instanceof INodeDouble) {
        return (INodeDouble) node;
    }
    Class<?> type = NodeTypes.getType(node);
    FunctionContext ctx = NodeTypes.getContext(type);
    if (ctx == null) {
        throw new InvalidExpressionException("Cannot cast " + node + " to a double!");
    }
    INodeFunc func = ctx.getFunction("(double)", Collections.singletonList(type));
    if (func == null || NodeTypes.getType(func) != double.class) {
        throw new InvalidExpressionException("Cannot cast " + node + " to a double!");
    }
    return (INodeDouble) func.getNode(new NodeStack(node));
}
Also used : 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)

Example 5 with INodeFunc

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

the class NodeCasting method castToObject.

public static <T> INodeFuncObject<T> castToObject(INodeFunc func, Class<T> to) throws InvalidExpressionException {
    Class<?> from = NodeTypes.getType(func);
    if (from == to) {
        return (INodeFuncObject<T>) func;
    }
    FunctionContext castingContext = new FunctionContext(NodeTypes.getContext(from), NodeTypes.getContext(to));
    INodeFunc caster = castingContext.getFunction("(" + NodeTypes.getName(to) + ")", Collections.singletonList(from));
    if (caster == null) {
        if (to == String.class) {
            return (INodeFuncObject<T>) castToString(func);
        }
        throw new InvalidExpressionException("Cannot cast from " + NodeTypes.getName(from) + " to " + NodeTypes.getName(to));
    }
    return new INodeFuncObject<T>() {

        @Override
        public INodeObject<T> getNode(INodeStack stack) throws InvalidExpressionException {
            return (INodeObject<T>) caster.getNode(new NodeStack(func.getNode(stack)));
        }

        @Override
        public Class<T> getType() {
            return to;
        }
    };
}
Also used : INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Aggregations

INodeFunc (buildcraft.lib.expression.api.INodeFunc)7 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)6 INodeStack (buildcraft.lib.expression.api.INodeStack)5 FunctionContext (buildcraft.lib.expression.FunctionContext)4 NodeStack (buildcraft.lib.expression.NodeStack)4 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)4 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)3 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)3 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)2 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)1 INodeFuncBoolean (buildcraft.lib.expression.api.INodeFunc.INodeFuncBoolean)1 INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)1 NodeTypes (buildcraft.lib.expression.api.NodeTypes)1 NodeCastLongToDouble (buildcraft.lib.expression.node.cast.NodeCastLongToDouble)1 NodeFuncObjectObjectToBoolean (buildcraft.lib.expression.node.func.NodeFuncObjectObjectToBoolean)1