//  File: tokenitem.C
//
//  Implementation of Item class for expression trees

#include <stdio.h>
#include <stdlib.h>
#include "tokenitem.h"

// Constructors

Item::Item() {
   item = NULL ;
}


Item::Item(data x) {
   item = new token_t ;
   if (item == NULL) {
	  fprintf(stderr, "Could not make new token structure\n") ;
	  exit(1) ;
   }
   *item = *x ;
}


Item::~Item() {
   if (item != NULL) delete item ;
}


data Item::copy() {
   data temp ;

   temp = new token_t ;
   if (temp == NULL) {
	  fprintf(stderr, "Could not make new token structure\n") ;
	  exit(1) ;
   }
   *temp = *item ;
   return temp ;
}


int Item::compare(data x) {

   // check for NULL cases

   if (item == NULL && x == NULL) return 0 ;
   if (item == NULL) return -1 ;
   if (x == NULL) return 1 ;

   if (item->kind == NUMBER && x->kind == NUMBER) {
	  if (item->value < x->value) return -1 ;
	  if (item->value > x->value) return  1 ;
	  return 0 ;
   } else {
	  if (item->kind < x->kind) return -1 ;
	  if (item->kind > x->kind) return  1 ;
	  return 0 ;
   }
}


void Item::print() {
   
   switch(item->kind) {
	  case NUMBER :
		 printf ("%d", item->value) ; break ;

	  case PLUS :
		 printf ("+") ; break ;

	  case MINUS :
		 printf ("-") ; break ;

	  case TIMES :
		 printf ("*") ; break ;

	  case DIVIDE :
		 printf ("/") ; break ;

	  case L_PAREN :
		 printf ("(") ; break ;

	  case R_PAREN :
		 printf (")") ; break ;
   }

}
