use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble 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);
}
use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble in project BuildCraft by BuildCraft.
the class InternalCompiler method pushFunctionNode.
private static void pushFunctionNode(NodeStack stack, String function, FunctionContext context) throws InvalidExpressionException {
String name = function.substring(0, function.indexOf(FUNCTION_ARGS));
String argCount = function.substring(function.indexOf(FUNCTION_ARGS) + 1);
int count = Integer.parseInt(argCount);
if (name.startsWith(".")) {
/*
* Allow object style function calling by making the called node be the first argument to the function,
* pushing all other nodes back
*/
name = name.substring(1);
count++;
}
List<IExpressionNode> popOrder = stack.peek(count);
List<Class<?>> functionOrder = new ArrayList<>(popOrder.size());
Map<List<Class<?>>, INodeFunc> functions = new HashMap<>();
if (name.contains(".")) {
int index = name.indexOf('.');
String type = name.substring(0, index);
FunctionContext ctx = getContext(type);
if (ctx != null) {
functions.putAll(ctx.getFunctions(name.substring(index + 1)));
}
}
FunctionContext[] ctxs = new FunctionContext[count + 1];
ctxs[0] = context;
int n = 1;
for (IExpressionNode node : popOrder) {
Class<?> nodeType = NodeTypes.getType(node);
functionOrder.add(0, nodeType);
ctxs[n++] = NodeTypes.getContext(nodeType);
}
functions.putAll(new FunctionContext(ctxs).getFunctions(name));
INodeFunc bestFunction = null;
int bestCastCount = Integer.MAX_VALUE;
List<INodeFunc> bestCasters = null;
List<Class<?>> bestClassesTo = null;
List<String> fnOrderNames = functionOrder.stream().map(NodeTypes::getName).collect(Collectors.toList());
ExpressionDebugManager.debugStart("Finding best function called '" + name + "' for " + fnOrderNames);
for (Map.Entry<List<Class<?>>, INodeFunc> func : functions.entrySet()) {
List<Class<?>> functionClasses = func.getKey();
List<String> fnClassNames = functionClasses.stream().map(NodeTypes::getName).collect(Collectors.toList());
ExpressionDebugManager.debugPrintln("Found " + fnClassNames);
if (functionClasses.size() != functionOrder.size()) {
continue;
}
int casts = 0;
boolean canCast = true;
List<INodeFunc> casters = new ArrayList<>();
ExpressionDebugManager.debugStart("Finding casters...");
for (int i = 0; i < functionClasses.size(); i++) {
Class<?> from = functionOrder.get(i);
Class<?> to = functionClasses.get(i);
ExpressionDebugManager.debugPrintln(" - " + NodeTypes.getName(from) + " -> " + NodeTypes.getName(to));
if (from == to) {
casters.add(a -> a.pop(from));
ExpressionDebugManager.debugPrintln(" - Equal types, no cast needed.");
continue;
}
INodeFunc caster;
if (from == long.class && to == INodeLong.class) {
caster = new NodeFuncWrapper() {
@Override
public IExpressionNode getNode(INodeStack s) throws InvalidExpressionException {
return new NodeConstantObject<>(INodeLong.class, s.popLong());
}
};
} else if (from == double.class && to == INodeDouble.class) {
caster = new NodeFuncWrapper() {
@Override
public IExpressionNode getNode(INodeStack s) throws InvalidExpressionException {
return new NodeConstantObject<>(INodeDouble.class, s.popDouble());
}
};
} else if (from == long.class && to == INodeDouble.class) {
caster = new NodeFuncWrapper() {
@Override
public IExpressionNode getNode(INodeStack s) throws InvalidExpressionException {
INodeLong node = s.popLong();
INodeDouble nodeD = new NodeCastLongToDouble(node);
return new NodeConstantObject<>(INodeDouble.class, nodeD.inline());
}
};
} else if (from == boolean.class && to == INodeBoolean.class) {
caster = new NodeFuncWrapper() {
@Override
public IExpressionNode getNode(INodeStack s) throws InvalidExpressionException {
return new NodeConstantObject<>(INodeBoolean.class, s.popBoolean());
}
};
} else {
FunctionContext castingCtx = new FunctionContext(NodeTypes.getContext(from), NodeTypes.getContext(to));
caster = castingCtx.getFunction("(" + NodeTypes.getName(to) + ")", Collections.singletonList(from));
if (caster == null) {
ExpressionDebugManager.debugPrintln(" - No cast found!");
canCast = false;
break;
}
}
casts++;
casters.add(caster);
ExpressionDebugManager.debugPrintln(" - Caster = " + caster);
}
ExpressionDebugManager.debugEnd("");
if (!canCast) {
continue;
}
if (casts < bestCastCount) {
bestCastCount = casts;
bestFunction = func.getValue();
bestCasters = casters;
bestClassesTo = functionClasses;
}
}
ExpressionDebugManager.debugEnd("Best = " + bestFunction);
if (bestFunction == null || bestCasters == null) {
// Allow any object to be compared to itself with == and !=
boolean isEq = "==".equals(name);
boolean isNE = "!=".equals(name);
if (count == 2 && (isEq | isNE) && functionOrder.get(0) == functionOrder.get(1)) {
Class<?> cls = functionOrder.get(0);
NodeFuncObjectObjectToBoolean.IFuncObjectObjectToBoolean<?, ?> func = isEq ? Objects::equals : (a, b) -> !Objects.equals(a, b);
bestFunction = new NodeFuncObjectObjectToBoolean(name, cls, cls, func);
bestCastCount = 0;
bestCasters = Collections.emptyList();
bestClassesTo = new ArrayList<>(functionOrder);
ExpressionDebugManager.debugPrintln("Using implicit object equality comparison.");
} else {
throw new InvalidExpressionException("No viable function called '" + name + "' found for " + fnOrderNames + getValidFunctionsErrorString(context));
}
}
NodeStack realStack;
if (bestCastCount == 0) {
realStack = stack;
} else {
IExpressionNode[] nodes = new IExpressionNode[count];
for (int i = count - 1; i >= 0; i--) {
INodeFunc caster = bestCasters.get(i);
Class<?> from = functionOrder.get(i);
Class<?> to = bestClassesTo.get(i);
stack.setRecorder(Collections.singletonList(from), caster);
IExpressionNode node = caster.getNode(stack);
stack.checkAndRemoveRecorder();
Class<?> actual = NodeTypes.getType(node);
if (actual != to) {
throw new IllegalStateException("The caster " + caster + " didn't produce the correct result! (Expected " + to + ", but got " + actual + ")");
}
nodes[i] = node;
}
ExpressionDebugManager.debugPrintln("Nodes = " + nodes);
realStack = new NodeStack(nodes);
}
INodeFunc func = bestFunction;
bestClassesTo = new ArrayList<>(bestClassesTo);
Collections.reverse(bestClassesTo);
realStack.setRecorder(bestClassesTo, func);
IExpressionNode node = func.getNode(realStack);
realStack.checkAndRemoveRecorder();
stack.push(node);
}
use of buildcraft.lib.expression.api.IExpressionNode.INodeDouble 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.INodeDouble 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.INodeDouble 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)));
}
Aggregations