Archived
1
0
Fork 0

Added builtin len functionality for Q-expressions

This commit is contained in:
Brandon Rozek 2018-06-07 20:22:06 -04:00
parent a485e65838
commit 3c27644903
3 changed files with 14 additions and 2 deletions

View file

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "expressions.h" #include "expressions.h"
#include "numbers.h"
#include "operations.h" #include "operations.h"
#include "error.h" #include "error.h"
@ -143,3 +144,11 @@ lval* builtin_join(lval* a) {
lval_del(a); lval_del(a);
return x; return x;
} }
lval* builtin_len(lval* a) {
LASSERT(a, a->cell[0]->type == LVAL_QEXPR, "Function 'len' passed incorrect type")
lval* x = lval_long(a->cell[0]->count);
lval_del(a);
return x;
}

View file

@ -36,6 +36,6 @@ lval* builtin_list(lval* a);
lval* builtin_eval(lval* a); lval* builtin_eval(lval* a);
lval* lval_join(lval* x, lval* y); lval* lval_join(lval* x, lval* y);
lval* builtin_join(lval* a); lval* builtin_join(lval* a);
lval* builtin_len(lval* a);
#endif #endif

View file

@ -55,7 +55,9 @@ int main (int argc, char** argv) {
"double : <long> '.' <number>; " "double : <long> '.' <number>; "
"symbol : '+' | '-' | '*' | '/' | '%' \ "symbol : '+' | '-' | '*' | '/' | '%' \
| '^' | \"min\" | \"max\" \ | '^' | \"min\" | \"max\" \
| \"list\" | \"head\" | \"tail\" | \"join\" | \"eval\";" | \"list\" | \"head\" | \"tail\" \
| \"join\" | \"eval\" | \"len\" \
| \"init\" | \"cons\"; "
"sexpr : '(' <expr>* ')'; " "sexpr : '(' <expr>* ')'; "
"qexpr : '{' <expr>* '}'; " "qexpr : '{' <expr>* '}'; "
@ -166,6 +168,7 @@ lval* builtin(lval* a, char* func) {
if (strcmp("tail", func) == 0) { return builtin_tail(a); } if (strcmp("tail", func) == 0) { return builtin_tail(a); }
if (strcmp("join", func) == 0) { return builtin_join(a); } if (strcmp("join", func) == 0) { return builtin_join(a); }
if (strcmp("eval", func) == 0) { return builtin_eval(a); } if (strcmp("eval", func) == 0) { return builtin_eval(a); }
if (strcmp("len", func) == 0) { return builtin_len(a); }
if (strstr("+-/*^%", func)) { return builtin_op(a, func); } if (strstr("+-/*^%", func)) { return builtin_op(a, func); }
if (strcmp("min", func) == 0) { 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); } if (strcmp("max", func) == 0) { return builtin_op(a, func); }