Example #1
0
lval *builtin_var(lenv *e, lval *a, char *func)
{
  LASSERT_TYPE("def", a, 0, LVAL_QEXPR);
  lval *syms = a->cell[0];
  for (int i = 0; i < syms->count; i++)
  {
    LASSERT(a, (syms->cell[i]->type == LVAL_SYM),
      "Function 'def' cannot define non-symbol"
      "\n  got %s expected %s",
      ltype_name(syms->cell[i]->type), ltype_name(LVAL_SYM));
  }
  LASSERT(a, (syms->count == a->count-1),
    "Function 'def' passed too many arguments for symbols"
    "\n  got %i expected %i",
    syms->count, a->count-1);
  for (int i = 0; i < syms->count; i++)
  {
    if (strcmp(func, "def") == 0)
    {
      lenv_def(e, syms->cell[i], a->cell[i + 1]);
    }
    if (strcmp(func, "=") == 0)
    {
      lenv_put(e, syms->cell[i], a->cell[i + 1]);
    }
  }
  lval_del(a);
  return lval_sexpr();
}
Example #2
0
lval* builtin_var(lenv* e, lval* a, char* func)
{
  LASSERT_TYPE(func, a, 0, LVAL_QEXPR);

  lval* syms = a->cell[0];

  for (int i = 0; i < syms->count; i++)
    LASSERT(a, syms->cell[i]->type == LVAL_SYM,
      "Function '%s' cannot define a non-symbol.  Got %s, Expected %s.",
      func, ltype_name(syms->cell[i]->type), ltype_name(LVAL_SYM));

  LASSERT(a, syms->count == a->count - 1,
    "Function '%s' passed too many arguments for symbols.  Got %i, Expected %i.",
    func, syms->count, a->count - 1);

  for (int i = 0; i < syms->count; i++)
  {
    if (strcmp(func, "def") == 0)
      lenv_def(e, syms->cell[i], a->cell[i+1]);
    
    if (strcmp(func, "=") == 0)
      lenv_put(e, syms->cell[i], a->cell[i+1]);
  }

  lval_del(a);
  return lval_sexpr();
}
Example #3
0
struct lval* lval_builtin_def(struct lenv* e, struct lval* v) {
    LTYPE(v, LVAL_QEXP, 0, "def");

    struct lval* syms = v->cell[0];

    for (int i = 0; i < syms->count; i++) {
        LASSERT(v, syms->cell[i]->type == LVAL_SYM,
                "'def' expects variable %i to be symbol", i);
    }

    LASSERT(v, syms->count == (v->count - 1),
            "'def' expects same variable & value count. "
            "Got %i variables and %i values.",
            syms->count, v->count -1);

    for (int i = 0; i < syms->count; i++) {
        char* sym = syms->cell[i]->sym;
        struct lval* x = lenv_get(e, sym);
        if (x->type == LVAL_FUN && x->fun_type == LVAL_FUN_BUILTIN) {
            struct lval* err = lval_err(
                                   "Cannot redefine builtin function '%s'", sym);
            lval_del(v);
            return err;
        }

        lenv_def(e, sym, v->cell[i + 1]);
    }

    return lval_take(v, 0);
}