Ejemplo n.º 1
0
void StreamTimerListe::Serialize( Json::Value& root )
{
   // serialize primitives
	unsigned int size = LD_StreamListe.size();
	//Json::Value timer_value(Json::arrayValue); // []
	//arr_value.append("Test1");
	//arr_value.append("Test2");
	Json::Value obj_value(Json::objectValue);
	for(unsigned int i=0; i<size; i++)
	{
		obj_value["STREAM"]["LDNAME"] = LD_StreamListe[i].getLdName();
		obj_value["STREAM"]["LDNUMBER"] = LD_StreamListe[i].getLdNumber();
		obj_value["STREAM"]["LDI2CCHANNEL"] = LD_StreamListe[i].getLdI2cChannel();
		for (int ii=0;ii<TIMERSTORECOUNT;ii++)
		{
			obj_value["STREAM"]["LDTIMEARRAY"].append(LD_StreamListe[i].getLdTimeArray()[ii]);
		}
		obj_value["STREAM"]["CHARTCOLOR"]=  LD_StreamListe[i].getChartColor();
		root["StreamListe"].append(obj_value);
		obj_value.clear();
		obj_value["STREAM"]["LDTIMEARRAY"].clear();
	}
	// version tag into stream
	root["version"] = c_fileversion;
}
Ejemplo n.º 2
0
struct object *
pic_obj_alloc(pic_state *pic, int type)
{
  struct object *obj;

  obj = pic_obj_alloc_unsafe(pic, type);

  pic_protect(pic, obj_value(pic, obj));
  return obj;
}
Ejemplo n.º 3
0
int queryType(char *http_payload) {
    debug("Find out query type");
    if (http_payload == NULL) {
        return -1;
    }
    std::string http_payload_str(http_payload);
    Json::Reader reader = Json::Reader();
    size_t pLastKey, pValue, pNextKey;
    std::map<std::string, std::string> content;

    pLastKey = 0;
    while (true) {
        pValue = http_payload_str.find('=', pLastKey);
        pNextKey = http_payload_str.find('&', pValue);
        std::string key = http_payload_str.substr(pLastKey, pValue - pLastKey);
        std::string value = http_payload_str.substr(pValue + 1, pNextKey == std::string::npos ? pNextKey : pNextKey - pValue -1);
        content.emplace(key, urlDecode(value));
        if (pNextKey == std::string::npos) break;
        pLastKey = pNextKey + 1;
    }
    std::string &query_str = content.at("query");

    std::unique_ptr<Json::Value> root (new Json::Value);
    if (reader.parse(query_str, (*root)) == false) {
        log_err("Error parsing json: %s", reader.getFormattedErrorMessages().c_str());
        debug("HTTP payload was %s", http_payload);
        return -1;
    }
    std::unique_ptr<Json::Value> query = std::move(root);


    Json::Value operators;
    Json::Value obj_value(Json::objectValue);

    if (!query->isObject() || query->isMember("operators") == false) {
        log_err("query content does not contain any operators");
        throw "query content does not contain any operators";
    }
    operators = query->get("operators", obj_value);

    for (auto op: operators) {
        auto type = op.get("type", "").asString();
        if (type == "InsertScan" or
            type == "Delete" or
            type == "PosUpdateIncrementScan" or
            type == "PosUpdateScan") {
                return WRITE;
        }
        if (type == "TableLoad") {
            return LOAD;
        }
    }
    return READ;
}
Ejemplo n.º 4
0
pic_value
pic_data_value(pic_state *pic, void *userdata, const pic_data_type *type)
{
  struct data *data;

  data = (struct data *)pic_obj_alloc(pic, PIC_TYPE_DATA);
  data->type = type;
  data->data = userdata;

  return obj_value(pic, data);
}
Ejemplo n.º 5
0
pic_value
pic_blob_value(pic_state *pic, const unsigned char *buf, int len)
{
  struct blob *bv;

  bv = (struct blob *)pic_obj_alloc(pic, PIC_TYPE_BLOB);
  bv->data = pic_malloc(pic, len);
  bv->len = len;
  if (buf) {
    memcpy(bv->data, buf, len);
  }
  return obj_value(pic, bv);
}
int RoundRobinDistributor::parseQuery(std::unique_ptr<Json::Value> query) {
    bool writeQuery = false;
    Json::Value operators;
    Json::Value obj_value(Json::objectValue);

    if (!query->isObject() || query->isMember("operators") == false) {
        std::cerr << "query content does not contain any operators";
        return -1;
    }
    operators = query->get("operators", obj_value);

    for (auto op: operators) {
        auto type = op.get("type", "").asString();
        if (type == "InsertScan" or type == "Delete" or type == "PosUpdateIncrementScan" or type == "PosUpdateScan") writeQuery = true;
        if (type == "TableLoad") return 2;
    }

    if (writeQuery) return 1;

    return 0;
}
Ejemplo n.º 7
0
void StreamTimerListe::SerializeAjax( Json::Value& root )
{
   // serialize primitives
	int size = LD_StreamListe.size();

	Json::Value timer_value(Json::arrayValue); // []
	Json::Value row(Json::arrayValue);
	Json::Value rowitem(Json::arrayValue);
	Json::Value rowdata(Json::arrayValue);
	Json::Value ledvalues(Json::arrayValue);
	Json::Value zeile(Json::objectValue);
	//arr_value.append("Test1");
	//arr_value.append("Test2");
	Json::Value obj_value(Json::objectValue);
	for(int i=0; i<size; i++)
	{
		for (int ii=0;ii<TIMERSTORECOUNT;ii++)
		{
			ledvalues.append(LD_StreamListe[i].getLdTimeArray()[ii]);
		}
		zeile["id"]   = LD_StreamListe[i].getLdNumber();
		rowitem.append(LD_StreamListe[i].getLdNumber());
		rowitem.append(LD_StreamListe[i].getLdName());
		rowitem.append(LD_StreamListe[i].getLdI2cChannel());
		rowitem.append(ledvalues);
		rowitem.append(LD_StreamListe[i].getChartColor());
		zeile["cell"] = rowitem;

		root["StreamListe"].append(zeile);
		ledvalues.clear();
		rowitem.clear();
	}
	// version tag into stream
	root["version"] = "2.0";
	root["fooddelay"] = FOOD_DELAY;								// Time in Sec that the streams are in food mode
	root["foodpower"] = FOOD_MIN_POWER;
}
Ejemplo n.º 8
0
//' @title Master Wrapper for the GMWM Estimator
//' @description This function generates WV, GMWM Estimator, and an initial test estimate.
//' @param data A \code{vec} containing the data.
//' @param theta A \code{vec} with dimensions N x 1 that contains user-supplied initial values for parameters
//' @param desc A \code{vector<string>} indicating the models that should be considered.
//' @param objdesc A \code{field<vec>} containing a list of parameters (e.g. AR(1) = c(1,1), ARMA(p,q) = c(p,q,1))
//' @param model_type A \code{string} that represents the model transformation
//' @param starting A \code{bool} that indicates whether the supplied values are guessed (T) or are user-based (F).
//' @param alpha A \code{double} that handles the alpha level of the confidence interval (1-alpha)*100
//' @param compute_v A \code{string} that describes what kind of covariance matrix should be computed.
//' @param K An \code{int} that controls how many times theta is updated.
//' @param H An \code{int} that controls how many bootstrap replications are done.
//' @param G An \code{int} that controls how many guesses at different parameters are made.
//' @param robust A \code{bool} that indicates whether the estimation should be robust or not.
//' @param eff A \code{double} that specifies the amount of efficiency required by the robust estimator.
//' @return A \code{field<mat>} that contains a list of ever-changing estimates...
//' @author JJB
//' @references Wavelet variance based estimation for composite stochastic processes, S. Guerrier and Robust Inference for Time Series Models: a Wavelet-Based Framework, S. Guerrier
//' @keywords internal
//' @export
//' @backref src/gmwm_logic.cpp
//' @backref src/gmwm_logic.h
// [[Rcpp::export]]
arma::field<arma::mat> gmwm_master_cpp(arma::vec& data, 
                                      arma::vec theta,
                                      const std::vector<std::string>& desc, const arma::field<arma::vec>& objdesc, 
                                      std::string model_type, bool starting,
                                      double alpha, 
                                      std::string compute_v, unsigned int K, unsigned int H,
                                      unsigned int G, 
                                      bool robust, double eff){
  
  // Obtain counts of the different models we need to work with
  std::map<std::string, int> models = count_models(desc);
  
  // HACK METHOD (todo: formalize it)
  // Determine if we need to difference
  if(models["SARIMA"] > 0){
    
    // Note: s, i, si are 6,7,8 => 5,6,7
    for(unsigned int i = 0; i < desc.size(); i ++){
      if(objdesc(i).n_elem > 3){
        arma::vec sarima_desc = objdesc(i);
        // Do we need to difference? 
        if(sarima_desc(6) > 0 || sarima_desc(7) > 0){
          // Perform differencing in specific order...
          // First non-seasonal and then seasonal.
          
          if(sarima_desc(6) > 0){
            // Lag is always 1, number of differences is (i)
            data = diff_cpp(data, 1, sarima_desc(6));
          }
          
          if(sarima_desc(7) > 0){
            // Lag is always seasonality (s) and differences is (si).
            data = diff_cpp(data, sarima_desc(5), sarima_desc(7));
          }
          
          // Kill loop. We only handle the tsmodel object with the first difference.
          break;
        }
      }
    }
  }
  
  // ------ Variable Declarations
  
  // Length of the Time Series
  unsigned int N = data.n_elem;
  
  // Number of Scales (J)
  unsigned int nlevels = floor(log2(N));
  
  // Number of parameters
  unsigned int np = theta.n_elem;
  
  // Take the mean of the first difference
  double expect_diff = mean_diff(data);
  
  // Guessed values of Theta (user supplied or generated)
  arma::vec guessed_theta = theta;
  
  // MODWT decomp
  arma::field<arma::vec> modwt_decomp = modwt_cpp(data, "haar", nlevels, "periodic", true);
  
  // Obtain WV and confidence intervals
  arma::mat wvar = wvar_cpp(modwt_decomp, robust, eff, alpha, "eta3");
  
  // Extract
  arma::vec wv_empir = wvar.col(0);
  arma::vec ci_lo = wvar.col(1);
  arma::vec ci_hi = wvar.col(2);
  
  //-------------------------
  // Obtain Covariance Matrix
  //-------------------------
  
  arma::mat V;
  
  // compute_cov_cpp is the hard core function. It can only be improved by using parallelization.
  if(compute_v == "diag" || compute_v == "full"){
    arma::field<arma::mat> Vout = compute_cov_cpp(modwt_decomp, nlevels, compute_v, robust, eff);
    if(robust){
      V = Vout(1);
    }else{
      V = Vout(0);
    }
  }else{
     V = fast_cov_cpp(wvar.col(2), wvar.col(1));
  }

  // Obtain the Omega matrix
  arma::mat omega = arma::inv(diagmat(V));
  
  // Store the original V matrix (in case of bootstrapping) for use in the update function
  arma::mat orgV = V;
  
  // Calculate the values of the Scales 
  arma::vec scales = scales_cpp(nlevels);
  
  // Min-Max / N
  double ranged = dr_slope(data);
  
  // Guess starting values for the theta parameters
  if(starting){
    
    // Always run guessing algorithm
    theta = guess_initial(desc, objdesc, model_type, np, expect_diff, N, wvar, scales, ranged, G);
    
    // If under ARMA case and only ARMA is in the model, 
    // then see how well these values are.
    if(desc.size() == 1 && (desc[0] == "SARIMA" || desc[0] == "ARMA11") && N <= 1000){
      
      // Use R's ARIMA function to estimate parameter space
      arma::vec theta2 = Rcpp_ARIMA(data, objdesc(0)); // Only 1 objdesc in the available.
      
      // Obtain the obj function under omega with these initial guesses
      // DO >>NOT<< USE Yannick's to optimize starting values!!!!
      double mle_css_obj = getObjFun(theta2, desc, objdesc,  model_type, omega, wv_empir, scales); 
      
      // Obtain the objective function under Yannick's starting algorithm
      double init_guess_obj = getObjFunStarting(theta, desc, objdesc,  model_type, wv_empir, scales);
      
      // What performs better? 
      if(mle_css_obj < init_guess_obj){
        // Disable starting value optimization if using MLE. 
        theta = theta2;
        starting = false;
      }

    }
    
    guessed_theta = theta;
  }
  
  // Obtain the GMWM estimator's estimates.
  theta = gmwm_engine(theta, desc, objdesc, model_type, 
                      wv_empir, omega, scales, starting);
  
  // Optim may return a very small value. In this case, instead of saying its zero (yielding a transform issue), make it EPSILON.
  theta = code_zero(theta);
  
  // Enable bootstrapping
  if(compute_v == "bootstrap"){
    for(unsigned int k = 0; k < K; k++){
        // Create the full V matrix
        V = cov_bootstrapper(theta, desc, objdesc, N, robust, eff, H, false);
      
        // Update the omega matrix
        omega = arma::inv(diagmat(V));
        
        // The theta update in this case MUST not use Yannick's starting algorithm. Hence, the false value.
        theta = gmwm_engine(theta, desc, objdesc, model_type, wv_empir, omega, scales, false);
        
        // Optim may return a very small value. In this case, instead of saying its zero (yielding a transform issue), make it EPSILON.
        theta = code_zero(theta);
    }
  }

  
  if(desc[0] == "SARIMA" && desc.size() == 1){
    
    arma::vec temp = objdesc(0);
    unsigned int p = temp(0);
    if(p != 0 && invert_check(arma::join_cols(arma::ones<arma::vec>(1), -theta.rows(0, p - 1))) == false){
      Rcpp::Rcout << "WARNING: This ARMA model contains AR coefficients that are NON-STATIONARY!" << std::endl;
    }
  } 
  
  // Order AR1s / GM so largest phi is first!
   if(models["AR1"] > 1 || models["GM"] > 1){
     theta = order_AR1s(theta, desc, objdesc);
   }

  // Obtain the objective value function
  arma::vec obj_value(1);
  obj_value(0) = getObjFun(theta, desc, objdesc,  model_type, omega, wv_empir, scales); 
  
  arma::vec dr_s(1);
  dr_s(0) = ranged;
  
  // Decomposition of the WV.
  arma::mat decomp_theo = decomp_theoretical_wv(theta, desc, objdesc, scales);
  arma::vec theo = decomp_to_theo_wv(decomp_theo);

  // Export information back
  arma::field<arma::mat> out(13);
  out(0) = theta;
  out(1) = guessed_theta;
  out(2) = wv_empir;
  out(3) = ci_lo;
  out(4) = ci_hi;
  out(5) = V;
  out(6) = orgV;
  out(7) = expect_diff;
  out(8) = theo;
  out(9) = decomp_theo;
  out(10) = obj_value;
  out(11) = omega;
  out(12) = dr_s;
  return out;
}
Ejemplo n.º 9
0
//' @title Update Wrapper for the GMWM Estimator
//' @description This function uses information obtained previously (e.g. WV covariance matrix) to re-estimate a different model parameterization
//' @param theta A \code{vec} with dimensions N x 1 that contains user-supplied initial values for parameters
//' @param desc A \code{vector<string>} indicating the models that should be considered.
//' @param objdesc A \code{field<vec>} containing a list of parameters (e.g. AR(1) = c(1,1), ARMA(p,q) = c(p,q,1))
//' @param model_type A \code{string} that represents the model transformation
//' @param wv_empir A \code{vec} that contains the empirical wavelet variance
//' @param omega A \code{mat} that represents the covariance matrix.
//' @param scales A \code{vec} that contains the scales or taus (2^(1:J))
//' @param starting A \code{bool} that indicates whether we guessed starting (T) or the user supplied estimates (F).
//' @return A \code{field<mat>} that contains the parameter estimates from GMWM estimator.
//' @author JJB
//' @references Wavelet variance based estimation for composite stochastic processes, S. Guerrier and Robust Inference for Time Series Models: a Wavelet-Based Framework, S. Guerrier
//' @keywords internal
//' @backref src/gmwm_logic.cpp
//' @backref src/gmwm_logic.h
// [[Rcpp::export]]
arma::field<arma::mat> gmwm_update_cpp(arma::vec theta,
                                      const std::vector<std::string>& desc, const arma::field<arma::vec>& objdesc, 
                                      std::string model_type, unsigned int N, double expect_diff, double ranged, 
                                      const arma::mat& orgV, const arma::vec& scales, const arma::mat& wv,
                                      bool starting, 
                                      std::string compute_v, unsigned int K, unsigned int H,
                                      unsigned int G, 
                                      bool robust, double eff){
  
  // Number of parameters
  unsigned int np = theta.n_elem;
    
  // Guessed Values
  arma::vec guessed_theta = theta;
  
  // V matrix
  arma::mat V = orgV;
  
  // Diagonal Omega Matrix
  arma::mat omega = arma::inv(diagmat(V));
  
  arma::vec wv_empir = wv.col(0);
  
  // Do we need to run a guessing algorithm?
  if(starting){

    theta = guess_initial(desc, objdesc, model_type, np, expect_diff, N, wv, scales, ranged, G);
    
    guessed_theta = theta;
  }

  // Obtain the GMWM estimator estimates.
  theta = gmwm_engine(theta, desc, objdesc, model_type, 
                      wv_empir, omega, scales, starting);

  theta = code_zero(theta);
    
  // Bootstrap the V matrix
  if(compute_v == "bootstrap"){
    for(unsigned int k = 0; k < K; k++){
        // False here means we create the "full V" matrix
        V = cov_bootstrapper(theta, desc, objdesc, N, robust, eff, H, false);
        omega = arma::inv(diagmat(V));
        
        // The theta update in this case MUST not use Yannick's starting algorithm. Hence, the false value.
        theta = gmwm_engine(theta, desc, objdesc, model_type, wv_empir, omega, scales, false);
        
        // Optim may return a very small value. In this case, instead of saying its zero (yielding a transform issue), make it EPSILON.
        theta = code_zero(theta);
    }
  }
  
  
  std::map<std::string, int> models = count_models(desc);
  
  // Order AR1s so largest phi is first!
  if(models["AR1"] > 1  || models["GM"] > 1){
    theta = order_AR1s(theta, desc, objdesc);
  }
  
  // Obtain the theoretical WV.
  arma::mat decomp_theo = decomp_theoretical_wv(theta, desc, objdesc, scales);
  arma::vec theo = decomp_to_theo_wv(decomp_theo);
  
  // Obtain the objective value function
  arma::vec obj_value(1);
  obj_value(0) = getObjFun(theta, desc, objdesc,  model_type, omega, wv_empir, scales); 
  
  // Export calculations to R.
  arma::field<arma::mat> out(6);
  out(0) = theta;
  out(1) = guessed_theta;
  out(2) = V;
  out(3) = theo;
  out(4) = decomp_theo;
  out(5) = obj_value;
  
  return out;
                                        
}
Ejemplo n.º 10
0
static void
gc_finalize_object(pic_state *pic, struct object *obj)
{
  switch (obj_type(pic, obj)) {
  case PIC_TYPE_VECTOR: {
    pic_free(pic, obj->u.vec.data);
    break;
  }
  case PIC_TYPE_BLOB: {
    pic_free(pic, obj->u.blob.data);
    break;
  }
  case PIC_TYPE_STRING: {
    pic_rope_decref(pic, obj->u.str.rope);
    break;
  }
  case PIC_TYPE_DATA: {
    if (obj->u.data.type->dtor) {
      obj->u.data.type->dtor(pic, obj->u.data.data);
    }
    break;
  }
  case PIC_TYPE_DICT: {
    kh_destroy(dict, &obj->u.dict.hash);
    break;
  }
  case PIC_TYPE_SYMBOL: {
    /* TODO: remove this symbol's entry from pic->syms immediately */
    break;
  }
  case PIC_TYPE_WEAK: {
    kh_destroy(weak, &obj->u.weak.hash);
    break;
  }
  case PIC_TYPE_IREP: {
    struct irep *irep = &obj->u.irep;
    if ((irep->flags & IREP_CODE_STATIC) == 0) {
      pic_free(pic, (code_t *) irep->code);
    }
    pic_free(pic, irep->obj);
    pic_free(pic, irep->irep);
    break;
  }
  case PIC_TYPE_PORT: {
    pic_fclose(pic, obj_value(pic, obj)); /* FIXME */
    break;
  }
  case PIC_TYPE_FRAME: {
    pic_free(pic, obj->u.frame.regs);
    break;
  }

  case PIC_TYPE_PAIR:
  case PIC_TYPE_ERROR:
  case PIC_TYPE_RECORD:
  case PIC_TYPE_PROC_FUNC:
  case PIC_TYPE_PROC_IREP:
    break;

  default:
    PIC_UNREACHABLE();
  }
}
Ejemplo n.º 11
0
static void
gc_mark_object(pic_state *pic, struct object *obj)
{
 loop:

  if (is_alive(obj))
    return;

  mark(obj);

#define LOOP(o) obj = (struct object *)(o); goto loop

  switch (obj_type(pic, obj)) {
  case PIC_TYPE_PAIR: {
    gc_mark(pic, obj->u.pair.car);
    if (obj_p(pic, obj->u.pair.cdr)) {
      LOOP(obj_ptr(pic, obj->u.pair.cdr));
    }
    break;
  }
  case PIC_TYPE_FRAME: {
    int i;

    for (i = 0; i < obj->u.frame.regc; ++i) {
      gc_mark(pic, obj->u.frame.regs[i]);
    }
    if (obj->u.frame.up) {
      LOOP(obj->u.frame.up);
    }
    break;
  }
  case PIC_TYPE_PROC_FUNC: {
    if (obj->u.proc.env) {
      LOOP(obj->u.proc.env);
    }
    break;
  }
  case PIC_TYPE_PROC_IREP: {
    if (obj->u.proc.env) {
      gc_mark_object(pic, (struct object *)obj->u.proc.env);
    }
    LOOP(obj->u.proc.u.irep);
    break;
  }
  case PIC_TYPE_IREP: {
    size_t i;
    for (i = 0; i < obj->u.irep.objc; ++i) {
      gc_mark(pic, obj->u.irep.obj[i]);
    }
    for (i = 0; i < obj->u.irep.irepc; ++i) {
      gc_mark_object(pic, (struct object *)obj->u.irep.irep[i]);
    }
    break;
  }
  case PIC_TYPE_PORT: {
    break;
  }
  case PIC_TYPE_ERROR: {
    gc_mark_object(pic, (struct object *)obj->u.err.type);
    gc_mark(pic, obj->u.err.irrs);
    LOOP(obj->u.err.msg);
    break;
  }
  case PIC_TYPE_STRING: {
    break;
  }
  case PIC_TYPE_VECTOR: {
    int i;
    for (i = 0; i < obj->u.vec.len; ++i) {
      gc_mark(pic, obj->u.vec.data[i]);
    }
    break;
  }
  case PIC_TYPE_BLOB: {
    break;
  }
  case PIC_TYPE_DATA: {
    break;
  }
  case PIC_TYPE_DICT: {
    pic_value key, val;
    int it = 0;

    while (pic_dict_next(pic, obj_value(pic, &obj->u.dict), &it, &key, &val)) {
      gc_mark(pic, key);
      gc_mark(pic, val);
    }
    break;
  }
  case PIC_TYPE_RECORD: {
    gc_mark(pic, obj->u.rec.datum);
    LOOP(obj->u.rec.type);
    break;
  }
  case PIC_TYPE_SYMBOL: {
    LOOP(obj->u.sym.str);
    break;
  }
  case PIC_TYPE_WEAK: {
    struct weak *weak = (struct weak *)obj;

    weak->prev = pic->heap->weaks;
    pic->heap->weaks = weak;
    break;
  }
  default:
    PIC_UNREACHABLE();
  }
}
Ejemplo n.º 12
0
pic_state *
pic_open(pic_allocf allocf, void *userdata)
{
  pic_state *pic;

  pic = allocf(userdata, NULL, sizeof(pic_state));

  if (! pic) {
    goto EXIT_PIC;
  }

  /* allocator */
  pic->allocf = allocf;

  /* user data */
  pic->userdata = userdata;

  /* context */
  pic->default_cxt.ai = 0;
  pic->default_cxt.pc = NULL;
  pic->default_cxt.fp = NULL;
  pic->default_cxt.sp = NULL;
  pic->default_cxt.irep = NULL;
  pic->default_cxt.prev = NULL;
  pic->cxt = &pic->default_cxt;

  /* arena */
  pic->arena = allocf(userdata, NULL, PIC_ARENA_SIZE * sizeof(struct object *));
  pic->arena_size = PIC_ARENA_SIZE;
  pic->ai = 0;

  if (! pic->arena) {
    goto EXIT_ARENA;
  }

  /* turn off GC */
  pic->gc_enable = false;

  /* memory heap */
  pic->heap = pic_heap_open(pic);

  /* symbol table */
  kh_init(oblist, &pic->oblist);

  /* global variables */
  pic->globals = pic_make_dict(pic);

  /* features */
  pic->features = pic_nil_value(pic);

  /* dynamic environment */
  pic->dyn_env = pic_list(pic, 1, pic_make_weak(pic));

  /* top continuation */
  {
    static const code_t halt_code[] = { 0x00 };
    struct irep *irep;
    struct proc *proc;
    irep = (struct irep *)pic_obj_alloc(pic, PIC_TYPE_IREP);
    irep->argc = 1;
    irep->flags = IREP_CODE_STATIC;
    irep->frame_size = 1;
    irep->irepc = 0;
    irep->objc = 0;
    irep->irep = NULL;
    irep->obj = NULL;
    irep->code = halt_code;
    irep->codec = sizeof halt_code / sizeof halt_code[0];
    proc = (struct proc *)pic_obj_alloc(pic, PIC_TYPE_PROC_IREP);
    proc->u.irep = irep;
    proc->env = NULL;
    pic->halt = obj_value(pic, proc);
  }

  /* panic handler */
  pic->panicf = NULL;

  /* turn on GC */
  pic->gc_enable = true;

  pic_init_core(pic);

  pic_leave(pic, 0);            /* empty arena */

  return pic;

 EXIT_ARENA:
  allocf(userdata, pic, 0);
 EXIT_PIC:
  return NULL;
}
Ejemplo n.º 13
0
/*
 * Describe the kind
 */
