rev 284 - in trunk: . include/prothon pr src
SVN User <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-04-05 01:22:49 -0400 (Mon, 05 Apr 2004)
New Revision: 284
Added:
trunk/pr/defs.pr
Modified:
trunk/STATUS.txt
trunk/include/prothon/prothon.h
trunk/src/builtins-core.c
trunk/src/builtins-dict.c
trunk/src/builtins-string.c
trunk/src/builtins-tuple.c
trunk/src/dict.h
trunk/src/interp.c
trunk/src/object.c
trunk/src/object.h
trunk/src/src.vcproj
trunk/src/symbol.c
Log:
added clone() method for all built-in protos,
def now uses clone instead of singleton,
(fixes bug shown in new defs.pr file)
Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/STATUS.txt 2004-04-05 05:22:49 UTC (rev 284)
@@ -1,4 +1,43 @@
+----------------------- TO-DO (highest priority first) ------------------------
+
+--- add list comprehension
+
+--- switch mro method to C3
+
+--- catch exceptions during module loading
+
+--- credit Ruby
+
+--- help() in interactive console()
+
+--- -d -m -l cmd-line options missing
+--- memory bounds -> mem-mgr priorities
+--- -i interactive console needs globals and locals from main1 module
+
+--- flesh out dlls, list.sort!(), dict stuff, string %
+
+--- plug memory leaks -- add del_unlocks
+
+--- a < b < c
+
+--- add thread object so prothon code can create threads
+
+--- check for OverflowError in math routines
+
+--- reduce array of act params passed to function to only odd entries
+
+--- improve torture with list and dict tortures, random walk against new end
+
+--- test += on modifiable type, i.e. __iadd__ type calls
+
+--- Traceback objects (stack trace of an exception), exceptions should show function names
+
+--- Does simple 1+2 expr evaluation create objects? If so, need shortcuts to speed it up
+
+--- thread monitor function
+
+
---------------- Ideas to consider for inclusion ------------------------------
Python add-on C module adapter for Prothon
@@ -60,48 +99,7 @@
--- make print a function ?
------------------------ TO-DO (highest priority first) ------------------------
---- add list comprehension
-
---- add Object.clone() method and clone methods for list, string etc.
---- switch def to use clone instead of singleton
-
---- switch mro method to C3
-
---- catch exceptions during module loading
-
---- credit Ruby
-
---- help() in interactive console()
-
---- -d -m -l cmd-line options missing
---- memory bounds -> mem-mgr priorities
---- -i interactive console needs globals and locals from main1 module
-
---- flesh out dlls, list.sort!(), dict stuff, string %
-
---- plug memory leaks -- add del_unlocks
-
---- a < b < c
-
---- add thread object so prothon code can create threads
-
---- check for OverflowError in math routines
-
---- reduce array of act params passed to function to only odd entries
-
---- improve torture with list and dict tortures, random walk against new end
-
---- test += on modifiable type, i.e. __iadd__ type calls
-
---- Traceback objects (stack trace of an exception), exceptions should show function names
-
---- Does simple 1+2 expr evaluation create objects? If so, need shortcuts to speed it up
-
---- thread monitor function
-
-
------------------------------ bugs -------------------------------------------
parser
bison warnings
Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/include/prothon/prothon.h 2004-04-05 05:22:49 UTC (rev 284)
@@ -359,6 +359,7 @@
LEN,
MAX,
MIN,
+ CLONE,
SYM_ENUM_COUNT
} sym_enum_t;
Added: trunk/pr/defs.pr
===================================================================
--- trunk/pr/defs.pr 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/pr/defs.pr 2004-04-05 05:22:49 UTC (rev 284)
@@ -0,0 +1,14 @@
+
+print "\nThis should print 1 2 3 1 2 3\n"
+
+def outer():
+ c = 0
+ def f():
+ &c += 1
+ return &c
+ return f
+
+c1 = outer()
+c2 = outer()
+print c1(),c1(),c1(),c2(),c2(),c2()
+
Modified: trunk/src/builtins-core.c
===================================================================
--- trunk/src/builtins-core.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/builtins-core.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -97,6 +97,14 @@
return OBJ(NONE);
}
+DEF(Object, clone, NULL) {
+ if (self->data_type == OBJ_TYPE_DATAPTR) {
+ raise_exception(ist, OBJ(TYPE_EXC), "clone() not supported on this object");
+ return NULL;
+ }
+ return clone_object(self);
+}
+
DEF(Object, has_proto_QUES, FORM_RPARAM) {
if (has_proto_QUES(ist, self, parms[1]))
return OBJ(PR_TRUE);
@@ -539,6 +547,15 @@
MODULE_SET_DOC(Func, "function object prototype");
}
+DEF(Func, clone, NULL) {
+ obj_p clone = clone_object(self);
+ code* p = self->data.ptr;
+ size_t len = sizeof(code) + p->len * sizeof(code_t);
+ clone->data.ptr = pr_malloc(len);
+ memcpy(clone->data.ptr, p, len);
+ return clone;
+}
+
DEF(Func, __objlist__, FORM_RPARAM) {
return parms[1];
}
@@ -548,6 +565,7 @@
{
MODULE_SUB_INIT(Object);
MODULE_ADD_SYM(Object, __init__);
+ MODULE_ADD_SYM(Object, clone);
MODULE_ADD_SYM(Object, has_proto_QUES);
MODULE_ADD_SYM(Object, set_proto);
MODULE_ADD_SYM(Object, add_proto);
@@ -598,5 +616,6 @@
MODULE_ADD_SYM(Gen, __gen__);
MODULE_SUB_INIT(Func);
+ MODULE_ADD_SYM(Func, clone);
MODULE_ADD_SYM(Func, __objlist__);
}
Modified: trunk/src/builtins-dict.c
===================================================================
--- trunk/src/builtins-dict.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/builtins-dict.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -145,6 +145,15 @@
return res_obj;
}
+DEF(Dict, clone, NULL) {
+ obj_p clone = clone_object(self);
+ size_t len = dict_size_bytes(self->data.ptr);
+ clone->data.ptr = pr_malloc(len);
+ memcpy(clone->data.ptr, self->data.ptr, len);
+ return clone;
+}
+
+
DEF(Dict, __objlist__, FORM_RPARAM) {
size_t i, size, len;
dict_p dict, dp;
@@ -259,6 +268,7 @@
MODULE_SUB_INIT(Dict);
MODULE_ADD_SYM(Dict, __init__);
MODULE_ADD_SYM(Dict, __str__);
+ MODULE_ADD_SYM(Dict, clone);
MODULE_ADD_SYM(Dict, __objlist__);
MODULE_ADD_SYM(Dict, __getitem__);
MODULE_ADD_SYM(Dict, __setitem__);
Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/builtins-string.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -92,6 +92,17 @@
return self;
}
+DEF(String, clone, NULL) {
+ size_t len;
+ obj_p clone = clone_object(self);
+ if (self->data_type == OBJ_TYPE_DATAPTR) {
+ len = sizeof(pr_str_t) + pr_strlen(self) + 1;
+ clone->data.ptr = pr_malloc(len);
+ memcpy(clone->data.ptr, self->data.ptr, len);
+ }
+ return clone;
+}
+
DEF(String, __hash__, NULL) {
i32_t res = 0x9a7c5e3;
char *p;
@@ -301,6 +312,7 @@
MODULE_ADD_SYM(String, __init__);
MODULE_ADD_SYM(String, __str__);
MODULE_ADD_SYM(String, __hash__);
+ MODULE_ADD_SYM(String, clone);
MODULE_ADD_SYM(String, __objlist__);
MODULE_ADD_SYM(String, __getitem__);
MODULE_ADD_SYM(String, __setitem__);
Modified: trunk/src/builtins-tuple.c
===================================================================
--- trunk/src/builtins-tuple.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/builtins-tuple.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -278,6 +278,10 @@
return NULL;
}
+DEF(Tuple, clone, NULL) {
+ return clone_list_obj(ist, self);
+}
+
DEF(Tuple, __gen__, NULL) {
obj_p list_obj, gen_obj = new_object(Tgen_OBJ);
gen_obj->data_type = OBJ_TYPE_DATAPTR;
@@ -312,6 +316,7 @@
{
MODULE_SUB_INIT(Tuple);
MODULE_ADD_SYM(Tuple, __init__);
+ MODULE_ADD_SYM(Tuple, clone);
MODULE_ADD_SYM(Tuple, __str__);
MODULE_ADD_SYM(Tuple, __getitem__);
MODULE_ADD_SYM(Tuple, __setitem__);
Modified: trunk/src/dict.h
===================================================================
--- trunk/src/dict.h 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/dict.h 2004-04-05 05:22:49 UTC (rev 284)
@@ -73,13 +73,16 @@
typedef dict_t* dict_p;
+#define dict_sizeof(n) (sizeof(dict_hdr_t) + (n) * sizeof(dict_entry_t))
+#define dict_size_bytes(dict) (dict_sizeof(dictsize(dict)))
+
#define DICT_HDR_INDEX 0
#define DICT_OVERHEAD 1
#define dict_d(dict_obj) ((dict_p)obj_data_p(dict_obj))
-#define dictsize(dict) (dict[DICT_HDR_INDEX].hdr.size)
-#define dictlen(dict) (dict[DICT_HDR_INDEX].hdr.len)
-#define dictptr(dict, i) (dict+DICT_OVERHEAD+((i)%dictsize(dict)))
-#define dicthash(dict,i) (dictptr(dict,i)->entry.hash)
-#define dictkey(dict, i) (dictptr(dict,i)->entry.key)
-#define dictitem(dict,i) (dictptr(dict,i)->entry.value)
+#define dictsize(dict) (((dict_p)(dict))[DICT_HDR_INDEX].hdr.size)
+#define dictlen(dict) (((dict_p)(dict))[DICT_HDR_INDEX].hdr.len)
+#define dictptr(dict, i) (((dict_p)(dict))+DICT_OVERHEAD+((i)%dictsize(dict)))
+#define dicthash(dict,i) (dictptr(((dict_p)(dict)),i)->entry.hash)
+#define dictkey(dict, i) (dictptr(((dict_p)(dict)),i)->entry.key)
+#define dictitem(dict,i) (dictptr(((dict_p)(dict)),i)->entry.value)
Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/interp.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -723,7 +723,7 @@
case OP_GEN:
case OP_DEF: {
int fparams_len = fr_data_int(1)*2;
- obj_p func = fr_data(2);
+ obj_p func = call_func0(ist, fr_data(2), SYM(CLONE));
obj_p fparams_obj = new_list_obj(fparams_len);
fr_sp -= fparams_len+2;
assert(sizeof(list_item_t) == sizeof(obj_p));
Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/object.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -230,6 +230,30 @@
return obj;
}
+
+//********************************* clone_object ******************************
+// clones everything but data pointed to by data.ptr
+obj_p clone_object(obj_p obj) {
+ obj_p clone;
+ attr_p new_attrp, attrp;
+ clone = new_object(NULL);
+ memcpy(clone, obj, sizeof(*obj)-sizeof(obj->next_obj));
+ clone->archived = FALSE;
+ clone->del_locked = TRUE;
+ clone->long_wrlock = FALSE;
+ clone->rdlock_cnt = 0;
+ clone->wrlock_req_cnt = 0;
+ clone->wrlock = 0;
+ if (obj->has_attrs) {
+ size_t size;
+ attrp = obj->attr_proto.attrs;
+ size = attr_tsize(attrp)*sizeof(attr_t);
+ clone->attr_proto.attrs = new_attrp = pr_malloc(size);
+ memcpy(new_attrp, attrp, size);
+ }
+ return clone;
+}
+
//********************************* free_object *******************************
void free_object(obj_p obj){
if (obj->has_attrs)
@@ -918,12 +942,17 @@
//********************************* clone_list_obj ****************************
obj_p clone_list_obj(isp ist, obj_p list_obj) {
obj_p res;
+ size_t llen, size;
+ list_p list_ptr;
read_lock(ist, list_obj);
- res = new_list_obj(listlen(list_obj));
- memcpy( ((list_p)(res->data.ptr))->item,
- ((list_p)(list_obj->data.ptr))->item,
- listlen(list_obj) * sizeof(list_item_t) );
- listlen(res) = listlen(list_obj);
+ llen = listlen(list_obj);
+ size = list_sizeof(llen);
+ res = clone_object(list_obj);
+ res->data.ptr = list_ptr = pr_malloc(size);
+ lstpsize(list_ptr) = llen;
+ lstplen(list_ptr) = llen;
+ memcpy( list_ptr->item, ((list_p)(list_obj->data.ptr))->item,
+ size-sizeof(list_hdr_t) );
read_unlock(ist, list_obj);
return res;
}
Modified: trunk/src/object.h
===================================================================
--- trunk/src/object.h 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/object.h 2004-04-05 05:22:49 UTC (rev 284)
@@ -128,6 +128,7 @@
void object_store_init(isp ist, int nop2);
obj_p new_object(obj_p proto);
+obj_p clone_object(obj_p obj);
void free_object(obj_p obj);
void set_item(isp ist, obj_p value, obj_p ref_self, obj_p ref_key);
Modified: trunk/src/src.vcproj
===================================================================
--- trunk/src/src.vcproj 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/src.vcproj 2004-04-05 05:22:49 UTC (rev 284)
@@ -244,6 +244,9 @@
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe">
<File
+ RelativePath="..\pr\defs.pr">
+ </File>
+ <File
RelativePath="..\pr\file_test.pr">
</File>
<File
Modified: trunk/src/symbol.c
===================================================================
--- trunk/src/symbol.c 2004-04-05 02:26:15 UTC (rev 283)
+++ trunk/src/symbol.c 2004-04-05 05:22:49 UTC (rev 284)
@@ -62,21 +62,21 @@
// this must match exact order in enum sym_enum_t in bytecodes.h
sym_id_entry_t sym_id_table[SYM_ENUM_COUNT] = {
{ "__string__", 0 }, // __string__ must be first (==0)
- { "__not__QUES", 0 },
+ { "__not__QUES", 0 },
{ "__pos__", 0 },
{ "__neg__", 0 },
{ "__invert__", 0 },
- { "__in__QUES", 0 },
- { "__notin__QUES", 0 },
- { "__is__QUES", 0 },
- { "__isnot__QUES", 0 },
+ { "__in__QUES", 0 },
+ { "__notin__QUES", 0 },
+ { "__is__QUES", 0 },
+ { "__isnot__QUES", 0 },
{ "cmp", 0 },
- { "__lt__QUES", 0 },
- { "__le__QUES", 0 },
- { "__gt__QUES", 0 },
- { "__ge__QUES", 0 },
- { "__ne__QUES", 0 },
- { "__eq__QUES", 0 },
+ { "__lt__QUES", 0 },
+ { "__le__QUES", 0 },
+ { "__gt__QUES", 0 },
+ { "__ge__QUES", 0 },
+ { "__ne__QUES", 0 },
+ { "__eq__QUES", 0 },
{ "__or__", 0 },
{ "__xor__", 0 },
{ "__and__", 0 },
@@ -101,8 +101,8 @@
{ "__ifloordiv__", 0 },
{ "__imod__", 0 },
{ "__ipow__", 0 },
- { "__rin__QUES", 0 },
- { "__rnotin__QUES", 0 },
+ { "__rin__QUES", 0 },
+ { "__rnotin__QUES", 0 },
{ "__rlt__", 0 },
{ "__rle__", 0 },
{ "__rgt__", 0 },
@@ -116,7 +116,7 @@
{ "__rpow__", 0 },
{ "__rcmp__", 0 },
{ "__init__", 0 },
- { "__bool__QUES", 0 },
+ { "__bool__QUES", 0 },
{ "__gen__", 0 },
{ "__doc__", 0 },
{ "__hash__", 0 },
@@ -130,7 +130,7 @@
{ "__poslist__", 0 },
{ "__new__", 0 },
{ "__copy__", 0 },
- { "__covers__QUES", 0 },
+ { "__covers__QUES", 0 },
{ "__coerce__", 0 },
{ "__str__", 0 }, // cf
{ "next", 0 },
@@ -139,9 +139,10 @@
{ "__delitem__", 0 },
{ "__objlist__", 0 },
{ "__del__", 0 },
- { "len", 0 },
- { "max", 0 },
- { "min", 0 }
+ { "len", 0 },
+ { "max", 0 },
+ { "min", 0 },
+ { "clone", 0 }
};
typedef struct trie_node_s* trie_p;