rev 138 - in trunk: . modules pr src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: bcollins
Date: 2004-03-26 17:31:07 -0500 (Fri, 26 Mar 2004)
New Revision: 138

Removed:
   trunk/modules/Int/
Modified:
   trunk/modules/Makefile.in
   trunk/pr/hex.pr
   trunk/src/builtins.c
   trunk/status.txt
Log:
Move Int module to builtins.


Modified: trunk/modules/Makefile.in
===================================================================
--- trunk/modules/Makefile.in	2004-03-26 21:54:50 UTC (rev 137)
+++ trunk/modules/Makefile.in	2004-03-26 22:31:07 UTC (rev 138)
@@ -11,7 +11,7 @@
 HAVE_BOOST_REGEX = @HAVE_BOOST_REGEX@
 BOOST_REGEX_LIBS = @BOOST_REGEX_LIBS@
 
-MODULE_TARGETS = Dict/Dict.so File/File.so Float/Float.so Int/Int.so List/List.so \
+MODULE_TARGETS = Dict/Dict.so File/File.so Float/Float.so List/List.so \
 	String/String.so Tuple/Tuple.so
 
 ifneq ($(HAVE_BOOST_REGEX),)

Modified: trunk/pr/hex.pr
===================================================================
--- trunk/pr/hex.pr	2004-03-26 21:54:50 UTC (rev 137)
+++ trunk/pr/hex.pr	2004-03-26 22:31:07 UTC (rev 138)
@@ -1,4 +1,4 @@
-import Int, String
+import String
 
 def hex(n):
 	s = ""

Modified: trunk/src/builtins.c
===================================================================
--- trunk/src/builtins.c	2004-03-26 21:54:50 UTC (rev 137)
+++ trunk/src/builtins.c	2004-03-26 22:31:07 UTC (rev 138)
@@ -61,7 +61,7 @@
 // ***************************** OBJECT_OBJ *********************************
 #define OBJECT_OBJ   OBJ(OBJECT)
 
