Skip to content

Instantly share code, notes, and snippets.

@romainfrancois
Last active December 25, 2015 13:09
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save romainfrancois/6981372 to your computer and use it in GitHub Desktop.
Reading the internal function table
#include <Rcpp.h>
using namespace Rcpp;
typedef SEXP (*CCODE)(SEXP, SEXP, SEXP, SEXP);
/* Information for Deparsing Expressions */
typedef enum {
PP_INVALID = 0,
PP_ASSIGN = 1,
PP_ASSIGN2 = 2,
PP_BINARY = 3,
PP_BINARY2 = 4,
PP_BREAK = 5,
PP_CURLY = 6,
PP_FOR = 7,
PP_FUNCALL = 8,
PP_FUNCTION = 9,
PP_IF = 10,
PP_NEXT = 11,
PP_PAREN = 12,
PP_RETURN = 13,
PP_SUBASS = 14,
PP_SUBSET = 15,
PP_WHILE = 16,
PP_UNARY = 17,
PP_DOLLAR = 18,
PP_FOREIGN = 19,
PP_REPEAT = 20
} PPkind;
typedef enum {
PREC_FN = 0,
PREC_LEFT = 1,
PREC_EQ = 2,
PREC_RIGHT = 3,
PREC_TILDE = 4,
PREC_OR = 5,
PREC_AND = 6,
PREC_NOT = 7,
PREC_COMPARE = 8,
PREC_SUM = 9,
PREC_PROD = 10,
PREC_PERCENT = 11,
PREC_COLON = 12,
PREC_SIGN = 13,
PREC_POWER = 14,
PREC_DOLLAR = 15,
PREC_NS = 16,
PREC_SUBSET = 17
} PPprec;
typedef struct {
PPkind kind; /* deparse kind */
PPprec precedence; /* operator precedence */
unsigned int rightassoc; /* right associative? */
} PPinfo;
typedef struct {
char *name; /* print name */
CCODE cfun; /* c-code address */
int code; /* offset within c-code */
int eval; /* evaluate args? */
int arity; /* function arity */
PPinfo gram; /* pretty-print info */
} FUNTAB;
extern FUNTAB R_FunTab[]; /* Built in functions */
CCODE get_internal( std::string name){
FUNTAB* p = R_FunTab ;
for( ; p->name != NULL; ++p ){
Rprintf( "%s\n", p->name ) ;
if( name == p->name )
return p->cfun ;
}
return NULL ;
}
// [[Rcpp::export]]
SEXP internal_rank( SEXP x){
CCODE rank_fun = get_internal( "rank" );
Language call( "rank", x, Rf_length(x), "average" ) ;
return rank_fun(call, Rf_ScalarInteger(0), CDR(call), R_GlobalEnv ) ;
}
/*** R
internal_rank(rnorm(10))
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment