feat: add list length function

This commit is contained in:
NunoSempere 2023-05-02 10:09:41 -04:00
parent a886aeb976
commit 0049079be2
2 changed files with 16 additions and 1 deletions

BIN
mumble

Binary file not shown.

View File

@ -407,6 +407,19 @@ lispval* builtin_list(lispval* v)
// Returns something that is independent of the input: yes. // 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* evaluate_lispval(lispval* l);
lispval* builtin_eval(lispval* v) lispval* builtin_eval(lispval* v)
{ {
@ -509,6 +522,8 @@ lispval* builtin_functions(char* func, lispval* v)
return builtin_join(v); return builtin_join(v);
} else if (strcmp("eval", func) == 0) { } else if (strcmp("eval", func) == 0) {
return builtin_eval(v); return builtin_eval(v);
} else if (strcmp("len", func) == 0) {
return builtin_len(v);
} else if (strstr("+-/*", func)) { } else if (strstr("+-/*", func)) {
return builtin_math_ops(func, v); return builtin_math_ops(func, v);
} else { } else {
@ -573,7 +588,7 @@ int main(int argc, char** argv)
mpca_lang(MPCA_LANG_DEFAULT, " \ mpca_lang(MPCA_LANG_DEFAULT, " \
number : /-?[0-9]+\\.?([0-9]+)?/ ; \ number : /-?[0-9]+\\.?([0-9]+)?/ ; \
symbol : \"list\" | \"head\" | \"tail\" \ symbol : \"list\" | \"head\" | \"tail\" \
| \"eval\" | \"join\" \ | \"eval\" | \"join\" | \"len\" \
| '+' | '-' | '*' | '/' ; \ | '+' | '-' | '*' | '/' ; \
sexpr : '(' <expr>* ')' ; \ sexpr : '(' <expr>* ')' ; \
qexpr : '{' <expr>* '}' ; \ qexpr : '{' <expr>* '}' ; \