Search in sources :

Example 1 with INodeObject

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

the class ElementTypeToolTip method deserialize0.

// Args:
// - pos[0], pos[1]: the area for help (where it will be drawn, relative to the root of the gui). Defaults
// to 0,0
// - size[0], size[1]: the size of the help area
// - area[0-3]: mapping for pos[0], pos[1], size[0], size[1]
// - text: The text to display in the tooltip
// - expression: The expression to display in the tooltip
@Override
public IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
    FunctionContext ctx = createContext(json);
    List<String> text = new ArrayList<>();
    String key = "text";
    boolean isExpression = false;
    if (json.properties.containsKey("expression") || json.properties.containsKey("expression[0]")) {
        key = "expression";
        isExpression = true;
    }
    if (json.properties.containsKey(key + "[0]")) {
        int i = 0;
        while (true) {
            String prop = json.properties.get(key + "[" + i + "]");
            if (prop == null) {
                break;
            }
            text.add(prop);
            i++;
        }
    } else {
        text.add(json.properties.getOrDefault(key, "ERROR: Text not given!"));
    }
    INodeBoolean visible = getEquationBool(json, "visible", ctx, true);
    ITooltipElement source;
    if (isExpression) {
        List<INodeObject<String>> nodes = new ArrayList<>(text.size());
        try {
            for (String s : text) {
                nodes.add(GenericExpressionCompiler.compileExpressionString(s, ctx));
            }
        } catch (InvalidExpressionException e) {
            throw new JsonSyntaxException(e);
        }
        source = (list) -> {
            if (visible.evaluate()) {
                String[] arr = new String[nodes.size()];
                for (int i = 0; i < arr.length; i++) {
                    arr[i] = nodes.get(i).evaluate();
                }
                list.add(ToolTip.createLocalized(arr));
            }
        };
    } else {
        ToolTip tooltip = ToolTip.createLocalized(text.toArray(new String[0]));
        source = (list) -> {
            if (visible.evaluate()) {
                list.add(tooltip);
            }
        };
    }
    inheritProperty(json, "pos[0]", "area[0]");
    inheritProperty(json, "pos[1]", "area[1]");
    inheritProperty(json, "size[0]", "area[2]");
    inheritProperty(json, "size[1]", "area[3]");
    IGuiArea area = resolveArea(json, "area", parent, ctx);
    return new GuiElementToolTip(gui, area, source);
}
Also used : GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) ToolTip(buildcraft.lib.gui.elem.ToolTip) IGuiArea(buildcraft.lib.gui.pos.IGuiArea) ITooltipElement(buildcraft.lib.gui.ITooltipElement) ArrayList(java.util.ArrayList) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) FunctionContext(buildcraft.lib.expression.FunctionContext) JsonSyntaxException(com.google.gson.JsonSyntaxException) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) GuiElementToolTip(buildcraft.lib.gui.GuiElementToolTip) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 2 with INodeObject

use of buildcraft.lib.expression.api.IExpressionNode.INodeObject 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)

Example 3 with INodeObject

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

the class InternalCompiler method compileFunction.

public static INodeFunc compileFunction(String expression, FunctionContext context, Argument... args) throws InvalidExpressionException {
    FunctionContext ctxReal = new FunctionContext(context);
    IVariableNode[] nodes = new IVariableNode[args.length];
    Class<?>[] types = new Class[args.length];
    for (int i = 0; i < nodes.length; i++) {
        types[i] = args[i].type;
        nodes[i] = ctxReal.putVariable(args[i].name, args[i].type);
    }
    IExpressionNode node = compileExpression(expression, ctxReal);
    if (node instanceof INodeLong) {
        return new NodeFuncGenericToLong((INodeLong) node, types, nodes);
    } else if (node instanceof INodeDouble) {
        return new NodeFuncGenericToDouble((INodeDouble) node, types, nodes);
    } else if (node instanceof INodeBoolean) {
        return new NodeFuncGenericToBoolean((INodeBoolean) node, types, nodes);
    } else if (node instanceof INodeObject<?>) {
        return new NodeFuncGenericToObject<>((INodeObject<?>) node, types, nodes);
    } else {
        ExpressionDebugManager.debugNodeClass(node.getClass());
        throw new IllegalStateException("Unknown node " + node.getClass());
    }
}
Also used : NodeFuncGenericToLong(buildcraft.lib.expression.node.func.NodeFuncGenericToLong) NodeFuncGenericToDouble(buildcraft.lib.expression.node.func.NodeFuncGenericToDouble) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) IVariableNode(buildcraft.lib.expression.api.IVariableNode) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeFuncGenericToBoolean(buildcraft.lib.expression.node.func.NodeFuncGenericToBoolean) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 4 with INodeObject

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

