Exemple #1
0
static int
lambda_call(strm_stream* strm, strm_value func, int argc, strm_value* argv, strm_value* ret)
{
  strm_lambda lambda = strm_value_lambda(func);
  node_lambda* nlbd = lambda->body;
  node_args* args = (node_args*)nlbd->args;
  strm_state c = {0};
  int i, n;
  node_error* exc;

  c.prev = lambda->state;
  if ((args == NULL && argc != 0) || (args->len != argc)) {
    if (strm) {
      strm_raise(strm, "wrong number of arguments");
      strm->exc->fname = nlbd->fname;
      strm->exc->lineno = nlbd->lineno;
    }
    return STRM_NG;
  }
  for (i=0; i<argc; i++) {
    n = strm_var_set(&c, node_to_sym(args->data[i]), argv[i]);
    if (n) return n;
  }
  n = exec_expr(strm, &c, nlbd->compstmt, ret);
  if (n == STRM_NG && strm) {
    exc = strm->exc;
    if (exc && exc->type == NODE_ERROR_RETURN) {
      *ret = exc->arg;
    }
  }
  return n;
}
Exemple #2
0
int
strm_funcall(strm_state* state, strm_value func, int argc, strm_value* argv, strm_value* ret)
{
  switch (strm_value_tag(func)) {
  case STRM_TAG_CFUNC:
    return (strm_value_cfunc(func))(state, argc, argv, ret);
  case STRM_TAG_ARRAY:
    return ary_get(state, func, argc, argv, ret);
  case STRM_TAG_PTR:
    if (!strm_lambda_p(func)) {
      strm_raise(state, "not a function");
      return STRM_NG;
    }
    else {
      strm_lambda lambda = strm_value_lambda(func);
      node_lambda* nlbd = lambda->body;
      node_args* args = (node_args*)nlbd->args;
      strm_state c = {0};
      int i, n;

      c.prev = lambda->state;
      if ((args == NULL && argc != 0) &&
          (args->len != argc)) return STRM_NG;
      for (i=0; i<argc; i++) {
        n = strm_var_set(&c, node_to_sym(args->data[i]), argv[i]);
        if (n) return n;
      }
      n = exec_expr(&c, nlbd->compstmt, ret);
      if (c.exc && c.exc->type == NODE_ERROR_RETURN) {
        *ret = c.exc->arg;
        return STRM_OK;
      }
      return n;
    }
  default:
    break;
  }
  return STRM_NG;
}
Exemple #3
0
static int
exec_bar(strm_stream* strm, int argc, strm_value* args, strm_value* ret)
{
  strm_value lhs, rhs;

  assert(argc == 2);
  /* int x int */
  if (strm_int_p(args[0]) && strm_int_p(args[1])) {
    *ret = strm_int_value(strm_value_int(args[0])|strm_value_int(args[1]));
    return STRM_OK;
  }

  lhs = args[0];
  /* lhs: io */
  if (strm_io_p(lhs)) {
    lhs = strm_stream_value(strm_io_stream(lhs, STRM_IO_READ));
  }
  /* lhs: lambda */
  else if (strm_lambda_p(lhs)) {
    strm_lambda lmbd = strm_value_lambda(lhs);
    lhs = strm_stream_value(strm_stream_new(strm_filter, blk_exec, NULL, (void*)lmbd));
  }
  /* lhs: array */
  else if (strm_array_p(lhs)) {
    struct array_data *arrd = malloc(sizeof(struct array_data));
    arrd->arr = strm_value_ary(lhs);
    arrd->n = 0;
    lhs = strm_stream_value(strm_stream_new(strm_producer, arr_exec, NULL, (void*)arrd));
  }
  /* lhs: should be stream */

  rhs = args[1];
  /* rhs: io */
  if (strm_io_p(rhs)) {
    rhs = strm_stream_value(strm_io_stream(rhs, STRM_IO_WRITE));
  }
  /* rhs: lambda */
  else if (strm_lambda_p(rhs)) {
    strm_lambda lmbd = strm_value_lambda(rhs);
    rhs = strm_stream_value(strm_stream_new(strm_filter, blk_exec, NULL, (void*)lmbd));
  }
  /* rhs: cfunc */
  else if (strm_cfunc_p(rhs)) {
    strm_cfunc func = strm_value_cfunc(rhs);
    rhs = strm_stream_value(strm_stream_new(strm_filter, cfunc_exec, NULL, func));
  }

  /* stream x stream */
  if (strm_stream_p(lhs) && strm_stream_p(rhs)) {
    strm_stream* lstrm = strm_value_stream(lhs);
    strm_stream* rstrm = strm_value_stream(rhs);
    if (lstrm == NULL || rstrm == NULL ||
        lstrm->mode == strm_consumer ||
        rstrm->mode == strm_producer) {
      strm_raise(strm, "stream error");
      return STRM_NG;
    }
    strm_stream_connect(strm_value_stream(lhs), strm_value_stream(rhs));
    *ret = rhs;
    return STRM_OK;
  }

  strm_raise(strm, "type error");
  return STRM_NG;
}
Exemple #4
0
static int
exec_bar(strm_state* state, int argc, strm_value* args, strm_value* ret)
{
  strm_value lhs, rhs;

  assert(argc == 2);
  /* int x int */
  if (strm_int_p(args[0]) && strm_int_p(args[1])) {
    *ret = strm_int_value(strm_value_int(args[0])|strm_value_int(args[1]));
    return STRM_OK;
  }

  lhs = args[0];
  /* lhs: io */
  if (strm_io_p(lhs)) {
    strm_io *io = strm_value_io(lhs);
    lhs = strm_task_value(strm_io_open(io, STRM_IO_READ));
  }
  /* lhs: lambda */
  else if (strm_lambda_p(lhs)) {
    strm_lambda *lmbd = strm_value_lambda(lhs)
    lhs = strm_task_value(strm_task_new(strm_task_filt, blk_exec, NULL, (void*)lmbd));
  }
  /* lhs: array */
  else if (strm_array_p(lhs)) {
    struct array_data *arrd = malloc(sizeof(struct array_data));
    arrd->arr = strm_value_array(lhs);
    arrd->n = 0;
    lhs = strm_task_value(strm_task_new(strm_task_prod, arr_exec, arr_finish, (void*)arrd));
  }
  /* lhs: should be task */

  rhs = args[1];
  /* rhs: io */
  if (strm_io_p(rhs)) {
    strm_io *io = strm_value_io(rhs);
    rhs = strm_task_value(strm_io_open(io, STRM_IO_WRITE));
  }
  /* rhs: lambda */
  else if (strm_lambda_p(rhs)) {
    strm_lambda *lmbd = strm_value_lambda(rhs);
    rhs = strm_task_value(strm_task_new(strm_task_filt, blk_exec, NULL, (void*)lmbd));
  }
  /* rhs: cfunc */
  else if (strm_cfunc_p(rhs)) {
    void *func = rhs.val.p;
    rhs = strm_task_value(strm_task_new(strm_task_filt, cfunc_exec, NULL, func));
  }

  /* task x task */
  if (strm_task_p(lhs) && strm_task_p(rhs)) {
    if (lhs.val.p == NULL || rhs.val.p == NULL) {
      node_raise(state, "task error");
      return STRM_NG;
    }
    strm_task_connect(strm_value_task(lhs), strm_value_task(rhs));
    *ret = rhs;
    return STRM_OK;
  }

  node_raise(state, "type error");
  return STRM_NG;
}