# include "rag.hh"
# include "nodes.h"
# include "grc.h"

IAT* grnl= NULL;

# include "ltgswbus.h"

PRIVATE void
PROCESINI (r)
    TNODE* r;
{
  TNODE* h[33];
  /* KJT 20/01/23: added "int" type */
  register int i;
  int nr;
  int* grl;

  while (r != NULL) {
    h[0]= r;
    heval (r, h);
    grl= grnl->data[(int)(r->value0)];
    for (i= 0; grl[i] != 0; i++) {
     nr= grl[i];
     switch (nr) {
      case _PTCS_1:
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_actl, h[3], 1);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 break;
      case _PTC_1:
	 if (h[1] != NULL)
	  adclr (-c_gmax, h[1], 2);
	 if (h[1] != NULL)
	  adclr (-c_gmin, h[1], 3);
	 if (h[2] != NULL)
	  adclr (-c_gmax, h[2], 4);
	 if (h[2] != NULL)
	  adclr (-c_gmin, h[2], 5);
	 if (h[1] != NULL)
	  adclr (-c_actl, h[1], 6);
	 if (h[2] != NULL)
	  adclr (-c_actl, h[2], 7);
	 break;
      case _sobjs_1:
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_actl, h[3], 8);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_gmax, h[3], 9);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_gmin, h[3], 10);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 if (h[0] != NULL)
	  adclr (-c_factl, h[0], 11);
	 break;
      case _Csubobj_1:
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_obj, h[3], 12);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_gmax, h[3], 13);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_gmin, h[3], 14);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 if (h[1] != NULL)
	  adclr (-c_actl, h[1], 15);
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[4] != NULL) {
	  adclr (-c_actl, h[4], 16);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 if (h[0] != NULL)
	  adclr (-c_factl, h[0], 17);
	 break;
      case _objs_1:
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_obj, h[3], 18);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_actl, h[3], 19);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_gmax, h[3], 20);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_gmin, h[3], 21);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 h[3]= h[1];
	 h[4]= gt_rb (h[3]);
	 while (h[3] != NULL) {
	  adclr (-c_print, h[3], 22);
	  h[3]= h[4];
	  h[4]= gt_rb (h[4]);
	 }
	 break;
      case _obj_1:
	 if (h[0] != NULL)
	  adclr (-c_factl, h[0], 23);
	 break;
      case _obj_2:
	 adclr (-c_factl, h[0], 24);
	 break;
      case _range_1:
	 adclr (-c_min, h[0], 25);
	 adclr (-c_max, h[0], 26);
	 break;
      case _minl_1:
	 adclr (-c_min, h[0], 27);
	 break;
      case _maxl_1:
	 adclr (-c_max, h[0], 28);
	 break;
      case _label_1:
	 if (h[0] != NULL)
	  adclr (-c_tof, h[0], 29);
	 break;
      case _experiment_list_1:
	 if (h[0] != NULL)
	  adclr (-c_exl, h[0], 30);
	 break;
      case _exp_1:
	 if (h[0] != NULL)
	  adclr (-c_exp, h[0], 31);
	 break;
      case _exp_2:
	 adclr (-c_exp, h[0], 32);
	 break;
      default:
	 assert ((1 <= nr) && (nr <= LAST_RULE));
      }
    } /* end of while (ngr != NULL) */
    r= succ (r, PREORDER);
  } /* end of while (r != NULL) */
} /* end of PROCESINI */

