#include "../../include/rcore.h"
#include "cpq.common.h"

/* build an environment binding the columns of a batch of generated
 * observations, enclosed by the environment the query was called from (the
 * envir/enclos of eval(expr, data, eval.env)). The returned environment is
 * left PROTECTed for the caller to release. */
SEXP make_data_env(SEXP data, SEXP eval_env) {

int i = 0, nc = length(data);
SEXP env, names;

  PROTECT(env = R_NewEnv(eval_env, TRUE, nc));
  names = getAttrib(data, R_NamesSymbol);
  for (i = 0; i < nc; i++)
    defineVar(installChar(STRING_ELT(names, i)), VECTOR_ELT(data, i), env);

  return env;

}/*MAKE_DATA_ENV*/
