Exemplo n.º 1
0
    @Override public RNode create(ASTNode call, RSymbol[] names, RNode[] exprs) {
        ArgumentInfo ia = check(call, names, exprs);
        boolean product = false;
        if (ia.provided("FUN")) {
            RNode fnode = exprs[ia.position("FUN")];
            if (fnode instanceof Constant) {
                RAny value = ((Constant) fnode).execute(null);
                if (value instanceof RString) {
                    RString str = (RString) value;
                    if (str.size() == 1) {
                        if (str.getString(0).equals("*")) {
                            product = true;
                        }
                    }
                }
            }
        } else {
            product = true;
        }
        if (product) { return new MatrixOperation.OuterProduct(call, exprs[ia.position("X")], exprs[ia.position("Y")]); }

        int cnArgs = 2 + names.length - 3; // "-2" because both FUN, X, Y
        RSymbol[] cnNames = new RSymbol[cnArgs];
        RNode[] cnExprs = new RNode[cnArgs];
        cnNames[0] = null;
        ValueProvider xArgProvider = new ValueProvider(call);
        cnExprs[0] = xArgProvider;
        ValueProvider yArgProvider = new ValueProvider(call);
        cnExprs[1] = yArgProvider;
        int j = 0;
        for (int i = 0; i < names.length; i++) {
            if (ia.position("X") == i || ia.position("Y") == i || ia.position("FUN") == i) {
                continue;
            }
            cnNames[2 + j] = names[i];
            cnExprs[2 + j] = exprs[i];
            j++;
        }

        final CallableProvider callableProvider = new CallableProvider(call, exprs[ia.position("FUN")]);
        final FunctionCall callNode = FunctionCall.getFunctionCall(call, callableProvider, cnNames, cnExprs);
        final int posX = ia.position("X");
        final int posY = ia.position("Y");
        final int posFUN = ia.position("FUN");
        return new OuterBuiltIn(call, names, exprs, callNode, callableProvider, xArgProvider, yArgProvider) {
            @Override public RAny doBuiltIn(Frame frame, RAny[] args) {
                return outer(frame, args[posX], args[posY], args[posFUN]);
            }
        };
    }