PUBLIC CLR_TYPE
eval (nar, r)
    int nar;
    TNODE* r;
{
  TNODE* h[33];

  if (r == NULL) return (CLR_TYPE)NULL;
  switch (nar) {
  case 1:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);

	return (CLR_TYPE) newactcol ((spe)spinit ((int)fdclr (c_lexv,h[0], nar), 0));
  case 2:
    h[0]= gt_ft (r);
    heval (h[0], h);
  {
     if (h[3] == NULL)
	return (CLR_TYPE) -1;
     else
	return (CLR_TYPE) fdclr (c_max,h[3], nar);
  }
  case 3:
    h[0]= gt_ft (r);
    heval (h[0], h);
  {
     if (h[3] == NULL)
	return (CLR_TYPE) 0;
     else
	return (CLR_TYPE) fdclr (c_min,h[3], nar);
  }
  case 4:
    h[0]= gt_ft (r);
    heval (h[0], h);
  {
     if (h[3] == NULL)
	return (CLR_TYPE) -1;
     else
	return (CLR_TYPE) fdclr (c_max,h[3], nar);
  }
  case 5:
    h[0]= gt_ft (r);
    heval (h[0], h);
  {
     if (h[3] == NULL)
	return (CLR_TYPE) 0;
     else
	return (CLR_TYPE) fdclr (c_min,h[3], nar);
  }
  case 6:
    h[0]= gt_ft (r);
    heval (h[0], h);
   return (CLR_TYPE) fdclr (c_actl,h[0], nar);
  case 7:
    h[0]= gt_ft (r);
    heval (h[0], h);
  {
     if (h[1] == NULL)
	return (CLR_TYPE) fdclr (c_actl,h[0], nar);
     else
	return (CLR_TYPE) fdclr (c_factl,h[1], nar);
  }
  case 8:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_actl,h[0], nar);
  case 9:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_gmax,h[0], nar);
  case 10:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_gmin,h[0], nar);
  case 11:
    h[0]= r;
    h[1]= gt_fs (h[0]);
    h[5]= gt_ls (h[0]);
  {
     TNODE*	auxn;
     TNactcol*	auxac;
     TNactcol*	aux2ac;
     int		N,I;
     N = 0;
     for (auxn = h[1]; auxn != NULL; auxn = gt_rb(auxn)){
       auxac = (TNactcol*)fdclr(c_factl,auxn,1000+__LINE__);
       N = N + auxac->nsons;
     }
     aux2ac = newactl ();
     aux2ac->actions = (TNaction**)emalloc(N*sizeof(TNaction*));
     aux2ac->nsons = N;
     aux2ac->maxsons = N;
     for (I=0,auxn = h[1]; auxn != NULL; auxn = gt_rb(auxn)){
       auxac = (TNactcol*)fdclr(c_factl,auxn,1000+__LINE__);
       for (N=0; N<auxac->nsons; N++){
	aux2ac->prevsobj[I] = auxac->prevsobj[N];
	aux2ac->actions[I++] = auxac->actions[N];
       }
     }
      return (CLR_TYPE) aux2ac;
  }
  case 12:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) FALSE;
  case 13:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_gmax,h[0], nar);
  case 14:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_gmin,h[0], nar);
  case 15:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_actl,h[0], nar);
  case 16:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[4]= r;
    h[3]= gt_lb (h[4]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_factl,h[3], nar);
  case 17:
    h[0]= r;
    h[1]= gt_fs (h[0]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_factl,h[5], nar);
  case 18:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) TRUE;
  case 19:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_actl,h[0], nar);
  case 20:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_gmax,h[0], nar);
  case 21:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) fdclr (c_gmin,h[0], nar);
  case 22:
    h[0]= gt_ft (r);
    h[1]= gt_fs (h[0]);
    h[3]= r;
    h[4]= gt_rb (h[3]);
    h[5]= gt_ls (h[0]);
   return (CLR_TYPE) wrttest((TNactcol*)fdclr (c_factl,h[3], nar));
  case 23:
    h[0]= r;
    heval (h[0], h);
  {
     if (h[3] == NULL)
	return (CLR_TYPE) mkactl((TNactcol*)fdclr (c_actl,h[0], nar),(testof*)fdclr (c_tof,h[2], nar),(TNODE*)h[0],
		    0, -1, (int)fdclr (c_gmin,h[0], nar), (int)fdclr (c_gmax,h[0], nar), (int)fdclr (c_obj,h[0], nar));
     else
       return (CLR_TYPE) mkactl((TNactcol*)fdclr (c_actl,h[0], nar),(testof*)fdclr (c_tof,h[2], nar),(TNODE*)h[0],
		   (int)fdclr (c_min,h[3], nar), (int)fdclr (c_max,h[3], nar),
		   (int)fdclr (c_gmin,h[0], nar), (int)fdclr (c_gmax,h[0], nar), (int)fdclr (c_obj,h[0], nar));
  }
  case 24:
    h[0]= r;
    heval (h[0], h);
   return (CLR_TYPE) mkractl ((TNactcol*)fdclr (c_actl,h[0], nar),
				  (testof*)fdclr (c_tof,h[2], nar),
				  (TNODE*)h[0]);
  case 25:
    h[0]= r;
    heval (h[0], h);
   return (CLR_TYPE) fdclr (c_min,h[1], nar);
  case 26:
    h[0]= r;
    heval (h[0], h);
   return (CLR_TYPE) fdclr (c_max,h[2], nar);
  case 27:
    h[0]= r;
    heval (h[0], h);
   return (CLR_TYPE) l2i ((int)fdclr (c_lexv,h[0], nar));
  case 28:
    h[0]= r;
    heval (h[0], h);
   return (CLR_TYPE) l2i ((int)fdclr (c_lexv,h[0], nar));
  case 29:
    h[0]= r;
    heval (h[0], h);
  {
      if (h[2] != NULL)
	 return (CLR_TYPE) mktof ((int)fdclr (c_lexv,h[1], nar),(testof*)fdclr (c_exl,h[2], nar));
      else
	 return (CLR_TYPE) mktof ((int)fdclr (c_lexv,h[1], nar),(testof*)NULL);
  }
  case 30:
    h[0]= r;
    heval (h[0], h);
  {
     testof*	aux;
     TNODE*	auxn;
     int 	i,j;
     aux = newtestof ();
     for (i = 0, auxn = h[1]; auxn != NULL; auxn = gt_rb (auxn), i++);
     aux->nexp = i;
     aux->explist= (expr**)emalloc (i * sizeof (expr*));
     for (j = 1, auxn = h[1]; j <= i && auxn != NULL ; auxn = gt_rb (auxn), j++){
	aux->explist[j-1] = (expr*) fdclr (c_exp, auxn, 1000+__LINE__);
     }
     assert (i == j-1);
      return (CLR_TYPE) aux;
  }
  case 31:
    h[0]= r;
    heval (h[0], h);
  {
	    if (h[3] == NULL)
	       return (CLR_TYPE) mkexp (VALUE, (int)fdclr (c_lexv,h[1], nar), -1);
	    else
	       return (CLR_TYPE) mkexp (VALUE, (int)fdclr (c_lexv,h[1], nar), (int)fdclr (c_lexv,h[2], nar));
  }
  case 32:
    h[0]= r;
    heval (h[0], h);
   return (CLR_TYPE) mkexp (VARIABLE, -1, (int)fdclr (c_lexv,h[1], nar));
     }
  return (CLR_TYPE)NULL;
}





PUBLIC void
rag (r)
    TNODE* r;
{
  PROCESINI (r);
  if (ragerrors == 0)
    todo (r);
} /* end of rag */
