// future plans for repearing
// 1. Make Negative variables (done)
// 2. Make 256 constants  (almost done)
// 3. Make open terms with real numbers


// calc. var num    /* buf_6[--i]&2 is 2,3  (6,7,10 are impossible) */
#define CALC_VAR_J(c,w) for (j=c, i=br_buf1; i>0; )\
  if ( buf_6[--i]&2 )\
  {\
    if (buf_6[i]&1)\
    {\
      m=Buda1[buf_4[i]]-TT_START;\
      if (Ufil_vs[m-MEMORY_C]!=M1)\
        j=Substi[Ufil_vs[m-MEMORY_C]+j];\
      else if (Ufil_c1[m]>=MEMORY_c256)\
        j+=Chfil_v[Ufil_c1[m]-MEMORY_c256];\
      else if (Ufil_c1[m]==TT_VAR)\
        j++;\
    }\
    j=Link[Buda2[buf_4[i]]+j];\
    goto w;\
  }\
  else if (buf_6[i]&1)\
  {\
    if (Ufil_vs[buf_4[i]-MEMORY_c256]!=M1)\
      j=Substi[Ufil_vs[buf_4[i]-MEMORY_c256]+j];\
    else if (Ufil_c1[buf_4[i]-TT_START]>=MEMORY_c256)\
      j+=Chfil_v[Ufil_c1[buf_4[i]-TT_START]-MEMORY_c256];\
    else if (Ufil_c1[buf_4[i]-TT_START]==TT_VAR)\
      j++;\
  }

// if buf1_min==0 then the value of n is not used but makes runtime wornniing if it is not defined
                            // n - jump        // SALAM - 8 - 2*(br_buf1-buf1_min)
#define WRITE_IN_STACK  if (salam_br>SALAM && correct_SALAM() )\
  goto FULL;\
n=salam_br;\
if (buf1_min)\
  for (; Salam[n-2]>=buf1_min; n-=8+2*(Salam[n-3]-Salam[n-2]));\
for (i=buf1_min; i<br_buf1; Salam[salam_br]=buf_6[i++], Salam[salam_br+1]=buf_4[i], salam_br+=2);\
salam_br+=8;\
Salam[salam_br-1]=n-8;\
Salam[salam_br-2]=buf1_min;\
Salam[salam_br-3]=buf1_min=br_buf1;\
Salam[salam_br-4]=durty_br

inline DWORD CreateArray(DWORD size)
{
  DWORD i, j, old;

  if (the_free_list!=M1)
  {
    for ( old=i=j=the_free_list; ObjectStore[i+3]<size && (j=ObjectStore[i+6])!=M1; old=i, i=j);
    if (j==M1)
    {
      if (correct_list(the_free_list, size))
        return(0);
      i=the_free_list;
      the_free_list=ObjectStore[i+6];
    }
    else if (the_free_list==i)
      the_free_list=ObjectStore[i+6];
    else
      ObjectStore[old+6]=ObjectStore[i+6];
  }
  else
  {
    i=ObjectStore_br;
    j=LISTS_INCREASE*(1+size/LISTS_INCREASE);
    if ((ObjectStore_br+=7)>=OBJ_STORE && correct_OBJ_ST() )
    {
      MessageBoxA(NUL,"Object Store is Full!", "No Space", MB_OK | MB_ICONSTOP);
      return(0);
    }
    ObjectStore[i+5]=(DWORD)GlobalAlloc(GMEM_MOVEABLE, 4*j);
    ObjectStore[i+4]=(DWORD)(LPDWORD)GlobalLock((MY_HANDLE)ObjectStore[i+5]);
    ObjectStore[i+3]=j;    // real size
  }
  ObjectStore[i]=CLASS_SYSTEM_LIST;
  ObjectStore[i+1]=M1;   // object name
  ObjectStore[i+2]=size;  // size
  ObjectStore[i+6]=HALF | TT_INT; // Flag "Temporal object" | type int
  return(i);
}
void error_func(DWORD type, DWORD types)
{
  strcat(string2, "\"\r\nis ");
  MitiText(string2);
  switch(type)
  {
   case 0:
    MitiText("\"var\"");
    break;
   case 1:
    MitiText("\"term\"");
    break;
   case 2:
    MitiText("\"str\"");
    break;
   case 3:
    MitiText("\"int\"");
    break;
   case 4:
    MitiText("\"float\"");
    break;
   case 5:
    MitiText("\"obj\"");
    break;
   case 6:
    MitiText("\"list\"");
  }
  MitiText( " but has to be ");
  {
    DWORD m1, or_flag=0, Pointers_flag=TP_VAR;
    for (m1=0; m1<6; m1++, Pointers_flag=Pointers_flag>>1)
    {
      if (types&Pointers_flag)
      {
        if (or_flag)
          MitiText(" or ");
        else
          or_flag=1;
        switch(m1)
        {
         case 0:
          MitiText("\"var\"");
          break;
         case 1:
          MitiText("\"term\"");
          break;
         case 2:
          MitiText("\"str\"");
          break;
         case 3:
          MitiText("\"int\"");
          break;
         case 4:
          MitiText("\"float\"");
          break;
         case 5:
          MitiText("\"obj\"");
        }
      }
    }
  }
  MitiText( ".\r\n\r\n");
  //if sound on error
  //MessageBeep(MB_ICONHAND);

  //Wait=Wait_d=0;
  //return(5);
}

void error_func2(DWORD t, DWORD k)
{
  DWORD i;

  sprintf(string2,"Runtime Error at step %d:\r\n", 0x10000*lSteps_br+Steps_br-Corection);
  MitiText(string2, &cf_red);
  for (i=sv_c+TT_START-1; i && (!(Pointers[i]&OBSEG3) || (F_field[D_field[(Pointers[i]&MASKA3)+4]-1]
                               &(M1^(HALF | TP_TEMP_FLAG | OBSEG1 | OBSEG2)))!=Ekler[k]); i--);
  if (i)
    MitiText("The value returned by the function \"");
  else
  {
    MitiText("The value returned by the constant \"");
    for (i=sv_c+TT_START-1; i && (!(Pointers[i]&OBSEG4)
                        || (D_field[(Pointers[i]&MASKA3)+5]&HALFM)!=Ekler[k]); i--);
  }
  len=0;
  write_p( i, 4, 0);
  error_func(t, Ekler[k-2]);
}

