Search in sources :

Example 1 with NodeStack

use of buildcraft.lib.expression.NodeStack 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;
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode)

Example 2 with NodeStack

use of buildcraft.lib.expression.NodeStack 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));
}
Also used : InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Example 3 with NodeStack

use of buildcraft.lib.expression.NodeStack 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;
        }
    };
}
Also used : INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext) INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject)

Example 4 with NodeStack

use of buildcraft.lib.expression.NodeStack in project BuildCraft by BuildCraft.

the class ExpressionTester method testMath.

@Test
public void testMath() throws InvalidExpressionException {
    FunctionContext ctx2 = DefaultContexts.createWithAll();
    List<Class<?>> list_d = Collections.singletonList(double.class);
    List<Class<?>> list_l = Collections.singletonList(long.class);
    List<Class<?>> list_ll = Arrays.asList(long.class, long.class);
    System.out.println(ctx2.getFunctions("sin"));
    System.out.println(ctx2.getFunction("sin", list_d));
    System.out.println(ctx2.getFunction("cosh", list_d));
    System.out.println(ctx2.getFunction("round", list_d));
    System.out.println(ctx2.getFunction("ceil", list_d));
    System.out.println(ctx2.getFunction("max", list_d));
    System.out.println(ctx2.getFunction("max", list_l));
    System.out.println(ctx2.getFunction("max", list_ll));
    NodeStack stack4 = new NodeStack();
    stack4.push(new NodeConstantDouble(0.4));
    INodeLong out = (INodeLong) ctx2.getFunction("ceil", list_d).getNode(stack4);
    System.out.println(out + " = " + out.evaluate());
    stack4.push(new NodeConstantDouble(0.4));
    out = (INodeLong) ctx2.getFunction("floor", list_d).getNode(stack4);
    System.out.println(out + " = " + out.evaluate());
    INodeDouble nd = (INodeDouble) ctx2.getVariable("pi");
    System.out.println(nd + " = " + nd.evaluate());
    nd = (INodeDouble) ctx2.getVariable("e");
    System.out.println(nd + " = " + nd.evaluate());
    INodeFuncLong func3 = GenericExpressionCompiler.compileFunctionLong("input * 2 + 1", ctx2, Argument.argLong("input"));
    NodeStack stack3 = new NodeStack();
    NodeVariableLong input = stack3.push(new NodeVariableLong("input"));
    INodeLong node3 = func3.getNode(stack3);
    input.value = 1;
    System.out.println(node3 + " = " + node3.evaluate());
    input.value = 30;
    System.out.println(node3 + " = " + node3.evaluate());
    ctx2.put_ll_l("sub", (a, b) -> a - b);
    testExpr("floor(ceil(0.5)+0.5)", ctx2);
    testExpr("sub(5, 6)", ctx2);
    testExpr("5.sub(6.4.round()) + 0.5.ceil()", ctx2);
    testExpr("5.sub(6) + 0.5.ceil() & ' -- ' & 45 + 2", ctx2);
    testExpr("165 + 15 - 6 * 46.sub(10)", ctx2);
    testExpr("log(10)", ctx2);
    testExpr("log10(10)", ctx2);
    testExpr("cos(radians(90))", ctx2);
    testExpr("cos(radians(90)).round_float()", ctx2);
    testExpr("cos(radians(91)).round_float()", ctx2);
    testExpr("cos(radians(92)).round_float()", ctx2);
    testExpr("cos(radians(93)).round_float()", ctx2);
    testExpr("cos(radians(94)).round_float()", ctx2);
    testExpr("floor(ceil(0.5)+0.5)", ctx2);
    testExpr("sub(5, 6)", ctx2);
    testExpr("5.sub(6.4.round()) + 0.5.ceil()", ctx2);
    testExpr("5.sub(6) + 0.5.ceil() & ' -- ' & 45 + 2", ctx2);
    testExpr("165 + 15 - 6 * 46.sub(10)", ctx2);
    testExpr("log(10)", ctx2);
    testExpr("log10(10)", ctx2);
    testExpr("cos(radians(90))", ctx2);
    testExpr("cos(radians(90)).round_float()", ctx2);
    testExpr("cos(radians(91)).round_float()", ctx2);
    testExpr("cos(radians(92)).round_float()", ctx2);
    testExpr("cos(radians(93)).round_float()", ctx2);
    testExpr("cos(radians(94)).round_float()", ctx2);
}
Also used : INodeFuncLong(buildcraft.lib.expression.api.INodeFunc.INodeFuncLong) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) NodeStack(buildcraft.lib.expression.NodeStack) INodeLong(buildcraft.lib.expression.api.IExpressionNode.INodeLong) NodeVariableLong(buildcraft.lib.expression.node.value.NodeVariableLong) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeConstantDouble(buildcraft.lib.expression.node.value.NodeConstantDouble) Test(org.junit.Test)

