Esempio n. 1
0
char *str_right_append(slot_t lhs, char *rhs, slot_t *type) {
  slot_t tag = *type;
  char buf[1024];

  switch(tag) {
  case -1:
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
    internal_tostring(&lhs,type,buf);
    break;
  default:
    internal_tostring((slot_t*)lhs,type,buf);
  }
  strcat(buf,rhs);
  char *result = malloc(1 + strlen(buf));
  strcpy(result, buf);
  return result;
}
Esempio n. 2
0
char *str_left_append(char *lhs, slot_t rhs, slot_t *type) {
  int lhsLen = strlen(lhs);
  slot_t tag = *type;
  char buf[1024];
  strcat(buf,lhs);

  switch(tag) {
  case 0:
  case 1:
  case 2:
  case 3:
  case 4:
    internal_tostring(&rhs,type,buf + lhsLen);
    break;
  default:
    internal_tostring((slot_t*)rhs,type,buf + lhsLen);
  }
  char *result = malloc(1 + strlen(buf));
  strcpy(result, buf);
  return result;
}
Esempio n. 3
0
void internal_tostring(slot_t *item, slot_t *type, char* buf) {

  // NOTE: this function is not working properly yet.

  slot_t tag = *type;

  switch(tag) {
  case VOID_TAG:
    // void
    break;
  case NULL_TAG:
      sprintf(buf,"null");
      break;
  case BOOL_TAG:
    // bool
    if(*item == 0) {
      sprintf(buf,"false");
    } else {
      sprintf(buf,"true");
    }
    break;
  case CHAR_TAG: 
    {
      // char
      char tmp[2];
      tmp[0] = *item;
      tmp[1] = '\0';
      sprintf(buf,"%s",tmp);
      break;
    }
  case INT_TAG:
    // int
    sprintf(buf,"%d",*item);
    break;
  case REAL_TAG:
    // real
    sprintf(buf,"%f",*item);
    break;
  case STRING_TAG:
    // string
    sprintf(buf,"%s",item);
    break;
  case RECORD_TAG:
    {
      int i;
      // record
      sprintf(buf,"{");
      buf += strlen(buf);
      slot_t nfields = *(++type);
      for(i=0;i!=nfields;++i) {
	if(i != 0) {
	  sprintf(buf,",");
	  buf += strlen(buf);
	}
	slot_t fieldNameSize = *(++type);
	sprintf(buf,"%s:",++type);
	buf += strlen(buf);
	type = ((void *)type) + fieldNameSize + 1;
	internal_tostring(item,type,buf);
	buf += strlen(buf);
	item = ((void *)item) + widthof(type);
	// FIXME: this is clearly broken here, because we don't increment type correctly.
      }
      sprintf(buf,"}");
    }
    break;
    case LIST_TAG:
        ;
        
    slot_t size = *item;
    slot_t *etype = (slot_t *) *(item + 1);

    strcat(buf, "[");

    int i = 0;
    for (i = 0; i < size; i++) {
        if (i != 0) {
            strcat(buf, ", ");
        }

        char buf2[1024];
        slot_t *element = item + i + 2;
        internal_tostring(element, etype, buf2);
        strcat(buf, buf2);
    }

    strcat(buf, "]");

    break;
  }
}