altos/scheme: add make-string builtin
[fw/altos] / src / scheme / ao_scheme_string.c
index e18a8e854df8ded0dfeef1ede11f757a61dd5a28..2c636d7ae49aaba9f518de2f2eca7ecdc8a3d3d4 100644 (file)
@@ -51,6 +51,7 @@ ao_scheme_string_alloc(int len)
        if (!s)
                return NULL;
        s->type = AO_SCHEME_STRING;
+       s->val[len] = '\0';
        return s;
 }
 
@@ -60,9 +61,9 @@ ao_scheme_string_copy(struct ao_scheme_string *a)
        int                     alen = strlen(a->val);
        struct ao_scheme_string *r;
 
-       ao_scheme_string_stash(0, a);
+       ao_scheme_string_stash(a);
        r = ao_scheme_string_alloc(alen);
-       a = ao_scheme_string_fetch(0);
+       a = ao_scheme_string_fetch();
        if (!r)
                return NULL;
        strcpy(r->val, a->val);
@@ -70,7 +71,19 @@ ao_scheme_string_copy(struct ao_scheme_string *a)
 }
 
 struct ao_scheme_string *
-ao_scheme_string_make(char *a)
+ao_scheme_make_string(int32_t len, char fill)
+{
+       struct ao_scheme_string *r;
+
+       r = ao_scheme_string_alloc(len);
+       if (!r)
+               return NULL;
+       memset(r->val, fill, len);
+       return r;
+}
+
+struct ao_scheme_string *
+ao_scheme_string_new(char *a)
 {
        struct ao_scheme_string *r;
 
@@ -87,9 +100,9 @@ ao_scheme_atom_to_string(struct ao_scheme_atom *a)
        int                     alen = strlen(a->name);
        struct ao_scheme_string *r;
 
-       ao_scheme_poly_stash(0, ao_scheme_atom_poly(a));
+       ao_scheme_atom_stash(a);
        r = ao_scheme_string_alloc(alen);
-       a = ao_scheme_poly_atom(ao_scheme_poly_fetch(0));
+       a = ao_scheme_atom_fetch();
        if (!r)
                return NULL;
        strcpy(r->val, a->name);
@@ -103,11 +116,11 @@ ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b)
        int                             blen = strlen(b->val);
        struct ao_scheme_string         *r;
 
-       ao_scheme_string_stash(0, a);
-       ao_scheme_string_stash(1, b);
+       ao_scheme_string_stash(a);
+       ao_scheme_string_stash(b);
        r = ao_scheme_string_alloc(alen + blen);
-       a = ao_scheme_string_fetch(0);
-       b = ao_scheme_string_fetch(1);
+       b = ao_scheme_string_fetch();
+       a = ao_scheme_string_fetch();
        if (!r)
                return NULL;
        strcpy(r->val, a->val);
@@ -123,9 +136,9 @@ ao_scheme_string_pack(struct ao_scheme_cons *cons)
        int                     len;
 
        len = ao_scheme_cons_length(cons);
-       ao_scheme_cons_stash(0, cons);
+       ao_scheme_cons_stash(cons);
        r = ao_scheme_string_alloc(len);
-       cons = ao_scheme_cons_fetch(0);
+       cons = ao_scheme_cons_fetch();
        if (!r)
                return AO_SCHEME_NIL;
        rval = r->val;
@@ -138,7 +151,6 @@ ao_scheme_string_pack(struct ao_scheme_cons *cons)
                        return ao_scheme_error(AO_SCHEME_INVALID, "non-int passed to pack");
                cons = ao_scheme_cons_cdr(cons);
        }
-       *rval++ = 0;
        return ao_scheme_string_poly(r);
 }
 
@@ -151,13 +163,13 @@ ao_scheme_string_unpack(struct ao_scheme_string *a)
 
        for (i = 0; (c = a->val[i]); i++) {
                struct ao_scheme_cons   *n;
-               ao_scheme_cons_stash(0, cons);
-               ao_scheme_cons_stash(1, tail);
-               ao_scheme_string_stash(0, a);
+               ao_scheme_cons_stash(cons);
+               ao_scheme_cons_stash(tail);
+               ao_scheme_string_stash(a);
                n = ao_scheme_cons_cons(ao_scheme_int_poly(c), AO_SCHEME_NIL);
-               a = ao_scheme_string_fetch(0);
-               cons = ao_scheme_cons_fetch(0);
-               tail = ao_scheme_cons_fetch(1);
+               a = ao_scheme_string_fetch();
+               tail = ao_scheme_cons_fetch();
+               cons = ao_scheme_cons_fetch();
 
                if (!n) {
                        cons = NULL;
@@ -173,42 +185,54 @@ ao_scheme_string_unpack(struct ao_scheme_string *a)
 }
 
 void
-ao_scheme_string_write(ao_poly p)
+ao_scheme_string_write(ao_poly p, bool write)
 {
        struct ao_scheme_string *s = ao_scheme_poly_string(p);
        char                    *sval = s->val;
        char                    c;
 
-       putchar('"');
-       while ((c = *sval++)) {
-               switch (c) {
-               case '\n':
-                       printf ("\\n");
-                       break;
-               case '\r':
-                       printf ("\\r");
-                       break;
-               case '\t':
-                       printf ("\\t");
-                       break;
-               default:
-                       if (c < ' ')
-                               printf("\\%03o", c);
-                       else
-                               putchar(c);
-                       break;
+       if (write) {
+               putchar('"');
+               while ((c = *sval++)) {
+                       switch (c) {
+                       case '\a':
+                               printf("\\a");
+                               break;
+                       case '\b':
+                               printf("\\b");
+                               break;
+                       case '\t':
+                               printf ("\\t");
+                               break;
+                       case '\n':
+                               printf ("\\n");
+                               break;
+                       case '\r':
+                               printf ("\\r");
+                               break;
+                       case '\f':
+                               printf("\\f");
+                               break;
+                       case '\v':
+                               printf("\\v");
+                               break;
+                       case '\"':
+                               printf("\\\"");
+                               break;
+                       case '\\':
+                               printf("\\\\");
+                               break;
+                       default:
+                               if (c < ' ')
+                                       printf("\\%03o", c);
+                               else
+                                       putchar(c);
+                               break;
+                       }
                }
+               putchar('"');
+       } else {
+               while ((c = *sval++))
+                       putchar(c);
        }
-       putchar('"');
-}
-
-void
-ao_scheme_string_display(ao_poly p)
-{
-       struct ao_scheme_string *s = ao_scheme_poly_string(p);
-       char                    *sval = s->val;
-       char                    c;
-
-       while ((c = *sval++))
-               putchar(c);
 }