rev 429 - in trunk: include/prothon pr src

SVN User <[email protected]> Thu, 29 Apr 2004 01:15:45 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-04-29 01:15:42 -0400 (Thu, 29 Apr 2004)
New Revision: 429

Added:
   trunk/pr/init.pr
Modified:
   trunk/include/prothon/prothon.h
   trunk/src/builtins-dict.c
   trunk/src/builtins-float.c
   trunk/src/builtins-int.c
   trunk/src/builtins-list.c
   trunk/src/builtins-string.c
   trunk/src/builtins-tuple.c
   trunk/src/interp.c
   trunk/src/src.vcproj
Log:
added initialization value capabilities to all built-in
prototype __init__ methods, added init.pr

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/include/prothon/prothon.h	2004-04-29 05:15:42 UTC (rev 429)
@@ -438,7 +438,7 @@
 	frame_p					frame;
 	obj_p					exception_obj;
 } interp_state_t;
-
+ 
 // NEW_OBJECT: Create a new empty object with only proto pointer
 // Creates a new empty object with a single proto obj.  If proto is
 // NULL then the proto defaults to OBJ(OBJECT).  The new object
@@ -847,58 +847,68 @@
 
 // xxx_PARAM: Convenience macro for validation and assignment of params
 // index is 1..n where 1 is first parameter and n is last
+// when index is 0 .. -n, parms[1] is a list and -index is list index
+#define ITEM (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index))
+
 #define INT_32_PARAM(index, var)	/* int var; */							\
-	if (!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(INT_PROTO))) {		\
+{	if (!has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)), OBJ(INT_PROTO))) {						\
 		raise_exception(ist, OBJ(TYPE_EXC),									\
 				"expected a integer in parameter " #index);					\
 		return NULL;														\
 	}																		\
-	(var) = (int) parms[((index)-1)*2+1]->data.i64
+	(var) = (int) (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index))->data.i64; }
+
 #define INT_64_PARAM(index, var)	/* i64_t var; */						\
-	if (!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(INT_PROTO))) {		\
+{	if (!has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)), OBJ(INT_PROTO))) {						\
 		raise_exception(ist, OBJ(TYPE_EXC),									\
 				"expected a integer in parameter " #index);					\
+		return (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index));														\
+	}																		\
+	(var) = (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index))->data.i64; }
+
+#define STRING_PARAM(index, var)   \
+{	if (!has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)), OBJ(STRING_PROTO))) {					\
+		raise_exception(ist, OBJ(TYPE_EXC),									\
+				"expected a string in parameter " #index);					\
 		return NULL;														\
-	}																		\
-	(var) = parms[((index)-1)*2+1]->data.i64
+	} (var) = pr_strptr((index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index))); }
+	
+
 #define FLOAT__PARAM(index, var)	/* double var; */						\
-	if (!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(FLOAT_PROTO))) {	\
+{	if (!has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)) , OBJ(FLOAT_PROTO))) {					\
 		raise_exception(ist, OBJ(TYPE_EXC),									\
 				"expected a float in parameter " #index);					\
 		return NULL;														\
 	}																		\
-	(var) = parms[((index)-1)*2+1]->data.f64
-#define STRING_PARAM(index, var)	/* char* var; */						\
-	if (!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(STRING_PROTO))) {	\
-		raise_exception(ist, OBJ(TYPE_EXC),									\
-				"expected a string in parameter " #index);					\
-		return NULL;														\
-	}																		\
-	(var) = pr_strptr(parms[((index)-1)*2+1])
+	(var) = (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index))->data.f64;	}
+
 #define STRBIN_PARAM(index, var)	/* apr_datum_t var; */					\
-	if (!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(STRING_PROTO))) {	\
+{	if (!has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)), OBJ(STRING_PROTO))) {	\
 		raise_exception(ist, OBJ(TYPE_EXC),									\
 				"expected a string in parameter " #index);					\
 		return NULL;														\
 	}																		\
-	(var).dptr  = pr_strptr(parms[((index)-1)*2+1]);						\
-	(var).dsize = pr_strlen(parms[((index)-1)*2+1]);
+	(var).dptr  = pr_strptr((index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)));	\
+	(var).dsize = pr_strlen((index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index))); }
+
 #define SEQ_NS_PARAM(index, var)	/* obj_p var; */						\
-	if ( has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(STRING_PROTO)) ||	\
-		!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(SEQ_PROTO))) {		\
+{	if ( has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)), OBJ(STRING_PROTO)) ||					\
+		!has_proto_QUES(ist, (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)), OBJ(SEQ_PROTO))) {						\
 		raise_exception(ist, OBJ(TYPE_EXC),									\
 				"expected a Tuple or List in parameter " #index);			\
 		return NULL;														\
 	}																		\
-	(var) = parms[((index)-1)*2+1]
+	(var) = (index > 0 ? parms[((index)-1)*2+1] : list_item(ist, parms[1], -index)); }
+
+
 #define SEQ_OR_PARAM(index, var)	/* obj_p var; */						\
