altos/scheme: Add builtin list-tail
[fw/altos] / src / scheme / ao_scheme_builtin.c
index 0b84a89a2b366bdb67f2cf337fa4dc2a34eac24f..1bfe6942b698dfc8ce6148e860a71d214f14842e 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)
 {