mumble

A Lisp written in C, following the *Build Your Own Lisp* book
Log | Files | Refs | README

commit 0049079be2255013051a2f654e3e8cb1eafe29d4
parent a886aeb976188e88b317c6e1cf49ca62d250afef
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Tue,  2 May 2023 10:09:41 -0400

feat: add list length function

Diffstat:
Mmumble | 0
Msrc/mumble.c | 17++++++++++++++++-
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/mumble b/mumble Binary files differ. diff --git a/src/mumble.c b/src/mumble.c @@ -407,6 +407,19 @@ lispval* builtin_list(lispval* v) // Returns something that is independent of the input: yes. } +lispval* builtin_len(lispval* v) +{ + // tail { 1 2 3 } + LISPVAL_ASSERT(v->count == 1, "Error: function len passed too many arguments"); + + lispval* source = v->cell[0]; + LISPVAL_ASSERT(source->type == LISPVAL_QEXPR, "Error: Argument passed to len is not a q-expr, i.e., a bracketed list."); + lispval* new = lispval_num(source->count); + return new; + // Returns something that should be freed later: yes. + // Returns something that doesn't share pointers with the input: yes. +} + lispval* evaluate_lispval(lispval* l); lispval* builtin_eval(lispval* v) { @@ -509,6 +522,8 @@ lispval* builtin_functions(char* func, lispval* v) return builtin_join(v); } else if (strcmp("eval", func) == 0) { return builtin_eval(v); + } else if (strcmp("len", func) == 0) { + return builtin_len(v); } else if (strstr("+-/*", func)) { return builtin_math_ops(func, v); } else { @@ -573,7 +588,7 @@ int main(int argc, char** argv) mpca_lang(MPCA_LANG_DEFAULT, " \ number : /-?[0-9]+\\.?([0-9]+)?/ ; \ symbol : \"list\" | \"head\" | \"tail\" \ - | \"eval\" | \"join\" \ + | \"eval\" | \"join\" | \"len\" \ | '+' | '-' | '*' | '/' ; \ sexpr : '(' <expr>* ')' ; \ qexpr : '{' <expr>* '}' ; \