From cd75e60fb94ee76d1dc58a1a8f624eefb93b1372 Mon Sep 17 00:00:00 2001 From: Brandon Rozek Date: Thu, 7 Jun 2018 20:33:56 -0400 Subject: [PATCH] Added builtin init functionality for Q-expressions --- lval/expressions.c | 17 +++++++++++++++-- lval/expressions.h | 8 ++++++-- prompt.c | 1 + 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lval/expressions.c b/lval/expressions.c index 3c4f579..77fc9e3 100644 --- a/lval/expressions.c +++ b/lval/expressions.c @@ -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") diff --git a/lval/expressions.h b/lval/expressions.h index 9bda991..5893a01 100644 --- a/lval/expressions.h +++ b/lval/expressions.h @@ -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); diff --git a/prompt.c b/prompt.c index 9a5d647..eb08752 100644 --- a/prompt.c +++ b/prompt.c @@ -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); }