|
|
@ -418,20 +418,14 @@ lval* builtin_op(lval* a, char* op) { |
|
|
|
lval* builtin_head(lval* a) { |
|
|
|
|
|
|
|
/* Check error conditions */ |
|
|
|
if (a->count != 1) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'head' passed too many arguments"); |
|
|
|
} |
|
|
|
LASSERT(a, a->count == 1, |
|
|
|
"Function 'head' passed too many arguments!"); |
|
|
|
|
|
|
|
if (a->cell[0]->type != LVAL_QEXPR) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'head' passed incorrect types!"); |
|
|
|
} |
|
|
|
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, |
|
|
|
"Function 'head' passed incorrect type!"); |
|
|
|
|
|
|
|
if (a->cell[0]->count == 0) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'head' passed empty {}!"); |
|
|
|
} |
|
|
|
LASSERT(a, a->cell[0]->count != 0, |
|
|
|
"Function 'head' passed {}!"); |
|
|
|
|
|
|
|
/* Otherwise take first argument */ |
|
|
|
lval* v = lval_take(a, 0); |
|
|
@ -449,20 +443,14 @@ lval* builtin_head(lval* a) { |
|
|
|
lval* builtin_tail(lval* a) { |
|
|
|
|
|
|
|
/* Check error conditions */ |
|
|
|
if (a->count != 1) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'tail' passed too many arguments"); |
|
|
|
} |
|
|
|
LASSERT(a, a->count == 1, |
|
|
|
"Function 'tail' passed too many arguments!"); |
|
|
|
|
|
|
|
if (a->cell[0]->type != LVAL_QEXPR) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'tail' passed incorrect types!"); |
|
|
|
} |
|
|
|
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, |
|
|
|
"Function 'tail' passed incorrect type!"); |
|
|
|
|
|
|
|
if (a->cell[0]->count == 0) { |
|
|
|
lval_del(a); |
|
|
|
return lval_err("Function 'tail' passed empty {}!"); |
|
|
|
} |
|
|
|
LASSERT(a, a->cell[0]->count != 0, |
|
|
|
"Function 'tail' passed {}!"); |
|
|
|
|
|
|
|
/* Otherwise take first argument */ |
|
|
|
lval* v = lval_take(a, 0); |
|
|
@ -475,6 +463,64 @@ lval* builtin_tail(lval* a) { |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lval* builtin_list(lval* a) { |
|
|
|
|
|
|
|
a->type = LVAL_QEXPR; |
|
|
|
return a; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lval* builtin_eval(lval* a) { |
|
|
|
|
|
|
|
/* Check error conditions */ |
|
|
|
LASSERT(a, a->count == 1, |
|
|
|
"Function 'eval' passed too many arguments!"); |
|
|
|
|
|
|
|
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, |
|
|
|
"Function 'eval' passed incorrect type!"); |
|
|
|
|
|
|
|
lval* x = lval_take(a, 0); |
|
|
|
x->type = LVAL_SEXPR; |
|
|
|
return lval_eval(x); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lval* builtin_join(lval* a) { |
|
|
|
|
|
|
|
/* Check error conditions */ |
|
|
|
for (size_t i = 0; i < a->count; i++) { |
|
|
|
LASSERT(a, a->cell[i]->type == LVAL_QEXPR, |
|
|
|
"Function 'join' passed incorrect type!"); |
|
|
|
} |
|
|
|
|
|
|
|
lval* x = lval_pop(a, 0); |
|
|
|
|
|
|
|
while (a->count) { |
|
|
|
x = lval_join(x, lval_pop(a, 0)); |
|
|
|
} |
|
|
|
|
|
|
|
lval_del(a); |
|
|
|
return x; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lval* lval_join(lval* x, lval* y) { |
|
|
|
|
|
|
|
/* For each cell in y add it to x */ |
|
|
|
while (y->count) { |
|
|
|
x = lval_add(x, lval_pop(y, 0)); |
|
|
|
} |
|
|
|
|
|
|
|
/* Delete the empty y and return x */ |
|
|
|
lval_del(y); |
|
|
|
return x; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
long min(long x, long y) { |
|
|
|
if (x <= y) { |
|
|
|
return x; |
|
|
|