aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2012-05-13 01:32:14 (CEST)
committerp4bl0 <r _at_ uzy .dot. me>2012-05-13 01:32:14 (CEST)
commit2502b2ff157242a512e7287e74076f7fbc2d9348 (patch)
tree19f7ca6c4b661aecacc71bf901aa3320eea231e5
parent4a654e6437836c29b5b33d88052fd2f8e0a53cb1 (diff)
downloadelispon-2502b2ff157242a512e7287e74076f7fbc2d9348.zip
elispon-2502b2ff157242a512e7287e74076f7fbc2d9348.tar.gz
length primitive now works with string and vector too
-rw-r--r--src/primitive.c2
-rw-r--r--src/primitives/core.c28
-rw-r--r--src/primitives/list.c13
3 files changed, 29 insertions, 14 deletions
diff --git a/src/primitive.c b/src/primitive.c
index fd42947..aef080a 100644
--- a/src/primitive.c
+++ b/src/primitive.c
@@ -92,12 +92,12 @@ Primitive prim_[PRIMITIVE_COUNT] = {
{ "eval", PrimitiveProc_eval },
{ "vau", PrimitiveProc_vau },
{ "environment", PrimitiveProc_environment },
+ { "length", PrimitiveProc_length },
{ "cons", PrimitiveProc_cons },
{ "car", PrimitiveProc_car },
{ "cdr", PrimitiveProc_cdr },
{ "list", PrimitiveProc_list },
- { "length", PrimitiveProc_length },
{ "vector", PrimitiveProc_vector },
diff --git a/src/primitives/core.c b/src/primitives/core.c
index d4547a7..e4cb33e 100644
--- a/src/primitives/core.c
+++ b/src/primitives/core.c
@@ -161,3 +161,31 @@ PrimitiveProc_environment (Expression *args, Environment **env, Eval *ev)
return Expression_new(ENVIRONMENT, Environment_copy(*env));
}
+
+static Expression *
+PrimitiveProc_length (Expression *args, Environment **env, Eval *ev)
+{
+ Expression *expr = NULL;
+ int length;
+
+ nb_args("length", 1, args);
+
+ if ((expr = Eval_eval(ev, car(args), env)) == NULL)
+ return NULL;
+
+ switch (Expression_type(expr)) {
+ case PAIR:
+ length = Expression_length(expr);
+ break;
+ case STRING:
+ length = strlen(String_buf(Expression_expr(expr)));
+ break;
+ case VECTOR:
+ length = Vector_size(Expression_expr(expr));
+ break;
+ default:
+ length = 0;
+ }
+
+ return Expression_new(NUMBER, Number_new(length));
+}
diff --git a/src/primitives/list.c b/src/primitives/list.c
index cbfd619..ad54759 100644
--- a/src/primitives/list.c
+++ b/src/primitives/list.c
@@ -79,16 +79,3 @@ PrimitiveProc_list (Expression *args, Environment **env, Eval *ev)
return list;
}
-
-static Expression *
-PrimitiveProc_length (Expression *args, Environment **env, Eval *ev)
-{
- Expression *list = NULL;
-
- nb_args("length", 1, args);
-
- if ((list = Eval_eval(ev, car(args), env)) == NULL)
- return NULL;
-
- return Expression_new(NUMBER, Number_new(Expression_length(list)));
-}
Pablo Rauzy — generated by cgit