Archived
1
0
Fork 0

Removed uneeded builtin function, functions with no arguments either evaluate or reprint. Added ls to show all variables in environment

This commit is contained in:
Brandon Rozek 2018-06-09 22:47:19 -04:00
parent c3065702e3
commit 7382155034
4 changed files with 26 additions and 17 deletions

View file

@ -123,4 +123,16 @@ lval* builtin_def(lenv* e, lval* a) {
lval_del(a);
return lval_sexpr();
}
lval* builtin_ls(lenv* e, lval* a) {
LASSERT(a, a->count == 0, "Function 'ls' passed an incorrect number of arguments. Got %i, expected %i.", a->count, 0)
lval* x = lval_qexpr();
for (int i = 0; i < e->count; i++) {
lval_add(x, lval_sym(e->syms[i]));
}
lval_del(a);
return x;
}

View file

@ -22,6 +22,7 @@ void lenv_add_builtin(lenv* e, char* name, lbuiltin func);
void lenv_add_builtins(lenv* e);
lval* builtin_def(lenv* e, lval* a);
lval* builtin_ls(lenv* e, lval* a);
#endif

View file

@ -55,6 +55,19 @@ lval* lval_take(lval* v, int i) {
}
lval* lval_eval_sexpr(lenv* e, lval* v) {
// No argument functions
if (v->count == 1) {
lval* x = lenv_get(e, v->cell[0]);
if (x->type == LVAL_FUN) {
lval_del(x);
if (strcmp(v->cell[0]->sym, "ls") == 0) {
lval_del(v);
return builtin_ls(e, lval_sexpr());
}
}
return v;
}
// Evaluate children
for (int i = 0; i < v->count; i++) {
v->cell[i] = lval_eval(e, v->cell[i]);

View file

@ -158,20 +158,3 @@ lval* builtin_op(lenv* e, lval* a, char* op) {
lval_del(a);
return x;
}
lval* builtin(lenv* e, lval* a, char* func) {
if (strcmp("list", func) == 0) { return builtin_list(e, a); }
if (strcmp("head", func) == 0) { return builtin_head(e, a); }
if (strcmp("tail", func) == 0) { return builtin_tail(e, a); }
if (strcmp("join", func) == 0) { return builtin_join(e, a); }
if (strcmp("eval", func) == 0) { return builtin_eval(e, a); }
if (strcmp("len", func) == 0) { return builtin_len(e, a); }
if (strcmp("init", func) == 0) { return builtin_init(e, a); }
if (strcmp("cons", func) == 0) { return builtin_cons(e, a); }
if (strstr("+-/*^%", func)) { return builtin_op(e, a, func); }
if (strcmp("min", func) == 0) { return builtin_op(e, a, func); }
if (strcmp("max", func) == 0) { return builtin_op(e, a, func); }
lval_del(a);
return lval_err("Unknown Function %s", func);
}