-	if ( has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(STRING_PROTO)) ||	\
-		!has_proto_QUES(ist, parms[((index)-1)*2+1], OBJ(SEQ_PROTO))) {		\
+	if ( has_proto_QUES(ist, ITEM, OBJ(STRING_PROTO)) ||					\
+		!has_proto_QUES(ist, ITEM, OBJ(SEQ_PROTO))) {						\
 		list = NEW_LIST(1);													\
-		list_append(ist, list, parms[((index)-1)*2+1]);						\
+		list_append(ist, list, ITEM);										\
 		(var) = list;														\
 	} else																	\
-		(var) = parms[((index)-1)*2+1]
+		(var) = ITEM
 
 // FORM_RPARAM: convenience macro for single formal right parameter
 // Defines a formal label/default-value list that matches  func(rparam).

Added: trunk/pr/init.pr
===================================================================
--- trunk/pr/init.pr	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/pr/init.pr	2004-04-29 05:15:42 UTC (rev 429)
@@ -0,0 +1,21 @@
+#!/usr/bin/env prothon
+
+# init.pr
+
+print
+print """This should print: 
+0 1234 0 -1
+0 12.34 .333333
+ abc 123
+() (1, 2, 3) (4, 5, 6)
+[] [1, 2, 3] [4, 5, 6]
+{1:2, 3:4} {a:1, b:2, c:3}
+
+"""
+
+print Int(), Int (1234), Int(1//3), Int(-01)
+print Float(), Float(12.34), Float(1/3) #   Float(-0.1)  ERR XXX
+print String(), String("abc"), String(123), String(String)
+print Tuple(), Tuple(*(1,2,3)), Tuple(4,5,6)
+print List(), List(*[1,2,3]), List(4,5,6)
+print Dict(**{1:2, 3:4}), Dict(a=1, b=2, c=3)
Modified: trunk/src/builtins-dict.c
===================================================================
--- trunk/src/builtins-dict.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/builtins-dict.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -273,13 +273,20 @@
 		strcat(res, "'");	   \
 } while(0)
 
-DEF(Dict, __init__, NULL) {
+DEF(Dict, __init__, FORM_STAR2_PARAM) {
 	dict_p dict;
+	obj_p list;
+	int i, llen, size = max(DEFAULT_INITIAL_DICT_SIZE, (int) dict_len(ist, parms[1])*3);
 
 	self->data_type = DATA_TYPE_DATAPTR;
-    dict = obj_malloc(self, (DICT_OVERHEAD+DEFAULT_INITIAL_DICT_SIZE)*sizeof(dict_t));
-    memset(dict, 0, (DICT_OVERHEAD+DEFAULT_INITIAL_DICT_SIZE)*sizeof(dict_t));
-    dictsize(dict) = DEFAULT_INITIAL_DICT_SIZE;
+    dict = obj_malloc(self, dict_sizeof(size));
+    memset(dict, 0, dict_sizeof(size));
+    dictsize(dict) = size;
+	list = dict_keys_values(ist, parms[1], 2 /*list*/);
+	llen = (int) list_len(ist, list);
+	for(i=0; i < llen; i += 2)
+		dict_add(ist, self, list_item(ist, list, i), list_item(ist, list, i+1));
+	del_unlock(list);
 	return OBJ(NONE);
 }
 

Modified: trunk/src/builtins-float.c
===================================================================
--- trunk/src/builtins-float.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/builtins-float.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -108,10 +108,13 @@
 	//Imag_OBJ->data.f64     = 0.0;
 }
 
-DEF(Float, __init__, NULL) { 
+DEF(Float, __init__, FORM_STAR_PARAM) { 
+	double f = 0.0;
+	if (list_len(ist, parms[1]))
+		FLOAT__PARAM(0, f);
 	self->data_type    = DATA_TYPE_IMMDATA;
 	self->imm_data_len = IMMEDIATE_DATA_LEN;
-	self->data.f64     = 0.0;
+	self->data.f64     = f;
 	return OBJ(NONE);
 }
 

Modified: trunk/src/builtins-int.c
===================================================================
--- trunk/src/builtins-int.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/builtins-int.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -108,10 +108,13 @@
 	set_immutable(OBJ(LONG_PROTO));
 }
 
-DEF(Int, __init__, NULL) {
+DEF(Int, __init__, FORM_STAR_PARAM) {
+	i64_t i = 0;
+	if (list_len(ist, parms[1]))
+		INT_64_PARAM(0,i);
 	self->data_type    = DATA_TYPE_IMMDATA;
 	self->imm_data_len = IMMEDIATE_DATA_LEN;
-	self->data.i64     = 0;
+	self->data.i64     = i;
 	return OBJ(NONE);
 }
 

Modified: trunk/src/builtins-list.c
===================================================================
--- trunk/src/builtins-list.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/builtins-list.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -312,13 +312,12 @@
 	lstplen(lstp)  = 0;
 }
 
-DEF(List, __init__, NULL) {
-	list_p lstp;
-
-	self->data_type = DATA_TYPE_DATAPTR;
-	lstp = self->data.ptr = pr_malloc(list_sizeof(2));
-	lstpsize(lstp) = 2;
-	lstplen(lstp)  = 0;
+DEF(List, __init__, FORM_STAR_PARAM) {
+	read_unlock(ist, self);
+	read_unlock(ist, parms[1]);
+	copy_object_data(ist, self, parms[1]);
+	read_lock(ist, parms[1]);
+	read_lock(ist, self);
 	return OBJ(NONE);
 }
 

Modified: trunk/src/builtins-string.c
===================================================================
--- trunk/src/builtins-string.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/builtins-string.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -115,10 +115,18 @@
 //********************************* set_string_data ***************************
 void set_string_data(isp ist, obj_p obj, char* p, size_t len) {
 	pr_str_p obj_str;
-	obj_str = obj->data.ptr = obj_malloc(obj, sizeof(pr_str_t)+len+1);
-	obj_str->len = len;
-	memcpy(&(obj_str->str[0]), p, len);
-	obj_str->str[len] = 0;
+	if (len < 8) {
+		obj->data_type    = DATA_TYPE_IMMDATA;
+		obj->imm_data_len = (u8_t) len;
+		memcpy(obj->data.str, p, len);
+		obj->data.str[len]  = 0;
+	} else {
+		obj->data_type    = DATA_TYPE_DATAPTR;
+		obj_str = obj->data.ptr = obj_malloc(obj, sizeof(pr_str_t)+len+1);
+		obj_str->len = len;
+		memcpy(&(obj_str->str[0]), p, len);
+		obj_str->str[len] = 0;
+	}
 	obj->immutable = TRUE;
 }
 
@@ -139,10 +147,13 @@
 	String_OBJ->data.str[0]  = 0;
 }
 
-DEF(String, __init__, NULL) {
-	self->data_type    = DATA_TYPE_IMMDATA;
-	self->imm_data_len = 0;
-	self->data.str[0]  = 0;
+DEF(String, __init__, FORM_STAR_PARAM) {
+	char *s;
+	if (!list_len(ist, parms[1])) 
+		s = "";
+	else
+		s = as_str(ist, list_item(ist, parms[1], 0));
+	set_string_data(ist, self, s, strlen(s));
 	return OBJ(NONE);
 }
 

Modified: trunk/src/builtins-tuple.c
===================================================================
--- trunk/src/builtins-tuple.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/builtins-tuple.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -93,13 +93,14 @@
 	lstplen(lstp)  = 0;
 }
 
-DEF(Tuple, __init__, NULL) {
-	list_p lstp;
-
-	self->data_type = DATA_TYPE_DATAPTR;
-	lstp = self->data.ptr = pr_malloc(list_sizeof(2));
-	lstpsize(lstp) = 2;
-	lstplen(lstp)  = 0;
+DEF(Tuple, __init__, FORM_STAR_PARAM) {
+	read_unlock(ist, self);
+	read_unlock(ist, parms[1]);
+	copy_object_data(ist, self, parms[1]);
+	switch_proto(ist, self, OBJ(TUPLE_PROTO));
+	set_immutable(self);
+	read_lock(ist, parms[1]);
+	read_lock(ist, self);
 	return OBJ(NONE);
 }
 

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/interp.c	2004-04-29 05:15:42 UTC (rev 429)
@@ -299,10 +299,12 @@
 				break;
 			}
 			case (uintptr_t)PARAM_STAR_STAR: {
-				clist_p plist = obj_data_p(value);
-				for(j=0; j < clist_len(plist); j+=2)
-					if ( add_label_param(ist, fparam_proc_state, clist_item(plist, j), 
-						                      clist_item(plist, j+1)) ) goto paramerr;
+				obj_p list = dict_keys_values(ist, value, 2 /*list*/);
+				int llen = (int) list_len(ist, list);
+				for(j=0; j < llen; j+=2)
+					if ( add_label_param(ist, fparam_proc_state, list_item(ist, list, j), 
+						                      list_item(ist, list, j+1)) ) goto paramerr;
+				del_unlock(list);
 				break;
 			}
 			default:

Modified: trunk/src/src.vcproj
===================================================================
--- trunk/src/src.vcproj	2004-04-28 23:50:18 UTC (rev 428)
+++ trunk/src/src.vcproj	2004-04-29 05:15:42 UTC (rev 429)
@@ -265,6 +265,9 @@
 				RelativePath="..\pr\hex.pr">
 			</File>
 			<File
+				RelativePath="..\pr\init.pr">
+			</File>
+			<File
 				RelativePath="init.pth">
 			</File>
 			<File