예제 #1
0
파일: Outer.java 프로젝트: srisatish/fastr
    @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]);
            }
        };
    }
예제 #2
0
    @Override
    public RNode create(ASTNode call, RSymbol[] names, RNode[] exprs) {
        ArgumentInfo ia = check(call, names, exprs);
        final int posExpr = ia.position("expr");

        return new Builtin(call, names, exprs) {

            @Override
            public RAny doBuiltIn(Frame frame, RAny[] args) {
                return RString.RStringFactory.getScalar(AsBase.deparse(args[posExpr], false));
            }

        };
    }
예제 #3
0
    @Override public RNode create(ASTNode call, RSymbol[] names, RNode[] exprs) {
        final ArgumentInfo ia = check(call, names, exprs);
        final int posText = ia.position("text");
        final int posLast = ia.position("last");
        final int posFirst = ia.position("first");
        final RDouble defaultLast = RDouble.RDoubleFactory.getScalar(1000000); // FIXME slow, but perhaps the default is not used, anyway
        return new Builtin(call, names, exprs) {
            @Override public RAny doBuiltIn(Frame frame, RAny[] args) {
                RString text = args[posText].asString();
                warn.naIntroduced = false;
                RDouble first = args[posFirst].asDouble(warn);
                RDouble last;
                last = posLast != -1 ? args[posLast].asDouble(warn) : defaultLast;

                RString res = substring(text, first, last, ast);
                if (warn.naIntroduced) {
                    RContext.warning(ast, RError.NA_INTRODUCED_COERCION);
                }
                return res;
            }
        };
    }
예제 #4
0
 private static ControlInfo parseFunction(String string, PArray array) throws Exception {
   // array(1) is inputs
   PArray args = PArray.coerce(array.get(1));
   ArgumentInfo[] inputs = new ArgumentInfo[args.getSize()];
   for (int i = 0; i < inputs.length; i++) {
     inputs[i] = ArgumentInfo.coerce(args.get(i));
   }
   // array(2) is outputs
   args = PArray.coerce(array.get(2));
   ArgumentInfo[] outputs = new ArgumentInfo[args.getSize()];
   for (int i = 0; i < outputs.length; i++) {
     outputs[i] = ArgumentInfo.coerce(args.get(i));
   }
   // optional array(3) is properties
   PMap properties;
   if (array.getSize() > 3) {
     properties = PMap.coerce(array.get(3));
   } else {
     properties = PMap.EMPTY;
   }
   return new ControlInfo(inputs, outputs, EMPTY_DEFAULTS, Type.Function, properties, string);
 }
예제 #5
0
 static OptionInfo createFromField(Field field) {
   Preconditions.checkNotNull(field);
   CmdLine cmdLine = field.getAnnotation(CmdLine.class);
   if (cmdLine == null) {
     throw new Configuration.ConfigurationException(
         "No @CmdLine Arg annotation for field " + field);
   }
   @SuppressWarnings("unchecked")
   OptionInfo optionInfo =
       new OptionInfo(
           checkValidName(cmdLine.name()),
           cmdLine.help(),
           ArgumentInfo.getArgForField(field),
           TypeUtil.getTypeParamTypeToken(field),
           field.getDeclaringClass().getCanonicalName(),
           Arrays.asList(field.getAnnotations()),
           cmdLine.parser());
   return optionInfo;
 }
예제 #6
0
 private static ControlInfo parseProperty(String string, Type type, PArray array)
     throws Exception {
   // array(1) is outputs
   PArray args = PArray.coerce(array.get(1));
   ArgumentInfo[] outputs = new ArgumentInfo[args.getSize()];
   for (int i = 0; i < outputs.length; i++) {
     outputs[i] = ArgumentInfo.coerce(args.get(i));
   }
   ArgumentInfo[] inputs = type == Type.ReadOnlyProperty ? EMPTY_INFO : outputs;
   // array(2) is defaults
   args = PArray.coerce(array.get(2));
   Argument[] defs = new Argument[args.getSize()];
   for (int i = 0; i < defs.length; i++) {
     defs[i] = PString.coerce(args.get(i));
   }
   // optional array(3) is properties
   PMap properties;
   if (array.getSize() > 3) {
     properties = PMap.coerce(array.get(3));
   } else {
     properties = PMap.EMPTY;
   }
   return new ControlInfo(inputs, outputs, defs, type, properties, string);
 }