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;
}
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);
}
}
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;
}
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!");
}
}
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);
}
Aggregations