[slang-users] patch: push and pop args using lists rather than an
array of structs
Doug Burke
dburke at cfa.harvard.edu
Fri Apr 28 11:00:53 EDT 2006
I've included below a patch I think is useful - it adds two new
intrinsics
that mirror __push_args/__pop_args but using a list rather than an array
of structures. If there's interest I'll submit a patch with tests and
docs
as well. Any comments on how to improve it are most welcome (including
better routine names :-), since I'm sure the list processing is not the
most efficient (e.g. would it be better to add the items to the end of
the
list and then reverse it in the push_args routine?).
The patch is against S-Lang v2.0.6.
Doug
--- sllist.c.orig 2006-04-27 17:10:20.000000000 -0400
+++ sllist.c 2006-04-27 18:03:43.000000000 -0400
@@ -760,6 +760,79 @@
return -1;
}
+void _pSLlist_pop_args (int *np)
+{
+ SLang_List_Type *list;
+ SLindex_Type i, n;
+
+ n = *np;
+
+ if (n < 0)
+ {
+ SLang_set_error (SL_INVALID_PARM);
+ return;
+ }
+
+ if (NULL == (list = allocate_list ()))
+ {
+ SLdo_pop_n (n);
+ return;
+ }
+
+ i = n;
+ while (i > 0)
+ {
+ SLang_Object_Type obj;
+
+ i--;
+
+ if (-1 == SLang_pop (&obj))
+ {
+ delete_list (list);
+ return;
+ }
+
+ /*
+ * Keep on adding to the start of the list.
+ * Probably not very efficient.
+ */
+ if (-1 == insert_element (list, &obj, 0))
+ {
+ /* SLang_free_object (obj); */
+ delete_list (list);
+ return;
+ }
+ }
+
+ (void) push_list (list);
+ return;
+
+}
+
+static void _pSLlist_push_args (SLang_List_Type *list)
+{
+ Chunk_Type *c;
+ int counter = 1;
+
+ if (NULL == list)
+ return;
+
+ c = list->first;
+ while (NULL != c)
+ {
+ SLang_Object_Type *obj = c->elements;
+ SLang_Object_Type *obj_max = obj + c->num_elements;
+
+ while (obj < obj_max)
+ {
+ (void) _pSLpush_slang_obj (obj);
+ obj++;
+ }
+
+ c = c->next;
+ }
+}
+
#define L SLANG_LIST_TYPE
#define I SLANG_INT_TYPE
static SLang_Intrin_Fun_Type Intrin_Table [] =
@@ -770,6 +843,8 @@
MAKE_INTRINSIC_0("list_append", list_append_elem, SLANG_VOID_TYPE),
MAKE_INTRINSIC_0("list_new", list_new, SLANG_VOID_TYPE),
MAKE_INTRINSIC_0("list_pop", list_pop, SLANG_VOID_TYPE),
+ MAKE_INTRINSIC_1("__pop_args_as_list", _pSLlist_pop_args,
SLANG_VOID_TYPE, I),
+ MAKE_INTRINSIC_1("__push_args_from_list", _pSLlist_push_args,
SLANG_VOID_TYPE, L),
SLANG_END_INTRIN_FUN_TABLE
};
#undef L
More information about the slang-users-l
mailing list