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