Search in sources :

Example 1 with InvalidExpressionException

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

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

the class InternalCompiler method compileExpression.

public static IExpressionNode compileExpression(String expression, FunctionContext context) throws InvalidExpressionException {
    if (context == null) {
        context = new FunctionContext("default");
    }
    try {
        ExpressionDebugManager.debugPrintln("Compiling " + expression);
        String[] infix = tokenize(expression, context);
        String[] postfix = convertToPostfix(infix);
        ExpressionDebugManager.debugPrintln(Arrays.toString(postfix));
        return makeExpression(postfix, context);
    } catch (InvalidExpressionException iee) {
        throw new InvalidExpressionException("Failed to compile expression " + expression, iee);
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException)

Example 3 with InvalidExpressionException

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

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

the class NodeStack method popLong.

@Override
public INodeLong popLong() throws InvalidExpressionException {
    checkTypeMatch(long.class);
    IExpressionNode node = pop();
    if (node instanceof INodeLong) {
        return (INodeLong) node;
    } else {
        throw new InvalidExpressionException("Cannot cast " + node + " to a long!");
    }
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 5 with InvalidExpressionException

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

Aggregations

InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)21 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)12 FunctionContext (buildcraft.lib.expression.FunctionContext)8 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)6 INodeFunc (buildcraft.lib.expression.api.INodeFunc)6 INodeStack (buildcraft.lib.expression.api.INodeStack)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)5 NodeStack (buildcraft.lib.expression.NodeStack)4 INodeBoolean (buildcraft.lib.expression.api.IExpressionNode.INodeBoolean)3 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)3 ArrayList (java.util.ArrayList)3 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)2 IGuiArea (buildcraft.lib.gui.pos.IGuiArea)2 IGuiPosition (buildcraft.lib.gui.pos.IGuiPosition)2 JsonObject (com.google.gson.JsonObject)2 ResourceLoaderContext (buildcraft.lib.client.model.ResourceLoaderContext)1 IConstantNode (buildcraft.lib.expression.api.IConstantNode)1 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)1 NodeTypes (buildcraft.lib.expression.api.NodeTypes)1