Esempio n. 1
0
static void write_c_value(NODE *f,CONTEXT *c,NODE *v,char *x) {
   if (v==NULL) {
      switch (c->default_policy) {
       case dp_value:
	 write_c_value(f,c,c->default_value,x);
	 break;

       case dp_any:
	 write_c_value(f,c,c->am.first_key->nodes,x);
	 break;

       case dp_null:
	 of_write_wrapped(f,"NULL%s",x);
	 break;

       default:
	 of_write_wrapped(f,"0 /* missing key */%s",x);
	 break;
      }

   } else if (v->type==nt_int) {
      if (c->quote_policy==qp_everything)
	of_write_wrapped(f,"\"%d\"%s",v->x,x);
      else
	of_write_wrapped(f,"0x%X%s",v->x,x);

   } else {
      if (c->quote_policy==qp_nothing)
	of_write_wrapped(f,"%s%s",v->cp,x);
      else
	of_write_wrapped(f,"\"%s\"%s",v->cp,x);
   }
}
Esempio n. 2
0
void gen_basic_array(CONTEXT *c) {
   NODE *cf,*hf;
   char *ots,*maybe_space;
   int i;
   NODE *k,*v;
   CONTEXT *ctx;
   
   /* check:  can we proceed? */
   if ((c->am.first_key!=NULL) && (c->am.first_key->type==nt_int) &&
       (c->am.last_key!=NULL) && (c->am.last_key->type==nt_int))
     c->generator=gen_basic_array;
   else {
      c->generator=NULL;
      return;
   }
   
   cf=open_output_file(c->c_file);
   hf=open_output_file(c->h_file);
   
   k=node_new();
   k->type=nt_int;
   
   if (c->value_c_type==NULL)
     ots="int";
   else
     ots=c->value_c_type;
   maybe_space=(ots[strlen(ots)-1]=='*')?"":" ";

   of_write(hf,"/* basic array lookup for map \"%s\" */\n\n",
	    c->id);
   of_write(hf,"extern %s%s__icemap_%s_array[];\n",
	    ots,maybe_space,c->id);
   if (c->am.first_key->x==0)
     of_write(hf,"#define %s_lookup(idx) \\\n"
	      "  (__icemap_%s_array[(idx)])\n\n",
	      c->id,c->id);
   else
     of_write(hf,"#define %s_lookup(idx) \\\n"
	      "  (__icemap_%s_array[(idx)-%d])\n\n",
	      c->id,c->id,c->am.first_key->x);

   of_write(cf,"/* basic array lookup for map \"%s\" */\n\n",
	    c->id);
   of_write(cf,"%s%s__icemap_%s_array[%d]={\n",
	    ots,maybe_space,c->id,c->am.last_key->x-c->am.first_key->x+1);
   of_indent(cf,2);

   for (i=c->am.first_key->x;i<=c->am.last_key->x;i++) {
      k->x=i;
      v=arrow_map_lookup(&(c->am),k);
      if (v==NULL)
	of_write_wrapped(cf,"0 /* missing key */,");
      else if (v->type==nt_int)
	of_write_wrapped(cf,"0x%X,",v->x);
      else
	of_write_wrapped(cf,"\"%s\",",v->cp);
   }

   of_unindent(cf,2);
   of_write(cf,"\n};\n\n");

   node_delete(k);

   for (ctx=context_stack->parent;ctx;ctx=ctx->parent)
     ctx->leaves++;
}