Exemplo n.º 1
0
int
strm_ptr_tag_p(strm_value v, enum strm_ptr_type e)
{
  if (strm_value_tag(v) == STRM_TAG_PTR) {
    void *p = strm_ptr(v);
    return strm_ptr_type(p) == e;
  }
  return FALSE;
}
Exemplo n.º 2
0
void*
strm_value_ptr(strm_value v, enum strm_ptr_type e)
{
  void *p;

  assert(strm_value_tag(v) == STRM_TAG_PTR);
  p = strm_ptr(v);
  assert(p && strm_ptr_type(p) == e);
  return p;
}
Exemplo n.º 3
0
strm_state*
strm_value_ns(strm_value val)
{
  if (strm_array_p(val))
    return strm_ary_ns(val);
  if (strm_value_tag(val) == STRM_TAG_PTR) {
    struct strm_misc* p = strm_ptr(val);

    if (strm_ptr_type(p) == STRM_PTR_MISC) {
      return p->ns;
    }
  }
  return NULL;
}
Exemplo n.º 4
0
strm_state*
strm_value_ns(strm_value val)
{
  if (strm_array_p(val)) {
    strm_state* ns = strm_ary_ns(val);
    if (ns) return ns;
    return strm_ns_array;
  }
  if (strm_string_p(val)) {
    return strm_ns_string;
  }
  if (strm_number_p(val)) {
    return strm_ns_number;
  }
  if (strm_value_tag(val) == STRM_TAG_PTR) {
    struct strm_misc* p = strm_ptr(val);

    if (!p) return NULL;
    if (strm_ptr_type(p) == STRM_PTR_AUX) {
      return p->ns;
    }
  }
  return NULL;
}
Exemplo n.º 5
0
void*
strm_value_foreign(strm_value v)
{
  assert(strm_value_tag(v) == STRM_TAG_FOREIGN);
  return strm_ptr(v);
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
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;
}