Archived
1
0
Fork 0

Added builtin init functionality for Q-expressions

This commit is contained in:
Brandon Rozek 2018-06-07 20:33:56 -04:00
parent 3c27644903
commit cd75e60fb9
3 changed files with 22 additions and 4 deletions

View file

@ -85,16 +85,29 @@ lval* lval_eval_sexpr(lval* v) {
}
lval* builtin_head(lval* a) {
lval* builtin_headn(lval* a, int n) {
LASSERT(a, a->count == 1, "Function 'head' passed too many arguments")
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, "Function 'head' passed incorrect type")
LASSERT(a, a->cell[0]->count != 0, "Function 'head' passed {}")
lval* v = lval_take(a, 0);
while (v->count > 1) { lval_del(lval_pop(v, 1)); }
while (v->count > n) { lval_del(lval_pop(v, v->count - 1)); }
return v;
}
lval* builtin_head(lval* a) {
return builtin_headn(a, 1);
}
lval* builtin_init(lval* a) {
LASSERT(a, a->count == 1, "Function 'init' passed too many arguments")
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, "Function 'init' passed incorrect type")
LASSERT(a, a->cell[0]->count != 0, "Function 'init' passed {}")
return builtin_headn(a, a->cell[0]->count - 1);
}
lval* builtin_tail(lval* a) {
LASSERT(a, a->count == 1, "Function 'tail' passed too many arguments")
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, "Function 'tail' passed incorrect type")

View file

@ -24,13 +24,17 @@ lval* lval_eval_sexpr(lval* v);
/*
QEXPR Operations
Head: Takes a Q-Expression and returns a Q-Expression with only of the first element
Head: Takes a Q-Expression and returns a Q-Expression with only the first element
Tail: Takes a Q-Expression and returns a Q-Expression with the first element removed
List: Takes one or more arguments and returns a new Q-Expression containing the arguments
Eval: Takes a Q-Expression and evaluates it as if it were a S-Expression
Join: join Takes one or more Q-Expressions and returns a Q-Expression of them conjoined together
Join: Takes one or more Q-Expressions and returns a Q-Expression of them conjoined together
Len: Takes a Q-Expression and returns the length of the Q-Expression
Init: Takes a Q-Expression and returns a Q-Expression with only the last element removed
*/
lval* builtin_headn(lval* a, int n);
lval* builtin_head(lval* a);
lval* builtin_init(lval* a);
lval* builtin_tail(lval* a);
lval* builtin_list(lval* a);
lval* builtin_eval(lval* a);

View file

@ -169,6 +169,7 @@ lval* builtin(lval* a, char* func) {
if (strcmp("join", func) == 0) { return builtin_join(a); }
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 (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); }