the class InternalCompiler method pushConditional.

private static void pushConditional(NodeStack stack) throws InvalidExpressionException {
    IExpressionNode right = stack.pop();
    IExpressionNode left = stack.pop();
    IExpressionNode conditional = stack.pop();
    right = convertBinary(right, left);
    left = convertBinary(left, right);
    if (conditional instanceof INodeBoolean) {
        INodeBoolean condition = (INodeBoolean) conditional;
        if (right instanceof INodeBoolean) {
            stack.push(new NodeConditionalBoolean(condition, (INodeBoolean) left, (INodeBoolean) right));
        } else if (right instanceof INodeDouble) {
            stack.push(new NodeConditionalDouble(condition, (INodeDouble) left, (INodeDouble) right));
        } else if (right instanceof INodeObject) {
            stack.push(new NodeConditionalObject(condition, (INodeObject) left, (INodeObject) right));
        } else if (right instanceof INodeLong) {
            stack.push(new NodeConditionalLong(condition, (INodeLong) left, (INodeLong) right));
        } else {
            throw new InvalidExpressionException("Unknown node " + left);
        }
    } else {
        throw new InvalidExpressionException("Required a boolean node, but got '" + conditional + "' of " + conditional.getClass());
    }
}
Also used : NodeConditionalBoolean(buildcraft.lib.expression.node.condition.NodeConditionalBoolean) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) NodeConditionalObject(buildcraft.lib.expression.node.condition.NodeConditionalObject) INodeBoolean(buildcraft.lib.expression.api.IExpressionNode.INodeBoolean) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeConditionalLong(buildcraft.lib.expression.node.condition.NodeConditionalLong) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject) NodeConditionalDouble(buildcraft.lib.expression.node.condition.NodeConditionalDouble)

Example 5 with INodeObject

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

the class NodeStack method popObject.

@Override
public <T> INodeObject<T> popObject(Class<T> type) throws InvalidExpressionException {
    checkTypeMatch(type);
    IExpressionNode node = pop();
    if (node instanceof INodeObject) {
        INodeObject<?> nodeObj = (INodeObject<?>) node;
        if (nodeObj.getType() == type) {
            return (INodeObject<T>) nodeObj;
        } else {
            throw new InvalidExpressionException("Cannot cast " + nodeObj.getType().getSimpleName() + " to " + type.getSimpleName() + "!");
        }
    } else {
        throw new InvalidExpressionException("Cannot cast " + node + " to " + type.getSimpleName() + "!");
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Aggregations

INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)5 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)4 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)3 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)3 FunctionContext (buildcraft.lib.expression.FunctionContext)2 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)2 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)2 NodeStack (buildcraft.lib.expression.NodeStack)1 INodeFunc (buildcraft.lib.expression.api.INodeFunc)1 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)1 INodeStack (buildcraft.lib.expression.api.INodeStack)1 IVariableNode (buildcraft.lib.expression.api.IVariableNode)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 NodeFuncGenericToLong (buildcraft.lib.expression.node.func.NodeFuncGenericToLong)1 GuiElementToolTip (buildcraft.lib.gui.GuiElementToolTip)1