Package org.renjin.sexp

Examples of org.renjin.sexp.SEXP


  /* C version of the standardGeneric R function. */
  public SEXP R_standardGeneric(Context context, Symbol fsym, Environment ev, SEXP fdef) {
    String fname = fsym.getPrintName();
    Environment f_env = context.getGlobalEnvironment().getBaseEnvironment();
    SEXP mlist = Null.INSTANCE;
    SEXP f;
    SEXP val = Null.INSTANCE;
    int nprotect = 0;

    //    if(!initialized)
    //      R_initMethodDispatch(NULL);

    if(fdef instanceof Closure) {
      f_env = ((Closure) fdef).getEnclosingEnvironment();
      mlist = f_env.getVariable(".Methods");
      if(mlist == Symbol.UNBOUND_VALUE) {
        mlist = Null.INSTANCE;
      }
    } else if(fdef instanceof PrimitiveFunction ) {
      f_env = context.getBaseEnvironment();
      //mlist = R_primitive_methods((PrimitiveFunction)fdef);
      throw new UnsupportedOperationException();
    } else {
      throw new EvalException("invalid generic function object for method selection for function '%s': expected a function or a primitive, got an object of class \"%s\"",
          fsym.getPrintName(), fdef.getAttributes().getClassVector());
    }
    if(mlist instanceof Null || mlist instanceof Closure || mlist instanceof PrimitiveFunction) {
      f = mlist;
    } else {
      //f = do_dispatch(fname, ev, mlist, TRUE, TRUE);
      throw new UnsupportedOperationException();
    }
    if(f == Null.INSTANCE) {
      SEXP value = R_S_MethodsListSelect(context, StringArrayVector.valueOf(fname), ev, mlist, f_env);
      if(value == Null.INSTANCE) {
        throw new EvalException("no direct or inherited method for function '%s' for this call",
            fname);
      }
      mlist = value;
View Full Code Here


    found++; // we also have our fake __S4_BIt for renjin

    PairList attrib = def.getAttributes().asPairList();
    for(PairList.Node s : attrib.nodes()) {
      SEXP t = s.getTag();
      if(t == R_target) {
        ev.setVariable(R_dot_target, s.getValue());
        found++;
      }
      else if(t == R_defined) {
View Full Code Here

  }


  private SEXP do_dispatch(Context context, String fname, SEXP ev, SEXP mlist, boolean firstTry, boolean evalArgs) {
    String klass;
    SEXP arg_slot;
    Symbol arg_sym;
    SEXP method, value = Null.INSTANCE;
    int nprotect = 0;
    /* check for dispatch turned off inside MethodsListSelect */
    if(mlist instanceof Function) {
      return mlist;
    }
    arg_slot = Methods.R_do_slot(context, mlist, s_argument);
    if(arg_slot == Null.INSTANCE) {
      throw new EvalException("object of class \"%s\" used as methods list for function '%s' " +
          "( no 'argument' slot)",
          mlist.toString(), fname);
    }
    if(arg_slot instanceof Symbol) {
      arg_sym = (Symbol) arg_slot;
    } else {
      /* shouldn't happen, since argument in class MethodsList has class
       "name" */
      arg_sym = Symbol.get(arg_slot.asString());
    }
    //    if(arg_sym == Symbols.ELLIPSES || DDVAL(arg_sym) > 0)
    //  error(_("(in selecting a method for function '%s') '...' and related variables cannot be used for methods dispatch"),
    //        CHAR(asChar(fname)));
    //    if(TYPEOF(ev) != ENVSXP) {
    //  error(_("(in selecting a method for function '%s') the 'environment' argument for dispatch must be an R environment; got an object of class \"%s\""),
    //      CHAR(asChar(fname)), class_string(ev));
    //  return(R_NilValue); /* -Wall */
    //    }
    /* find the symbol in the frame, but don't use eval, yet, because
       missing arguments are ok & don't require defaults */
    if(evalArgs) {
      if(is_missing_arg(context, arg_sym, (Environment)ev)) {
        klass = "missing";
      } else {
        /*  get its class */
        SEXP arg, class_obj;
        try {
          arg = context.evaluate(arg_sym, (Environment)ev);
        } catch(EvalException e) {
          throw new EvalException(String.format("error in evaluating the argument '%s' in selecting a method for function '%s'",
              arg_sym.getPrintName(), fname), e);
        }

        class_obj = Methods.R_data_class(arg, true);
        klass = class_obj.asString();
      }
    } else {
      /* the arg contains the class as a string */
      SEXP arg; int check_err;
      try {
        arg = context.evaluate(arg_sym, (Environment)ev);
      } catch(Exception e) {
        throw new EvalException(String.format("error in evaluating the argument '%s' in selecting a method for function '%s'",
            arg_sym.getPrintName(), fname));
     
      klass = arg.asString();
    }
    method = R_find_method(mlist, klass, fname);
    if(method == Null.INSTANCE) {
      if(!firstTry) {
        throw new EvalException("no matching method for function '%s' (argument '%s', with class \"%s\")",
View Full Code Here

      if(!next.hasTag()) {
        throw new EvalException("closure formal has no tag! op = " + op);
      }
     
      Symbol symbol = next.getTag();
      SEXP val = rho.findVariable(symbol);
      if(val == Symbol.UNBOUND_VALUE) {
        throw new EvalException("could not find symbol \"%s\" in the environment of the generic function", symbol.getPrintName());
      }

      //      SET_FRAME(newrho, CONS(val, FRAME(newrho)));
      //      SET_TAG(FRAME(newrho), symbol);

      newrho.setVariable(symbol, val);

      //      if (missing) {
      //        SET_MISSING(FRAME(newrho), missing);
      //        if (TYPEOF(val) == PROMSXP && PRENV(val) == rho) {
      //          SEXP deflt;
      //          SET_PRENV(val, newrho);
      //          /* find the symbol in the method, copy its expression
      //           * to the promise */
      //          for(deflt = CAR(op); deflt != R_NilValue; deflt = CDR(deflt)) {
      //            if(TAG(deflt) == symbol)
      //              break;
      //          }
      //          if(deflt == R_NilValue)
      //            error(_("symbol \"%s\" not in environment of method"),
      //                CHAR(PRINTNAME(symbol)));
      //          SET_PRCODE(val, CAR(deflt));
      //        }
      //      }
    }

    /* copy the bindings of the spacial dispatch variables in the top
       frame of the generic call to the new frame */
    newrho.setVariable(DOT_DEFINED, rho.getVariable(DOT_DEFINED));
    newrho.setVariable(DOT_METHOD, rho.getVariable(DOT_METHOD));
    newrho.setVariable(DOT_TARGET, rho.getVariable(DOT_TARGET));

    /* copy the bindings for .Generic and .Methods.  We know (I think)
       that they are in the second frame, so we could use that. */
    newrho.setVariable(Symbols.GENERIC, newrho.getVariable(".Generic"));
    newrho.setVariable(DOT_METHODS, newrho.getVariable(DOT_METHODS));

    /* Find the calling context.  Should be R_GlobalContext unless
       profiling has inserted a CTXT_BUILTIN frame. */
    Context cptr = context;
    //    cptr = R_GlobalContext;
    //    if (cptr->callflag & CTXT_BUILTIN)
    //      cptr = cptr->nextcontext;

    /* The calling environment should either be the environment of the
       generic, rho, or the environment of the caller of the generic,
       the current sysparent. */
    Environment callerenv = cptr.getCallingEnvironment(); /* or rho? */

    /* get the rest of the stuff we need from the current context,
       execute the method, and return the result */
    FunctionCall call = cptr.getCall();
    PairList arglist = cptr.getArguments();
    SEXP val = R_execClosure(context, call, op, arglist, callerenv, newrho);
    return val;
  }
View Full Code Here

    return Calls.applyClosure(op, context, callerenv, call, arglist, newrho, new HashFrame());
  }
 
 
  private  SEXP do_inherited_table(Context context, SEXP class_objs, SEXP fdef, SEXP mtable, Environment ev) {
    SEXP fun = methodsNamespace.findFunction(context, Symbol.get(".InheritForDispatch"));
   
    return context.evaluate(FunctionCall.newCall(fun, class_objs, fdef, mtable), ev);
  }
View Full Code Here

public class IRProgramTest {

  @Test
  public void meanOnline() throws IOException {
   
    SEXP programExpression = RParser.parseSource(new InputStreamReader(getClass()
        .getResourceAsStream("/meanVarOnline.R")));
   
    ProgramCompiler compiler = new ProgramCompiler();
    compiler.compile(programExpression);
   
View Full Code Here

  //  assertThat( getValue( topLevelContext.getSession().baseNamespaceEnv, "letters" ).length(), equalTo( 26 ));

  }

  private SEXP getValue(Environment env, String name) {
    SEXP value = env.getVariable(name);
    if(value instanceof Promise) {
      value = value.force(topLevelContext);
    }
    return value;
  }
View Full Code Here

    assertThat( eval("f()"), equalTo(c_i(1)));

    eval("g<-function() eval(formals(sys.function(sys.parent()))[['event']]) ");
    eval("f<-function(event=c('a','b','c')) g() ");

    SEXP result = eval("f(1) ");
    assertThat(result, Matchers.equalTo(c("a", "b", "c")));
  }
View Full Code Here

        }
      }
    }


    SEXP exp = parser.getResult();
    if(parser.getResultStatus() == StatusResult.EOF) {
      return true;
    } else if(exp == null) {
      return true;
    }
   
    // clean up last warnings from any previous run
    clearWarnings();

    try {
      SEXP result = topLevelContext.evaluate(exp, topLevelContext.getGlobalEnvironment());

      if(!topLevelContext.getSession().isInvisible()) {
        topLevelContext.evaluate(FunctionCall.newCall(Symbol.get("print"), Promise.repromise(result)));
      }
View Full Code Here

    e.printRStackTrace(reader.getOutput());   
    reader.getOutput().flush();
  }

  private void printWarnings() {
    SEXP warnings = topLevelContext.getEnvironment().getBaseEnvironment().getVariable(Warning.LAST_WARNING);
    if(warnings != Symbol.UNBOUND_VALUE) {
      topLevelContext.evaluate( FunctionCall.newCall(Symbol.get("print.warnings"), warnings),
        topLevelContext.getEnvironment().getBaseEnvironment());
    }
  }
View Full Code Here

TOP

Related Classes of org.renjin.sexp.SEXP

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.