aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2012-05-19 00:08:38 (CEST)
committerp4bl0 <r _at_ uzy .dot. me>2012-05-19 00:08:38 (CEST)
commitccaf95d6d33753fec2416cd3e172c6a2524e69d2 (patch)
tree58ea0409d457db9f5bfaa0eaede92af423b08cfa
parentde0ba545d38aa2cfe3c9a4d7b1b19000862fcbef (diff)
downloadelispon-ccaf95d6d33753fec2416cd3e172c6a2524e69d2.zip
elispon-ccaf95d6d33753fec2416cd3e172c6a2524e69d2.tar.gz
add vector->list and list->vector primitives
-rw-r--r--src/primitive.c2
-rw-r--r--src/primitive.h2
-rw-r--r--src/primitives/conversion.c55
3 files changed, 58 insertions, 1 deletions
diff --git a/src/primitive.c b/src/primitive.c
index aef080a..280ef48 100644
--- a/src/primitive.c
+++ b/src/primitive.c
@@ -139,6 +139,8 @@ Primitive prim_[PRIMITIVE_COUNT] = {
{ "string->symbol", PrimitiveProc_string_to_symbol },
{ "number->string", PrimitiveProc_number_to_string },
{ "string->number", PrimitiveProc_string_to_number },
+ { "vector->list", PrimitiveProc_vector_to_list },
+ { "list->vector", PrimitiveProc_list_to_vector },
{ "%open-fexpr%", PrimitiveProc_open_fexpr },
{ "%open-struct%", PrimitiveProc_open_struct }
diff --git a/src/primitive.h b/src/primitive.h
index d27dde6..1522bf0 100644
--- a/src/primitive.h
+++ b/src/primitive.h
@@ -7,7 +7,7 @@
#include "environment.h"
#include "eval.h"
-#define PRIMITIVE_COUNT 50
+#define PRIMITIVE_COUNT 52
typedef struct Primitive Primitive;
typedef Expression *(*PrimitiveProc)(Expression *, Environment **, Eval *);
diff --git a/src/primitives/conversion.c b/src/primitives/conversion.c
index 9b15e6a..6bc332a 100644
--- a/src/primitives/conversion.c
+++ b/src/primitives/conversion.c
@@ -75,3 +75,58 @@ PrimitiveProc_number_to_string (Expression *args, Environment **env, Eval *ev)
return Expression_new(STRING, String_new(str));
}
+
+static Expression *
+PrimitiveProc_vector_to_list (Expression *args, Environment **env, Eval *ev)
+{
+ Expression *expr = NULL, *list = NULL;
+ Vector *vector = NULL;
+ int i;
+
+ nb_args("vector->list", 1, args);
+
+ if ((expr = Eval_eval(ev, car(args), env)) == NULL)
+ return NULL;
+
+ if (Expression_type(expr) != VECTOR) {
+ Utils_error("vector->list: expected vector");
+ return NULL;
+ }
+
+ vector = Expression_expr(expr);
+ list = Expression_new(NIL, NULL);
+ for (i = Vector_size(vector) - 1; i >= 0; i--)
+ list = cons(Vector_get(vector, i), list);
+
+ return list;
+}
+
+static Expression *
+PrimitiveProc_list_to_vector (Expression *args, Environment **env, Eval *ev)
+{
+ Expression *expr = NULL;
+ Vector *vector = NULL;
+ int i, length;
+
+ nb_args("list->vector", 1, args);
+
+ if ((expr = Eval_eval(ev, car(args), env)) == NULL)
+ return NULL;
+
+ if (Expression_type(expr) != PAIR && Expression_type(expr) != NIL) {
+ Utils_error("list->vector: expected list");
+ return NULL;
+ }
+
+ i = 0;
+ length = Expression_length(expr);
+ vector = Vector_new(length);
+ if (length > 0) {
+ while (Expression_type(expr) == PAIR) {
+ Vector_set(vector, i++, car(expr));
+ expr = cdr(expr);
+ }
+ }
+
+ return Expression_new(VECTOR, vector);
+}
Pablo Rauzy — generated by cgit