altos/scheme: Allow make-vector value param to be optional
[fw/altos] / src / scheme / ao_scheme_builtin.c
index 8438243412ac7fb4483171c56e8c066f585dd015..4cb8b901e7c425933470dc638576d5a4f0e9e8b8 100644 (file)
@@ -105,17 +105,23 @@ ao_scheme_check_argc(ao_poly name, struct ao_scheme_cons *cons, int min, int max
        return _ao_scheme_bool_true;
 }
 
-ao_poly
-ao_scheme_arg(struct ao_scheme_cons *cons, int argc)
+static ao_poly
+ao_scheme_opt_arg(struct ao_scheme_cons *cons, int argc, ao_poly def)
 {
-       if (!cons)
-               return AO_SCHEME_NIL;
-       while (argc--) {
+       for (;;) {
                if (!cons)
-                       return AO_SCHEME_NIL;
+                       return def;
+               if (argc == 0)
+                       return cons->car;
                cons = ao_scheme_cons_cdr(cons);
+               argc--;
        }
-       return cons->car;
+}
+
+ao_poly
+ao_scheme_arg(struct ao_scheme_cons *cons, int argc)
+{
+       return ao_scheme_opt_arg(cons, argc, AO_SCHEME_NIL);
 }
 
 ao_poly
@@ -140,6 +146,18 @@ ao_scheme_arg_int(ao_poly name, struct ao_scheme_cons *cons, int argc)
        return i;
 }
 
+static int32_t
+ao_scheme_opt_arg_int(ao_poly name, struct ao_scheme_cons *cons, int argc, int def)
+{
+       ao_poly         p = ao_scheme_opt_arg(cons, argc, ao_scheme_int_poly(def));
+       bool            fail = false;
+       int32_t         i = ao_scheme_poly_integer(p, &fail);
+
+       if (fail)
+               (void) ao_scheme_error(AO_SCHEME_INVALID, "%v: arg %d invalid type %v", name, argc, p);
+       return i;
+}
+
 ao_poly
 ao_scheme_do_car(struct ao_scheme_cons *cons)
 {
@@ -212,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)
 {
@@ -321,7 +364,7 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
 
                if (cons == orig_cons) {
                        ret = car;
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        if (cons->cdr == AO_SCHEME_NIL) {
                                switch (op) {
                                case builtin_minus:
@@ -349,7 +392,7 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                                        break;
                                }
                        }
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
                } else if (ao_scheme_integer_typep(rt) && ao_scheme_integer_typep(ct)) {
                        int32_t r = ao_scheme_poly_integer(ret, NULL);
                        int32_t c = ao_scheme_poly_integer(car, NULL);
@@ -393,6 +436,11 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                        case builtin_quotient:
                                if (c == 0)
                                        return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "quotient by zero");
+                               r = r / c;
+                               break;
+                       case builtin_floor_quotient:
+                               if (c == 0)
+                                       return ao_scheme_error(AO_SCHEME_DIVIDE_BY_ZERO, "floor-quotient by zero");
                                if (r % c != 0 && (c < 0) != (r < 0))
                                        r = r / c - 1;
                                else
@@ -413,9 +461,9 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                        default:
                                break;
                        }
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        ret = ao_scheme_integer_poly(r);
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
 #ifdef AO_SCHEME_FEATURE_FLOAT
                } else if (ao_scheme_number_typep(rt) && ao_scheme_number_typep(ct)) {
                        float r, c;
@@ -436,22 +484,23 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                                r /= c;
                                break;
                        case builtin_quotient:
+                       case builtin_floor_quotient:
                        case builtin_remainder:
                        case builtin_modulo:
                                return ao_scheme_error(AO_SCHEME_INVALID, "non-integer value in integer divide");
                        default:
                                break;
                        }
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        ret = ao_scheme_float_get(r);
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
 #endif
                }
                else if (rt == AO_SCHEME_STRING && ct == AO_SCHEME_STRING && op == builtin_plus) {
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        ret = ao_scheme_string_poly(ao_scheme_string_cat(ao_scheme_poly_string(ret),
                                                                         ao_scheme_poly_string(car)));
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
                        if (!ret)
                                return ret;
                }
@@ -491,6 +540,12 @@ ao_scheme_do_quotient(struct ao_scheme_cons *cons)
        return ao_scheme_math(cons, builtin_quotient);
 }
 
+ao_poly
+ao_scheme_do_floor_quotient(struct ao_scheme_cons *cons)
+{
+       return ao_scheme_math(cons, builtin_floor_quotient);
+}
+
 ao_poly
 ao_scheme_do_modulo(struct ao_scheme_cons *cons)
 {
@@ -732,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
@@ -1067,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
@@ -1108,11 +1185,21 @@ ao_scheme_do_list_to_vector(struct ao_scheme_cons *cons)
 ao_poly
 ao_scheme_do_vector_to_list(struct ao_scheme_cons *cons)
 {
-       if (!ao_scheme_check_argc(_ao_scheme_atom_vector2d3elist, cons, 1, 1))
+       int     start, end;
+
+       if (!ao_scheme_check_argc(_ao_scheme_atom_vector2d3elist, cons, 1, 3))
                return AO_SCHEME_NIL;
        if (!ao_scheme_check_argt(_ao_scheme_atom_vector2d3elist, cons, 0, AO_SCHEME_VECTOR, 0))
                return AO_SCHEME_NIL;
-       return ao_scheme_cons_poly(ao_scheme_vector_to_list(ao_scheme_poly_vector(ao_scheme_arg(cons, 0))));
+       start = ao_scheme_opt_arg_int(_ao_scheme_atom_vector2d3elist, cons, 1, ao_scheme_int_poly(0));
+       if (ao_scheme_exception)
+               return AO_SCHEME_NIL;
+       end = ao_scheme_opt_arg_int(_ao_scheme_atom_vector2d3elist, cons, 2, ao_scheme_int_poly(-1));
+       if (ao_scheme_exception)
+               return AO_SCHEME_NIL;
+       return ao_scheme_cons_poly(ao_scheme_vector_to_list(ao_scheme_poly_vector(ao_scheme_arg(cons, 0)),
+                                                           start,
+                                                           end));
 }
 
 ao_poly