aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2012-05-19 11:53:28 (CEST)
committerp4bl0 <r _at_ uzy .dot. me>2012-05-19 11:53:28 (CEST)
commit3a8536a8e63bd64831010ed49671c4eb52d33112 (patch)
tree1d285603f5b932e49e9803d37585e1a0e619673e
parentccaf95d6d33753fec2416cd3e172c6a2524e69d2 (diff)
downloadelispon-3a8536a8e63bd64831010ed49671c4eb52d33112.zip
elispon-3a8536a8e63bd64831010ed49671c4eb52d33112.tar.gz
add string->list primitive
-rw-r--r--src/primitive.c1
-rw-r--r--src/primitive.h2
-rw-r--r--src/primitives/conversion.c27
-rw-r--r--src/primitives/core.c2
-rw-r--r--src/string.c18
-rw-r--r--src/string.h3
6 files changed, 49 insertions, 4 deletions
diff --git a/src/primitive.c b/src/primitive.c
index 280ef48..bf3e563 100644
--- a/src/primitive.c
+++ b/src/primitive.c
@@ -139,6 +139,7 @@ Primitive prim_[PRIMITIVE_COUNT] = {
{ "string->symbol", PrimitiveProc_string_to_symbol },
{ "number->string", PrimitiveProc_number_to_string },
{ "string->number", PrimitiveProc_string_to_number },
+ { "string->list", PrimitiveProc_string_to_list },
{ "vector->list", PrimitiveProc_vector_to_list },
{ "list->vector", PrimitiveProc_list_to_vector },
diff --git a/src/primitive.h b/src/primitive.h
index 1522bf0..1e394e1 100644
--- a/src/primitive.h
+++ b/src/primitive.h
@@ -7,7 +7,7 @@
#include "environment.h"
#include "eval.h"
-#define PRIMITIVE_COUNT 52
+#define PRIMITIVE_COUNT 53
typedef struct Primitive Primitive;
typedef Expression *(*PrimitiveProc)(Expression *, Environment **, Eval *);
diff --git a/src/primitives/conversion.c b/src/primitives/conversion.c
index 6bc332a..8d4fdc0 100644
--- a/src/primitives/conversion.c
+++ b/src/primitives/conversion.c
@@ -77,6 +77,33 @@ PrimitiveProc_number_to_string (Expression *args, Environment **env, Eval *ev)
}
static Expression *
+PrimitiveProc_string_to_list (Expression *args, Environment **env, Eval *ev)
+{
+ Expression *expr = NULL, *list = NULL;
+ String *string = NULL;
+ int i;
+
+ nb_args("string->list", 1, args);
+
+ if ((expr = Eval_eval(ev, car(args), env)) == NULL)
+ return NULL;
+
+ if (Expression_type(expr) != STRING) {
+ Utils_error("string->list: expected string");
+ return NULL;
+ }
+
+ string = Expression_expr(expr);
+ list = Expression_new(NIL, NULL);
+ for (i = String_length(string) - 1; i >= 0; i--)
+ list = cons(Expression_new(CHARACTER,
+ Character_new(String_getChar(string, i))),
+ list);
+
+ return list;
+}
+
+static Expression *
PrimitiveProc_vector_to_list (Expression *args, Environment **env, Eval *ev)
{
Expression *expr = NULL, *list = NULL;
diff --git a/src/primitives/core.c b/src/primitives/core.c
index e4cb33e..f5c1e84 100644
--- a/src/primitives/core.c
+++ b/src/primitives/core.c
@@ -178,7 +178,7 @@ PrimitiveProc_length (Expression *args, Environment **env, Eval *ev)
length = Expression_length(expr);
break;
case STRING:
- length = strlen(String_buf(Expression_expr(expr)));
+ length = String_length(Expression_expr(expr));
break;
case VECTOR:
length = Vector_size(Expression_expr(expr));
diff --git a/src/string.c b/src/string.c
index e3caeba..4ade9c0 100644
--- a/src/string.c
+++ b/src/string.c
@@ -14,8 +14,8 @@ String_new (const char *str)
alloc_one(self);
- self->size = strlen(str) + 1;
- alloc_(self->buf, self->size);
+ self->size = strlen(str);
+ alloc_(self->buf, self->size + 1);
strcpy(self->buf, str);
return self;
@@ -37,3 +37,17 @@ String_buf (String *self)
{
return self->buf;
}
+
+int
+String_length (String *self)
+{
+ return self->size;
+}
+
+/* ----- */
+
+char
+String_getChar (String *self, int i)
+{
+ return self->buf[i];
+}
diff --git a/src/string.h b/src/string.h
index 755030c..ed6a306 100644
--- a/src/string.h
+++ b/src/string.h
@@ -7,5 +7,8 @@ String *String_new (const char *str);
void String_delete (String *self);
char *String_buf (String *self);
+int String_length (String *self);
+
+char String_getChar (String *self, int i);
#endif /* STRING_H__ */
Pablo Rauzy — generated by cgit