altos/scheme: Allow make-vector value param to be optional
[fw/altos] / src / scheme / ao_scheme_builtin.c
index 0da68778eef7398164644e461d3bb3cbe93bd910..4cb8b901e7c425933470dc638576d5a4f0e9e8b8 100644 (file)
@@ -230,6 +230,31 @@ ao_scheme_do_list_copy(struct ao_scheme_cons *cons)
        return ao_scheme_cons_poly(new);
 }
 
+ao_poly
+ao_scheme_do_list_tail(struct ao_scheme_cons *cons)
+{
+       ao_poly list;
+       int32_t v;
+
+       if (!ao_scheme_check_argc(_ao_scheme_atom_list2dtail, cons, 2, 2))
+               return AO_SCHEME_NIL;
+       if (!ao_scheme_check_argt(_ao_scheme_atom_list2dtail, cons, 0, AO_SCHEME_CONS, 1))
+               return AO_SCHEME_NIL;
+       list = ao_scheme_arg(cons, 0);
+       v = ao_scheme_arg_int(_ao_scheme_atom_list2dtail, cons, 1);
+       if (ao_scheme_exception)
+               return AO_SCHEME_NIL;
+       while (v > 0) {
+               if (!list)
+                       return ao_scheme_error(AO_SCHEME_INVALID, "%v: ran off end", _ao_scheme_atom_list2dtail);
+               if (!ao_scheme_is_cons(list))
+                       return ao_scheme_error(AO_SCHEME_INVALID, "%v: invalid list", _ao_scheme_atom_list2dtail);
+               list = ao_scheme_poly_cons(list)->cdr;
+               v--;
+       }
+       return list;
+}
+
 ao_poly
 ao_scheme_do_quote(struct ao_scheme_cons *cons)
 {
@@ -762,17 +787,39 @@ ao_scheme_do_string_set(struct ao_scheme_cons *cons)
        val = ao_scheme_arg_int(_ao_scheme_atom_string2dset21, cons, 2);
        if (ao_scheme_exception)
                return AO_SCHEME_NIL;
+       if (!val)
+               goto fail;
        while (*string && ref) {
                ++string;
                --ref;
        }
        if (!*string)
-               return ao_scheme_error(AO_SCHEME_INVALID, "%v: string %v ref %v invalid",
-                                      _ao_scheme_atom_string2dset21,
-                                      ao_scheme_arg(cons, 0),
-                                      ao_scheme_arg(cons, 1));
+               goto fail;
        *string = val;
        return ao_scheme_int_poly(*string);
+fail:
+       return ao_scheme_error(AO_SCHEME_INVALID, "%v: %v[%v] = %v invalid",
+                              _ao_scheme_atom_string2dset21,
+                              ao_scheme_arg(cons, 0),
+                              ao_scheme_arg(cons, 1),
+                              ao_scheme_arg(cons, 2));
+}
+
+ao_poly
+ao_scheme_do_make_string(struct ao_scheme_cons *cons)
+{
+       int32_t len;
+       char    fill;
+
+       if (!ao_scheme_check_argc(_ao_scheme_atom_make2dstring, cons, 1, 2))
+               return AO_SCHEME_NIL;
+       len = ao_scheme_arg_int(_ao_scheme_atom_make2dstring, cons, 0);
+       if (ao_scheme_exception)
+               return AO_SCHEME_NIL;
+       fill = ao_scheme_opt_arg_int(_ao_scheme_atom_make2dstring, cons, 1, ' ');
+       if (ao_scheme_exception)
+               return AO_SCHEME_NIL;
+       return ao_scheme_string_poly(ao_scheme_make_string(len, fill));
 }
 
 ao_poly
@@ -1097,12 +1144,12 @@ ao_scheme_do_make_vector(struct ao_scheme_cons *cons)
 {
        int32_t k;
 
-       if (!ao_scheme_check_argc(_ao_scheme_atom_make2dvector, cons, 2, 2))
+       if (!ao_scheme_check_argc(_ao_scheme_atom_make2dvector, cons, 1, 2))
                return AO_SCHEME_NIL;
        k = ao_scheme_arg_int(_ao_scheme_atom_make2dvector, cons, 0);
        if (ao_scheme_exception)
                return AO_SCHEME_NIL;
-       return ao_scheme_vector_poly(ao_scheme_vector_alloc(k, ao_scheme_arg(cons, 1)));
+       return ao_scheme_vector_poly(ao_scheme_vector_alloc(k, ao_scheme_opt_arg(cons, 1, _ao_scheme_bool_false)));
 }
 
 ao_poly