Beispiel #1
0
strm_string
strm_inspect(strm_value v)
{
  if (strm_string_p(v)) {
    strm_string str = strm_value_str(v);
    return str_dump(str, str_dump_len(str));
  }
  else if (strm_array_p(v)) {
    char *buf = malloc(32);
    strm_int i, bi = 0, capa = 32;
    strm_array a = strm_value_ary(v);

    for (i=0; i<strm_ary_len(a); i++) {
      strm_string str = strm_inspect(strm_ary_ptr(a)[i]);
      strm_string key = (strm_ary_headers(a) &&
                         strm_string_p(strm_ary_ptr(strm_ary_headers(a))[i])) ?
        strm_value_str(strm_ary_ptr(strm_ary_headers(a))[i]) : strm_str_null;
      strm_int slen = (key ? (strm_str_len(key)+1) : 0) + strm_str_len(str) + 3;

      if (bi+slen > capa) {
        capa *= 2;
        buf = realloc(buf, capa);
      }
      if (bi == 0) {
        buf[bi++] = '[';
      }
      else {
        buf[bi++] = ',';
        buf[bi++] = ' ';
      }
      if (key) {
        if (!str_symbol_p(key)) {
          key = str_dump(key, str_dump_len(key));
        }
        memcpy(buf+bi, strm_str_ptr(key), strm_str_len(key));
        bi += strm_str_len(key);
        buf[bi++] = ':';
      }
      memcpy(buf+bi, strm_str_ptr(str), strm_str_len(str));
      bi += strm_str_len(str);
    }
    buf[bi++] = ']';
    return strm_str_new(buf, bi);
  }
  else {
    return strm_to_str(v);
  }
}
Beispiel #2
0
strm_string
strm_to_str(strm_value v)
{
  char buf[32];
  int n;
  strm_state* ns = strm_value_ns(v);

  if (ns) {
    strm_value m;

    n = strm_var_get(ns, strm_str_intern_lit("string"), &m);
    if (n == STRM_OK) {
      n = strm_funcall(NULL, m, 1, &v, &m);
      if (n == STRM_OK && strm_string_p(m)) return m;
    }
  }
  switch (strm_value_tag(v)) {
  case STRM_TAG_INT:
    n = sprintf(buf, "%d", strm_to_int(v));
    return strm_str_new(buf, n);
  case STRM_TAG_BOOL:
    n = sprintf(buf, strm_to_int(v) ? "true" : "false");
    return strm_str_new(buf, n);
  case STRM_TAG_CFUNC:
    n = sprintf(buf, "<cfunc:%p>", (void*)strm_value_cfunc(v));
    return strm_str_new(buf, n);
  case STRM_TAG_STRING_I:
  case STRM_TAG_STRING_6:
  case STRM_TAG_STRING_O:
  case STRM_TAG_STRING_F:
    return strm_value_str(v);
  case STRM_TAG_ARRAY:
  case STRM_TAG_STRUCT:
    return strm_inspect(v);
  case STRM_TAG_PTR:
    if (strm_value_val(v) == 0)
      return strm_str_lit("nil");
    else {
      void *p = strm_ptr(v);
      switch (strm_ptr_type(p)) {
      case STRM_PTR_STREAM:
        n = sprintf(buf, "<stream:%p>", p);
        break;
      case STRM_PTR_IO: {
        strm_io io = (strm_io)p;
        char *mode;

        switch (io->mode & 3) {
        case STRM_IO_READ:
          mode = "r"; break;
        case STRM_IO_WRITE:
          mode = "w"; break;
        case STRM_IO_READ|STRM_IO_WRITE:
          mode = "rw"; break;
        default:
          mode = "?"; break;
        }
        n = sprintf(buf, "<io: fd=%d mode=%s>", io->fd, mode);
        break;
      }
      case STRM_PTR_LAMBDA:
        n = sprintf(buf, "<lambda:%p>", p);
        break;
      case STRM_PTR_AUX:
        n = sprintf(buf, "<obj:%p>", p);
        break;
      }
      return strm_str_new(buf, n);
    }
  default:
    if (strm_flt_p(v)) {
      n = sprintf(buf, "%.14g", strm_to_flt(v));
      return strm_str_new(buf, n);
    }
    n = sprintf(buf, "<%p>", strm_value_vptr(v));
    return strm_str_new(buf, n);
  }
  /* not reached */
  return strm_str_null;
}
Beispiel #3
0
strm_string
strm_to_str(strm_value v)
{
  char buf[32];
  int n;

  switch (strm_value_tag(v)) {
  case STRM_TAG_INT:
    n = sprintf(buf, "%d", strm_to_int(v));
    return strm_str_new(buf, n);
  case STRM_TAG_BOOL:
    n = sprintf(buf, strm_to_int(v) ? "true" : "false");
    return strm_str_new(buf, n);
  case STRM_TAG_CFUNC:
    n = sprintf(buf, "<cfunc:%p>", (void*)strm_value_cfunc(v));
    return strm_str_new(buf, n);
  case STRM_TAG_STRING_I:
  case STRM_TAG_STRING_6:
  case STRM_TAG_STRING_O:
  case STRM_TAG_STRING_F:
    return strm_value_str(v);
  case STRM_TAG_ARRAY:
  case STRM_TAG_STRUCT:
    return strm_inspect(v);
  case STRM_TAG_PTR:
    if (strm_value_val(v) == 0)
      return strm_str_new("nil", 3);
    else {
      void *p = strm_ptr(v);
      switch (strm_ptr_type(p)) {
      case STRM_PTR_TASK:
        n = sprintf(buf, "<task:%p>", p);
        break;
      case STRM_PTR_IO: {
        strm_io io = (strm_io)p;
        char *mode;

        switch (io->mode & 3) {
        case STRM_IO_READ:
          mode = "r"; break;
        case STRM_IO_WRITE:
          mode = "w"; break;
        case STRM_IO_READ|STRM_IO_WRITE:
          mode = "rw"; break;
        }
        n = sprintf(buf, "<io: fd=%d mode=%s>", io->fd, mode);
        break;
      }
      case STRM_PTR_LAMBDA:
        n = sprintf(buf, "<lambda:%p>", p);
        break;
      case STRM_PTR_MISC:
        n = sprintf(buf, "<obj:%p>", p);
        break;
      }
      return strm_str_new(buf, n);
      break;
    }
  default:
    if (strm_flt_p(v)) {
      n = sprintf(buf, "%g", strm_to_flt(v));
      return strm_str_new(buf, n);
    }
    n = sprintf(buf, "<%p>", strm_value_vptr(v));
    return strm_str_new(buf, n);
  }
  /* not reached */
  return strm_str_null;
}