use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.
the class JsonModelRule method deserialize.
public static JsonModelRule deserialize(JsonElement json, FunctionContext fnCtx, ResourceLoaderContext ctx) {
if (!json.isJsonObject()) {
throw new JsonSyntaxException("Expected an object, got " + json);
}
JsonObject obj = json.getAsJsonObject();
String when = JsonUtils.getString(obj, "when");
INodeBoolean nodeWhen = JsonVariableModelPart.convertStringToBooleanNode(when, fnCtx);
String type = JsonUtils.getString(obj, "type");
if (type.startsWith("builtin:")) {
String builtin = type.substring("builtin:".length());
if ("rotate_facing".equals(builtin)) {
fnCtx = new FunctionContext(fnCtx, ExpressionCompat.ENUM_FACING);
String from = JsonUtils.getString(obj, "from");
INodeObject<EnumFacing> nodeFrom = JsonVariableModelPart.convertStringToObjectNode(from, fnCtx, EnumFacing.class);
String to = JsonUtils.getString(obj, "to");
INodeObject<EnumFacing> nodeTo = JsonVariableModelPart.convertStringToObjectNode(to, fnCtx, EnumFacing.class);
INodeDouble[] origin;
if (obj.has("origin")) {
origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
} else {
origin = RuleRotateFacing.DEFAULT_ORIGIN;
}
return new RuleRotateFacing(nodeWhen, nodeFrom, nodeTo, origin);
} else if ("rotate".equals(builtin)) {
INodeDouble[] origin;
if (obj.has("origin")) {
origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
} else {
origin = RuleRotate.DEFAULT_ORIGIN;
}
INodeDouble[] angles = JsonVariableModelPart.readVariablePosition(obj, "angle", fnCtx);
return new RuleRotate(nodeWhen, origin, angles);
} else if ("scale".equals(builtin)) {
INodeDouble[] origin;
if (obj.has("origin")) {
origin = JsonVariableModelPart.readVariablePosition(obj, "origin", fnCtx);
} else {
origin = RuleRotate.DEFAULT_ORIGIN;
}
INodeDouble[] scales = JsonVariableModelPart.readVariablePosition(obj, "scale", fnCtx);
return new RuleScale(nodeWhen, origin, scales);
} else {
throw new JsonSyntaxException("Unknown built in rule type '" + builtin + "'");
}
} else {
throw new JsonSyntaxException("Unknown rule type '" + type + "'");
}
}
use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.
the class ElementTypeContainer method deserialize0.
@Override
protected IGuiElement deserialize0(BuildCraftJsonGui gui, IGuiPosition parent, JsonGuiInfo info, JsonGuiElement json) {
FunctionContext ctx = createContext(json);
boolean scissor = resolveEquationBool(json, "limit", ctx, false);
if (scissor) {
IGuiArea area = resolveArea(json, "area", parent, ctx);
return new GuiElementContainerScissor(gui, area);
} else {
IGuiPosition pos = resolvePosition(json, "pos", parent, ctx);
return new GuiElementContainerResizing(gui, pos);
}
}
use of buildcraft.lib.expression.FunctionContext in project BuildCraft by BuildCraft.
the class ExpressionTester method testFunctions.
@Test
public void testFunctions() throws InvalidExpressionException {
FunctionContext ctx = DefaultContexts.createWithAll();
compileFuncLong(ctx, "one", "1");
compileFuncLong(ctx, "same", "value", Argument.argLong("value"));
compileFuncDouble(ctx, "same", "value", Argument.argDouble("value"));
compileFuncDouble(ctx, "powertwo", "pow(2,input)", Argument.argDouble("input"));
compileFuncDouble(ctx, "subtract", "l - r", Argument.argDouble("l"), Argument.argDouble("r"));
compileFuncDouble(ctx, "tuple", "a + b + c", Argument.argDouble("a"), Argument.argDouble("b"), Argument.argDouble("c"));
compileFuncDouble(ctx, "powlong", "pow((same(a + 1) - 1) , (same(b) * one()))", Argument.argDouble("a"), Argument.argDouble("b"));
bakeAndCallDouble("one()", 1, ctx);
bakeAndCallDouble("oNe()", 1, ctx);
bakeAndCallDouble("same(0)", 0, ctx);
bakeAndCallDouble("same(one())", 1, ctx);
bakeAndCallDouble("same(pow(2,5))", 32, ctx);
bakeAndCallDouble("powerTwo(5)", 32, ctx);
bakeAndCallDouble("powertwo(6)", 64, ctx);
bakeAndCallDouble("subtract(3, 1)", 2, ctx);
bakeAndCallDouble("subtract(1, 3)", -2, ctx);
bakeAndCallDouble("subtract(1, -3)", 4, ctx);
bakeAndCallDouble("subtract(1, -3)", 4, ctx);
bakeAndCallDouble("tuple(1, 2, 3)", 6, ctx);
bakeAndCallDouble("tuple(3, 2, 1)", 6, ctx);
bakeAndCallDouble("tuple(-7, 1, 0)", -6, ctx);
bakeAndCallDouble("tuple(1, 3, 2)", 6, ctx);
bakeAndCallDouble("powLong(3, 3)", 27, ctx);
}
use of buildcraft.lib.expression.FunctionContext 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.FunctionContext 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));
}
Aggregations