Example 5 with NodeStack

use of buildcraft.lib.expression.NodeStack in project BuildCraft by BuildCraft.

the class NodeCasting method castToDouble.

public static INodeFuncDouble castToDouble(INodeFunc func) throws InvalidExpressionException {
    if (func instanceof INodeFuncDouble) {
        return (INodeFuncDouble) func;
    }
    Class<?> type = NodeTypes.getType(func);
    FunctionContext ctx = NodeTypes.getContext(type);
    if (ctx == null) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    INodeFunc caster = ctx.getFunction("(double)", Collections.singletonList(type));
    if (caster == null || NodeTypes.getType(caster) != double.class) {
        throw new InvalidExpressionException("Cannot cast " + func + " to a double!");
    }
    return (stack) -> (INodeDouble) caster.getNode(new NodeStack(func.getNode(stack)));
}
Also used : INodeObject(buildcraft.lib.expression.api.IExpressionNode.INodeObject) NodeTypes(buildcraft.lib.expression.api.NodeTypes) IExpressionNode(buildcraft.lib.expression.api.IExpressionNode) INodeFuncObject(buildcraft.lib.expression.api.INodeFunc.INodeFuncObject) INodeStack(buildcraft.lib.expression.api.INodeStack) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) FunctionContext(buildcraft.lib.expression.FunctionContext) NodeStack(buildcraft.lib.expression.NodeStack) Collections(java.util.Collections) INodeFuncDouble(buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble) InvalidExpressionException(buildcraft.lib.expression.api.InvalidExpressionException) INodeDouble(buildcraft.lib.expression.api.IExpressionNode.INodeDouble) INodeFunc(buildcraft.lib.expression.api.INodeFunc) INodeStack(buildcraft.lib.expression.api.INodeStack) NodeStack(buildcraft.lib.expression.NodeStack) FunctionContext(buildcraft.lib.expression.FunctionContext)

Aggregations

FunctionContext (buildcraft.lib.expression.FunctionContext)5 NodeStack (buildcraft.lib.expression.NodeStack)5 INodeFunc (buildcraft.lib.expression.api.INodeFunc)4 INodeStack (buildcraft.lib.expression.api.INodeStack)4 InvalidExpressionException (buildcraft.lib.expression.api.InvalidExpressionException)4 INodeDouble (buildcraft.lib.expression.api.IExpressionNode.INodeDouble)3 IExpressionNode (buildcraft.lib.expression.api.IExpressionNode)2 INodeObject (buildcraft.lib.expression.api.IExpressionNode.INodeObject)2 INodeFuncObject (buildcraft.lib.expression.api.INodeFunc.INodeFuncObject)2 INodeLong (buildcraft.lib.expression.api.IExpressionNode.INodeLong)1 INodeFuncDouble (buildcraft.lib.expression.api.INodeFunc.INodeFuncDouble)1 INodeFuncLong (buildcraft.lib.expression.api.INodeFunc.INodeFuncLong)1 NodeTypes (buildcraft.lib.expression.api.NodeTypes)1 NodeConstantDouble (buildcraft.lib.expression.node.value.NodeConstantDouble)1 NodeVariableLong (buildcraft.lib.expression.node.value.NodeVariableLong)1 Collections (java.util.Collections)1 Test (org.junit.Test)1