int sprintTrieSymbol(char * buffer, Cell symbol) {
  int ctr;

  if ( symbol == ESCAPE_NODE_SYMBOL )
    return sprintf(buffer, "%lu [ESCAPE_NODE_SYMBOL]", ESCAPE_NODE_SYMBOL);
  else {
    switch(TrieSymbolType(symbol)) {
    case XSB_INT:
      return sprintf(buffer, IntegerFormatString, int_val(symbol));
      break;
    case XSB_FLOAT:
      return sprintf(buffer, "%f", float_val(symbol));
      break;
    case XSB_STRING:
      return sprintf(buffer, "%s", string_val(symbol));
      break;
    case XSB_TrieVar:
      return sprintf(buffer, "_V" IntegerFormatString, DecodeTrieVar(symbol));
      break;
    case XSB_STRUCT:
      {
	Psc psc;
	if (isboxedfloat(symbol))
          {
              return sprintf(buffer, "%lf", boxedfloat_val(symbol));
              break;              
          }
	psc = DecodeTrieFunctor(symbol);
	ctr = sprint_quotedname(buffer, 0, get_name(psc));
	return sprintf(buffer+ctr, "/%d", get_arity(psc));
      }
      break;
    case XSB_LIST:
      return sprintf(buffer, "LIST");
      break;
    default:
      return sprintf(buffer, "Unknown symbol (tag = %" Intfmt")", cell_tag(symbol));
      break;
    }
  }
}
void printTrieSymbol(FILE *fp, Cell symbol) {

  if ( symbol == ESCAPE_NODE_SYMBOL )
    fprintf(fp, "%lu [ESCAPE_NODE_SYMBOL]", ESCAPE_NODE_SYMBOL);
  else {
    switch(TrieSymbolType(symbol)) {
    case XSB_INT:
      fprintf(fp, IntegerFormatString, int_val(symbol));
      break;
    case XSB_FLOAT:
      fprintf(fp, "%f", float_val(symbol));
      break;
    case XSB_STRING:
      fprintf(fp, "%s", string_val(symbol));
      break;
    case XSB_TrieVar:
      fprintf(fp, "_V" IntegerFormatString, DecodeTrieVar(symbol));
      break;
    case XSB_STRUCT:
      {
	Psc psc;
	if (isboxedfloat(symbol))
          {
              fprintf(fp, "%lf", boxedfloat_val(symbol));
              break;              
          }
	psc = DecodeTrieFunctor(symbol);
	fprintf(fp, "%s/%d", get_name(psc), get_arity(psc));
      }
      break;
    case XSB_LIST:
      fprintf(fp, "LIST");
      break;
    default:
      fprintf(fp, "Unknown symbol (tag = %" Intfmt")", cell_tag(symbol));
      break;
    }
  }
}
Ejemplo n.º 3
0
Archivo: subp.c Proyecto: flavioc/XSB
int compare(CTXTdeclc const void * v1, const void * v2)
{
  int comp;
  CPtr cptr1, cptr2;
  Cell val1 = (Cell) v1 ;
  Cell val2 = (Cell) v2 ;

  XSB_Deref(val2);		/* val2 is not in register! */
  XSB_Deref(val1);		/* val1 is not in register! */
  if (val1 == val2) return 0;
  switch(cell_tag(val1)) {
  case XSB_FREE:
  case XSB_REF1:
    if (isattv(val2))
      return vptr(val1) - (CPtr)dec_addr(val2);
    else if (isnonvar(val2)) return -1;
    else { /* in case there exist local stack variables in the	  */
	   /* comparison, globalize them to guarantee that their  */
	   /* order is retained as long as nobody "touches" them  */
	   /* in the future -- without copying garbage collection */
      if ((top_of_localstk <= vptr(val1)) &&
	  (vptr(val1) <= (CPtr)glstack.high-1)) {
	bld_free(hreg);
	bind_ref(vptr(val1), hreg);
	hreg++;
	val1 = follow(val1);	/* deref again */
      }
      if ((top_of_localstk <= vptr(val2)) &&
	  (vptr(val2) <= (CPtr)glstack.high-1)) {
	bld_free(hreg);
	bind_ref(vptr(val2), hreg);
	hreg++;
	val2 = follow(val2);	/* deref again */
      }
      return vptr(val1) - vptr(val2);
    }
  case XSB_FLOAT:
    if (isref(val2) || isattv(val2)) return 1;
    else if (isofloat(val2)) 
      return sign(float_val(val1) - ofloat_val(val2));
    else return -1;
  case XSB_INT:
    if (isref(val2) || isofloat(val2) || isattv(val2)) return 1;
    else if (isinteger(val2)) 
      return int_val(val1) - int_val(val2);
    else if (isboxedinteger(val2))
      return int_val(val1) - boxedint_val(val2);
    else return -1;
  case XSB_STRING:
    if (isref(val2) || isofloat(val2) || isinteger(val2) || isattv(val2)) 
      return 1;
    else if (isstring(val2)) {
      return strcmp(string_val(val1), string_val(val2));
    }
    else return -1;
  case XSB_STRUCT:
    // below, first 2 if-checks test to see if this struct is actually a number representation,
    // (boxed float or boxed int) and if so, does what the number case would do, only with boxed_val
    // macros.
    if (isboxedinteger(val1)) {
      if (isref(val2) || isofloat(val2) || isattv(val2)) return 1;
      else if (isinteger(val2)) 
	return boxedint_val(val1) - int_val(val2);
      else if (isboxedinteger(val2))
	return boxedint_val(val1) - boxedint_val(val2);
      else return -1;
    } else if (isboxedfloat(val1)) {
        if (isref(val2) || isattv(val2)) return 1;
        else if (isofloat(val2)) 
          return sign(boxedfloat_val(val1) - ofloat_val(val2));
        else return -1;            
    } else if (cell_tag(val2) != XSB_STRUCT && cell_tag(val2) != XSB_LIST) return 1;
    else {
      int arity1, arity2;
      Psc ptr1 = get_str_psc(val1);
      Psc ptr2 = get_str_psc(val2);

      arity1 = get_arity(ptr1);
      if (islist(val2)) arity2 = 2; 
      else arity2 = get_arity(ptr2);
      if (arity1 != arity2) return arity1-arity2;
      if (islist(val2)) comp = strcmp(get_name(ptr1), ".");
      else comp = strcmp(get_name(ptr1), get_name(ptr2));
      if (comp || (arity1 == 0)) return comp;
      cptr1 = clref_val(val1);
      cptr2 = clref_val(val2);
      for (arity2 = 1; arity2 <= arity1; arity2++) {
	if (islist(val2))
	  comp = compare(CTXTc (void*)cell(cptr1+arity2), (void*)cell(cptr2+arity2-1));  
	else
	  comp = compare(CTXTc (void*)cell(cptr1+arity2), (void*)cell(cptr2+arity2));
	if (comp) break;
      }
      return comp;
    }
    break;
  case XSB_LIST:
    if (cell_tag(val2) != XSB_STRUCT && cell_tag(val2) != XSB_LIST) return 1;
    else if (isconstr(val2)) return -(compare(CTXTc (void*)val2, (void*)val1));
    else {	/* Here we are comparing two list structures. */
      cptr1 = clref_val(val1);
      cptr2 = clref_val(val2);
      comp = compare(CTXTc (void*)cell(cptr1), (void*)cell(cptr2));
      if (comp) return comp;
      return compare(CTXTc (void*)cell(cptr1+1), (void*)cell(cptr2+1));
    }
    break;
  case XSB_ATTV:
    if (isattv(val2))
      return (CPtr)dec_addr(val1) - (CPtr)dec_addr(val2);
    else if (isref(val2))
      return (CPtr)dec_addr(val1) - vptr(val2);
    else
      return -1;
  default:
    xsb_abort("Compare (unknown tag %ld); returning 0", cell_tag(val1));
    return 0;
  }
}
static int symstkSPrintNextTerm(CTXTdeclc char * buffer, xsbBool list_recursion) {
  int ctr = 0;
  Cell symbol;

  if ( SymbolStack_IsEmpty ) {
    //    fprintf(fp, "<no subterm>");
    return 0;
  }
  SymbolStack_Pop(symbol);
  switch ( TrieSymbolType(symbol) ) {
  case XSB_INT:
    if ( list_recursion )
      ctr = ctr + sprintf(buffer+ctr, "|" IntegerFormatString "]", int_val(symbol));
    else
      ctr = ctr + sprintf(buffer+ctr, IntegerFormatString, int_val(symbol));
    break;
  case XSB_FLOAT:
    if ( list_recursion )
      ctr = ctr + sprintf(buffer+ctr, "|%f]", float_val(symbol));
    else
      ctr = ctr + sprintf(buffer+ctr, "%f", float_val(symbol));
    break;
  case XSB_STRING:
    {
      char *string = string_val(symbol);
      if ( list_recursion ) {
	if ( string == nil_string )
	  ctr = ctr + sprintf(buffer+ctr, "]");
	else {
	  //	  ctr = ctr + sprintf(buffer+ctr, "|%s]", string);
	  ctr = ctr + sprintf(buffer+ctr, "|");
	  ctr = sprint_quotedname(buffer, ctr, string);
	  ctr = ctr + sprintf(buffer+ctr, "]");
	}
      }
      else
	//	ctr = ctr + sprintf(buffer+ctr, "%s", string);
	ctr = sprint_quotedname(buffer, ctr, string);
    }
    break;
  case XSB_TrieVar:
    if ( list_recursion )
      ctr = ctr + sprintf(buffer+ctr, "|V" IntegerFormatString "]", DecodeTrieVar(symbol));
    else
      ctr = ctr + sprintf(buffer+ctr, "_V" IntegerFormatString, DecodeTrieVar(symbol));
    break;
  case XSB_STRUCT:
    {
      Psc psc;
      int i;
      if (isboxedfloat(symbol))
      {
        if ( list_recursion ) 
	  ctr = ctr + sprintf(buffer+ctr, "|%lf]", boxedfloat_val(symbol));
        else
          ctr = ctr + sprintf(buffer+ctr, "%lf", boxedfloat_val(symbol));
        break;         
      }

      if ( list_recursion )
	ctr = ctr + sprintf(buffer+ctr, "|");
      psc = DecodeTrieFunctor(symbol);
      //      ctr = ctr + sprintf(buffer+ctr, "%s(", get_name(psc));
      ctr = sprint_quotedname(buffer, ctr, get_name(psc));
      ctr = ctr + sprintf(buffer+ctr, "(");
      for (i = 1; i < (int)get_arity(psc); i++) {
	ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,FALSE);
	ctr = ctr + sprintf(buffer+ctr, ",");
      }
      ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,FALSE);
      ctr = ctr + sprintf(buffer+ctr, ")");
      if ( list_recursion )
	ctr = ctr + sprintf(buffer+ctr, "]");
    }
    break;
  case XSB_LIST:
    if ( list_recursion )
      ctr = ctr + sprintf(buffer+ctr, ",");
    else
      ctr = ctr + sprintf(buffer+ctr, "[");
    ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,FALSE);
    ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,TRUE);
    break;
  default:
    ctr = ctr + sprintf(buffer+ctr, "<unknown symbol>");
    break;
  }
  return ctr;
}
static void symstkPrintNextTerm(CTXTdeclc FILE *fp, xsbBool list_recursion) {

  Cell symbol;

  if ( SymbolStack_IsEmpty ) {
    fprintf(fp, "<no subterm>");
    return;
  }
  SymbolStack_Pop(symbol);
  switch ( TrieSymbolType(symbol) ) {
  case XSB_INT:
    if ( list_recursion )
      fprintf(fp, "|" IntegerFormatString "]", int_val(symbol));
    else
      fprintf(fp, IntegerFormatString, int_val(symbol));
    break;
  case XSB_FLOAT:
    if ( list_recursion )
      fprintf(fp, "|%f]", float_val(symbol));
    else
      fprintf(fp, "%f", float_val(symbol));
    break;
  case XSB_STRING:
    {
      char *string = string_val(symbol);
      if ( list_recursion ) {
	if ( string == nil_string )
	  fprintf(fp, "]");
	else
	  fprintf(fp, "|%s]", string);
      }
      else
	fprintf(fp, "%s", string);
    }
    break;
  case XSB_TrieVar:
    if ( list_recursion )
      fprintf(fp, "|V" IntegerFormatString "]", DecodeTrieVar(symbol));
    else
      fprintf(fp, "_V" IntegerFormatString, DecodeTrieVar(symbol));
    break;
  case XSB_STRUCT:
    {
      Psc psc;
      int i;
      if (isboxedfloat(symbol))
      {
        if ( list_recursion )
          fprintf(fp, "|%lf]", boxedfloat_val(symbol));
        else
          fprintf(fp, "%lf", boxedfloat_val(symbol));
        break;         
      }

      if ( list_recursion )
	fprintf(fp, "|");
      psc = DecodeTrieFunctor(symbol);
      fprintf(fp, "%s(", get_name(psc));
      for (i = 1; i < (int)get_arity(psc); i++) {
	symstkPrintNextTerm(CTXTc fp,FALSE);
	fprintf(fp, ",");
      }
      symstkPrintNextTerm(CTXTc fp,FALSE);
      fprintf(fp, ")");
      if ( list_recursion )
	fprintf(fp, "]");
    }
    break;
  case XSB_LIST:
    if ( list_recursion )
      fprintf(fp, ",");
    else
      fprintf(fp, "[");
    symstkPrintNextTerm(CTXTc fp,FALSE);
    symstkPrintNextTerm(CTXTc fp,TRUE);
    break;
  default:
    fprintf(fp, "<unknown symbol>");
    break;
  }
}