#include"mbasic.h"

static char * cmp_toks[]={"EQ","NE","BG","LW","BE","LE"};

static int check_if_token(void)
{
 char p[3];
 int i;
 SkipWhite();
 GetName();
 for(i=0;i<6;i++)
 {
  if(!strncmp(cmp_toks[i],current_token,2))
  {
   return i;
  }
 }
 Error("Unknown if type %s. (EQ ==, NE !=, BG >, LW <, BE >=, LE <=)",p);
}

void ParseIf(void)
{
 int l;
 int L1,ELB;
 SkipWhite();
 Expression();
 emit("mov ebx,eax\n");
 l=check_if_token();
 L1=NewLabel();
 ELB=NewLabel();
 SkipWhite();
 Expression();
 emit("cmp ebx,eax\n");
 switch(l)
 {
  case 0:
   emit("jne L%u\n",ELB);
   break;
  case 1:
   emit("je L%u\n",ELB);
   break;
  case 2:
   emit("jnb L%u\n",ELB);
   break;
  case 3:
   emit("jb L%u\n",ELB); 
   break;
  case 4:
   emit("jnb L%u\n",ELB);
   emit("jne L%u\n",ELB);
   break;
  case 5:
   emit("jns L%u\n",ELB);
   emit("jne L%u\n",ELB);
   break;
  default:
   Error("%s: Internal error in function check_if_token()",__FILE__);
 }
 SkipWhite();
 Block();
 emit("jmp L%u\n",L1);
 PostLabel(ELB);
 if(!strncmp(current_token,"ELSE",4))
 {
  Block();
 }
 PostLabel(L1);
}