-isp ist;
+static isp ist;
 
 DEF(OBJECT_OBJ, __init__, NULL) {
 	return self;
@@ -241,13 +241,13 @@
 // ***************************** SEQUENCE *************************************
 #define SEQ_PROTO_OBJ OBJ(SEQ_PROTO)
 
-int seq_len(isp ist, obj_p seq) {
+static int seq_len(isp ist, obj_p seq) {
 	if (has_proto(ist, seq, OBJ(STRING_PROTO)))
 		return (int) strlen(strch(seq));
 	else return list_len(ist, seq);
 }
 
-obj_p seq_item(isp ist, obj_p seq, int i) {
+static obj_p seq_item(isp ist, obj_p seq, int i) {
 	char s[2];
 	if (has_proto(ist, seq, OBJ(STRING_PROTO))) {
 		s[0] = strch(seq)[i];
@@ -258,7 +258,7 @@
 }
 
 
-obj_p str_tuple_list(isp ist, obj_p self, char* ldelim, char* rdelim) {
+static obj_p str_tuple_list(isp ist, obj_p self, char* ldelim, char* rdelim) {
 	int i, len=list_len(ist, self);
 	char msg[1024], *res = pr_malloc(2); 
 	strcpy(res, ldelim);
@@ -285,7 +285,7 @@
 #define SEQ_TYPE_TUPLE		1
 #define SEQ_TYPE_LIST		2
 
-obj_p get_sequence_item(isp ist, obj_p self, obj_p slice, int seq_type) {
+static obj_p get_sequence_item(isp ist, obj_p self, obj_p slice, int seq_type) {
 	obj_p res = NULL, slice_item1, slice_item2, slice_item3;
 	int i, index1 = 0, index2 = 0, index3 = 0, slice1_empty=FALSE, slice2_empty=FALSE;
 	int exp_len, self_len, slice_len = list_len(ist, slice);
@@ -583,6 +583,7 @@
 	write_unlock(ist, self);   read_lock(ist, self); 
 	return NULL;
 }
+
 DEF(LIST_OBJ, __delitem__, FORM_RPARAM) {
 	obj_p slice_item1, slice_item2, slice_item3;
 	obj_p slice = parms[1];
@@ -784,6 +785,245 @@
 	return NULL;
 }
 
+// ***************************** INT *******************************************
+#define INT_OBJ		OBJ(INT_PROTO)
+#define	INT_DATA_SIZE		8
+#define is_Int(objid)		(has_proto(ist, objid, INT_OBJ))
+#define Int_value(objid)	(objid->data.i64)
+
+// static obj_p INTGEN_OBJ, SYM_LIMIT;	
+
+DEF(INT_OBJ, __bool__, NULL) { 
+	if (Int_value(self)) return OBJ(TRUE);
+	else				 return OBJ(FALSE);
+}
+
+DEF(INT_OBJ, __hash__, NULL) { 
+	i32_t i32 = (i32_t) Int_value(self);
+	return new_hash_obj((((i32*i32)<<(i32 & 16))+i32)*17);
+}
+
+DEF(INT_OBJ, __abs__, NULL){
+	i64_t num = Int_value(self);
+	return new_int_obj(num < 0 ? -num : num);
+}
+
+DEF(INT_OBJ, __neg__, NULL){
+	return new_int_obj( - Int_value(self) );
+}
+
+DEF(INT_OBJ, __pos__, NULL){
+	return self;
+}
+
+DEF(INT_OBJ, __add__, FORM_RPARAM){
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__ADD__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "Integer cannot be added to this object");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) + Int_value(other));
+}
+
+DEF(INT_OBJ, __div__, FORM_RPARAM){
+	obj_p res;
+	obj_p float_self, float_other, other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__RDIV__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "Integer cannot be divided by this object");
+		return OBJ(NONE);
+	}
+	float_self  = call_func1(ist, OBJ(FLOAT_PROTO), SYM(__COERCE__), self);  if_exc_return NULL;
+	float_other = call_func1(ist, OBJ(FLOAT_PROTO), SYM(__COERCE__), other); if_exc_return NULL;
+	res = call_func1(ist, float_self, SYM(__DIV__), float_other);
+	del_unlock(ist, float_self);  del_unlock(ist, float_other);  
+	return res;
+}
+
+DEF(INT_OBJ, __floordiv__, FORM_RPARAM){
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__RFLOORDIV__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "Integer cannot be floor divided by this object");
+		return OBJ(NONE);
+	}
+	if (Int_value(other) == 0) {
+		raise_exception(ist, OBJ(DIVIDEZERO_EXC), NULL);
+		return NULL;
+	}
+	return new_int_obj(Int_value(self) / Int_value(other));
+}
+
+DEF(INT_OBJ, __mod__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__RMOD__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "Integer cannot be modded by this object");
+		return OBJ(NONE);
+	}
+	if (Int_value(other) == 0) {
+		raise_exception(ist, OBJ(DIVIDEZERO_EXC), "modulo by zero");
+		return NULL;
+	}
+	return new_int_obj(Int_value(self) % Int_value(other));
+}
+
+DEF(INT_OBJ, __mul__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__MUL__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "Integer cannot be multiplied by this object");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) * Int_value(other));
+}
+
+DEF(INT_OBJ, __sub__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__RSUB__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "This object cannot be subtracted from an Integer");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) - Int_value(other));
+}
+
+DEF(INT_OBJ, __pow__, FORM_RPARAM){
+	obj_p res;
+	obj_p float_self, float_other, other = parms[1];
+	float_self  = call_func1(ist, OBJ(FLOAT_PROTO), SYM(__COERCE__), self);  if_exc_return NULL;
+	float_other = call_func1(ist, OBJ(FLOAT_PROTO), SYM(__COERCE__), other); if_exc_return NULL;
+	res = call_func1(ist, float_self, SYM(__POW__), float_other);
+	del_unlock(ist, float_self); del_unlock(ist, float_other); 
+	return res;
+}
+
+DEF(INT_OBJ, cmp, FORM_RPARAM){
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__RCMP__), self);
+		raise_exception(ist, OBJ(TYPE_EXC), "This object cannot be compared to an Integer");
+		return OBJ(NONE);
+	}
+	if (Int_value(self) == Int_value(other))
+		return new_int_obj(0);
+	else if (Int_value(self) > Int_value(other))
+		return new_int_obj(1);
+	else
+		return new_int_obj(-1);
+}
+
+DEF(INT_OBJ, __eq__, FORM_RPARAM){
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		if (covers(other, self))
+			return call_func1(ist, other, SYM(__EQ__), self);
+		return OBJ(FALSE);
+	}
+	if (Int_value(self) == Int_value(other)) return OBJ(TRUE);
+	else                                     return OBJ(FALSE);
+}
+DEF(INT_OBJ, __invert__, NULL){
+	return new_int_obj( ~ Int_value(self) );
+}
+
+DEF(INT_OBJ, __xor__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		raise_exception(ist, OBJ(TYPE_EXC), "object cannot be xor'd with an Integer");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) ^ Int_value(other));
+}
+
+DEF(INT_OBJ, __and__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		raise_exception(ist, OBJ(TYPE_EXC), "object cannot be and'd with an Integer");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) & Int_value(other));
+}
+
+DEF(INT_OBJ, __or__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		raise_exception(ist, OBJ(TYPE_EXC), "object cannot be or'd with an Integer");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) | Int_value(other));
+}
+
+DEF(INT_OBJ, __rshift__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		raise_exception(ist, OBJ(TYPE_EXC), "object cannot be shifted with an Integer");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) >> Int_value(other));
+}
+
+DEF(INT_OBJ, __lshift__, FORM_RPARAM) {
+	obj_p other = parms[1];
+	if (!is_Int(other)) {
+		raise_exception(ist, OBJ(TYPE_EXC), "object cannot be shifted with an Integer");
+		return OBJ(NONE);
+	}
+	return new_int_obj(Int_value(self) << Int_value(other));
+}
+
+DEF(INT_OBJ, __str__, NULL){
+	char str[24];
+#ifdef WIN32
+	sprintf(str, "%I64d", Int_value(self));
+#else
+	sprintf(str, "%lld", (long long)Int_value(self));
+#endif
+	return new_string_obj(str);
+}
+
+#if 0
+DEF (INT_OBJ, __gen__, NULL) {
+	obj_p gen_obj = new_int_obj(0);
+	gen_obj->attr_proto.proto = INTGEN_OBJ;
+	set_attr(ist, gen_obj, SYM_LIMIT, self);
+	return gen_obj;
+}
+
+DEF (INTGEN_OBJ, next, NULL) {
+	obj_p limit, res;
+	if ( !(limit=get_attr(ist, self, SYM_LIMIT)) ||
+		  Int_value(self) == Int_value(limit) ) {
+		if (limit) {
+			read_unlock(ist, self);
+			del_attr(ist, self, SYM_LIMIT);
+			read_lock(ist, self);
+		}
+		raise_exception(ist, OBJ(END_OF_GEN_EXC), NULL);
+		return NULL;
+	}
+	res = new_int_obj(Int_value(self));
+	read_unlock(ist, self); write_lock(ist, self);
+	Int_value(self)++;
+	write_unlock(ist, self); read_lock(ist, self); 
+	return res;
+}
+#endif
+
+DEF(INT_OBJ, chr, NULL){
+	char s[1];
+	s[0] = (char) Int_value(self);
+	return new_string_n_obj(s, 1);
+}
+
 // ***************************** FUNC ******************************************
 #define FUNC_OBJ OBJ(FUNC_PROTO)
 
