rev 232 - trunk/src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: bcollins
Date: 2004-03-31 10:39:21 -0500 (Wed, 31 Mar 2004)
New Revision: 232

Modified:
   trunk/src/builtins.c
Log:
Add generator for Dict object. Like python, it iterates a list of the
keys.


Modified: trunk/src/builtins.c
===================================================================
--- trunk/src/builtins.c	2004-03-31 15:12:34 UTC (rev 231)
+++ trunk/src/builtins.c	2004-03-31 15:39:21 UTC (rev 232)
@@ -74,6 +74,7 @@
 MODULE_DECLARE(Tuple);
 MODULE_DECLARE(List);
 MODULE_DECLARE(Dict);
+MODULE_DECLARE(DictGen);
 MODULE_DECLARE(Seq);
 MODULE_DECLARE(Func);
 MODULE_DECLARE(Int);
@@ -1278,8 +1279,38 @@
 	return NULL;
 }
 
+DEF(Dict, __gen__, NULL) {
+	obj_p list_obj, gen_obj = new_object(DictGen_OBJ);
+
+	gen_obj->data_type = OBJ_TYPE_DATAPTR;
+	gen_obj->data.ptr = list_obj = clone_list_obj(ist, dict_keys(ist, self));
+
+	listlen(list_obj) = 0;
+	return gen_obj;
+}
+
 MODULE_END(Dict);
 
+MODULE_START(DictGen)
+{
+	DictGen_OBJ = new_object(NULL);
+}
+
+DEF(DictGen, next, NULL) {
+	obj_p res, list_obj = self->data.ptr;
+	size_t lsiz = listsize(list_obj);
+	size_t llen = listlen(list_obj);
+	if (llen == lsiz) {
+		raise_exception(ist, OBJ(STOP_ITERATION_EXC), NULL);
+		return NULL;
+	}
+	res = listitem(list_obj, llen);
+	listlen(list_obj)++;
+	return res;
+}
+
+MODULE_END(DictGen);
+
 // ***************************** FUNC ******************************************
 
 MODULE_START(Func)
@@ -1345,6 +1376,7 @@
 	MODULE_SUB_INIT(Gen);
 	MODULE_SUB_INIT(String);
 	MODULE_SUB_INIT(Dict);
+	MODULE_SUB_INIT(DictGen);
 	MODULE_SUB_INIT(Func);
 
 	SYM_LIMIT = sym(ist, "limit");
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.