use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.
the class ElementType method resolvePosition.
public static IGuiPosition resolvePosition(JsonGuiElement json, String name, IGuiPosition parent, FunctionContext ctx) {
String eqn = json.properties.get(name);
if (eqn == null) {
INodeDouble x = getEquationDouble(json, name + "[0]", ctx);
INodeDouble y = getEquationDouble(json, name + "[1]", ctx);
return IGuiPosition.create(x, y).offset(parent);
}
try {
return GenericExpressionCompiler.compileExpressionObject(IGuiPosition.class, eqn, ctx).evaluate();
} catch (InvalidExpressionException e) {
throw new JsonSyntaxException("Failed to resolve a position for " + json.fullName, e);
}
}
use of buildcraft.lib.expression.api.InvalidExpressionException in project BuildCraft by BuildCraft.
the class ElementType method resolveArea.
public static IGuiArea resolveArea(JsonGuiElement json, String name, IGuiPosition parent, FunctionContext ctx) {
String eqn = json.properties.get(name);
if (eqn == null) {
INodeDouble x = getEquationDouble(json, name + "[0]", ctx);
INodeDouble y = getEquationDouble(json, name + "[1]", ctx);
INodeDouble w = getEquationDouble(json, name + "[2]", ctx);
INodeDouble h = getEquationDouble(json, name + "[3]", ctx);
return IGuiArea.create(x, y, w, h).offset(parent);
}
try {
return GenericExpressionCompiler.compileExpressionObject(IGuiArea.class, eqn, ctx).evaluate();
} catch (InvalidExpressionException e) {
throw new JsonSyntaxException("Failed to resolve an area for " + json.fullName, e);
}
}
use of buildcraft.lib.expression.api.InvalidExpressionException 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;
}
use of buildcraft.lib.expression.api.InvalidExpressionException 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));
}
use of buildcraft.lib.expression.api.InvalidExpressionException 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;
}
};
}
Aggregations