Archived
1
0
Fork 0

Added builtin cons operation

This commit is contained in:
Brandon Rozek 2018-06-09 10:16:03 -04:00
parent cd75e60fb9
commit a47f72f510
4 changed files with 17 additions and 1 deletions

View file

@ -165,3 +165,18 @@ lval* builtin_len(lval* a) {
lval_del(a);
return x;
}
lval* builtin_cons(lval* a) {
LASSERT(a, a->cell[0]->type != LVAL_QEXPR, "Function 'cons' passed incorrect type on first argument")
LASSERT(a, a->cell[1]->type == LVAL_QEXPR, "Function 'cons' passed incorrect type on second argument")
LASSERT(a, a->count == 2, "Function 'cons' passed an incorrect number of arguments")
lval* x = lval_qexpr();
x = lval_add(x, lval_pop(a, 0));
x = lval_join(x, lval_pop(a, 0));
lval_del(a);
return x;
}

View file

@ -41,5 +41,6 @@ lval* builtin_eval(lval* a);
lval* lval_join(lval* x, lval* y);
lval* builtin_join(lval* a);
lval* builtin_len(lval* a);
lval* builtin_cons(lval* a);
#endif

View file

@ -50,7 +50,6 @@ lval* lval_eval(lval* v) {
return v;
}
void lval_del(lval* v) {
switch (v->type) {
case LVAL_LONG: break;

View file

@ -170,6 +170,7 @@ lval* builtin(lval* a, char* func) {
if (strcmp("eval", func) == 0) { return builtin_eval(a); }
if (strcmp("len", func) == 0) { return builtin_len(a); }
if (strcmp("init", func) == 0) { return builtin_init(a); }
if (strcmp("cons", func) == 0) { return builtin_cons(a); }
if (strstr("+-/*^%", func)) { return builtin_op(a, func); }
if (strcmp("min", func) == 0) { return builtin_op(a, func); }
if (strcmp("max", func) == 0) { return builtin_op(a, func); }