static void kind_info(char *buf, char *dam, char *wgt, int *lev, s32b *val, int k)
{
    object_type forge;
    object_type *q_ptr;


    /* Get local object */
    q_ptr = &forge;

    /* Prepare a fake item */
    object_prep(q_ptr, k);

    /* It is known */
    q_ptr->ident |= (IDENT_KNOWN);

    /* Cancel bonuses */
    q_ptr->pval = 0;
    q_ptr->to_a = 0;
    q_ptr->to_h = 0;
    q_ptr->to_d = 0;


    /* Level */
    (*lev) = k_info[q_ptr->k_idx].level;

    /* Value */
    (*val) = obj_value(q_ptr);


    /* Hack */
    if (!buf || !dam || !wgt) return;


    /* Description (too brief) */
    object_desc(buf, q_ptr, (OD_NAME_ONLY | OD_STORE));


    /* Misc info */
    strcpy(dam, "");

    /* Damage */
    switch (q_ptr->tval)
    {
        /* Bows */
        case TV_BOW:
        {
            break;
        }

        /* Ammo */
        case TV_SHOT:
        case TV_BOLT:
        case TV_ARROW:
        {
            sprintf(dam, "%dd%d", q_ptr->dd, q_ptr->ds);
            break;
        }

        /* Weapons */
        case TV_HAFTED:
        case TV_POLEARM:
        case TV_SWORD:
        case TV_DIGGING:
        {
            sprintf(dam, "%dd%d", q_ptr->dd, q_ptr->ds);
            break;
        }

        /* Armour */
        case TV_BOOTS:
        case TV_GLOVES:
        case TV_CLOAK:
        case TV_CROWN:
        case TV_HELM:
        case TV_SHIELD:
        case TV_SOFT_ARMOR:
        case TV_HARD_ARMOR:
        case TV_DRAG_ARMOR:
        {
            sprintf(dam, "%d", q_ptr->ac);
            break;
        }
    }


    /* Weight */
    sprintf(wgt, "%3d.%d", q_ptr->weight / 10, q_ptr->weight % 10);
}