@@ -794,6 +1034,7 @@
 // ***************************** INIT_BUILTINS ********************************
 void init_builtins() {
 	ist  = get_ist();
+
 	OBJECT_OBJ__init__init();
 	OBJECT_OBJ__getitem__init();
 	OBJECT_OBJ__setitem__init();
@@ -820,35 +1061,68 @@
 	OBJECT_OBJ__ge__init();
 	OBJECT_OBJ__in__init();
 	OBJECT_OBJ__notin__init();
+
 	FALSE_OBJ__str__init();
 	FALSE_OBJ__bool__init();
 	FALSE_OBJcmpinit();
+
 	TRUE_OBJ__str__init();
 	TRUE_OBJ__bool__init();
 	TRUE_OBJcmpinit();
+
 	NONE_OBJ__str__init();
 	NONE_OBJ__bool__init();
+
 	EXCEPTION_OBJ__init__init();
+	
 	GEN_OBJ__gen__init();
+
 	STRING_OBJ__str__init();
 	STRING_OBJ__hash__init();
 	STRING_OBJ__objlist__init();
 	STRING_OBJ__getitem__init();
 	STRING_OBJ__setitem__init();
 	STRING_OBJ__delitem__init();
+
 	TUPLE_OBJ__str__init();
 	TUPLE_OBJ__objlist__init();
 	TUPLE_OBJ__getitem__init();
 	TUPLE_OBJ__setitem__init();
 	TUPLE_OBJ__delitem__init();
+
 	LIST_OBJ__str__init();
 	LIST_OBJ__getitem__init();
 	LIST_OBJ__setitem__init();
 	LIST_OBJ__delitem__init();
+
 	DICT_OBJ__str__init();
 	DICT_OBJ__getitem__init();
 	DICT_OBJ__setitem__init();
 	DICT_OBJ__delitem__init();
 	DICT_OBJ__objlist__init();
+
 	FUNC_OBJ__objlist__init();
+
+	INT_OBJ__str__init();
+	INT_OBJ__bool__init();
+	INT_OBJ__hash__init();
+	INT_OBJcmpinit();
+	INT_OBJ__eq__init();
+	INT_OBJ__abs__init();
+	INT_OBJ__neg__init();
+	INT_OBJ__pos__init();
+	INT_OBJ__add__init();
+	INT_OBJ__sub__init();
+	INT_OBJ__mul__init();
+	INT_OBJ__div__init();
+	INT_OBJ__floordiv__init();
+	INT_OBJ__mod__init();
+	INT_OBJ__pow__init();
+	INT_OBJ__invert__init();
+	INT_OBJ__xor__init();
+	INT_OBJ__and__init();
+	INT_OBJ__or__init();
+	INT_OBJ__lshift__init();
+	INT_OBJ__rshift__init();
+	INT_OBJchrinit();
 }

Modified: trunk/status.txt
===================================================================
--- trunk/status.txt	2004-03-26 21:54:50 UTC (rev 137)
+++ trunk/status.txt	2004-03-26 22:31:07 UTC (rev 138)
@@ -40,8 +40,6 @@
 
 --- convert all to apr_snprintf()
 
---- move Int to a built-in
-	
 ------------------------------ bugs -------------------------------------------
 parser
 	bison warnings
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.