rev 170 - in trunk: . pr src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-03-28 00:46:41 -0500 (Sun, 28 Mar 2004)
New Revision: 170

Added:
   trunk/pr/file_test.pr
Modified:
   trunk/
   trunk/src/builtins.c
   trunk/status.txt
Log:
added file_test.pr, fixed int.__str__


Property changes on: trunk
___________________________________________________________________
Name: svn:ignore
   - config.log
config.cache
config.status
Makefile
prothon.ncb
prothon.sln
prothon.suo
bison_prothon.bat.lnk
apr
Rules.mk
libtool
Shortcut to rel.bat.lnk

   + config.log
config.cache
config.status
Makefile
prothon.ncb
prothon.sln
prothon.suo
bison_prothon.bat.lnk
apr
Rules.mk
libtool
Shortcut to rel.bat.lnk
Shortcut to prothon.exe.lnk


Added: trunk/pr/file_test.pr
===================================================================
--- trunk/pr/file_test.pr	2004-03-28 04:01:57 UTC (rev 169)
+++ trunk/pr/file_test.pr	2004-03-28 05:46:41 UTC (rev 170)
@@ -0,0 +1,41 @@
+
+# file_test.pr
+
+f = File("test.txt", 'w')
+for i in 5:
+	f.write("line "+i+'\n')
+f.writelines( ['line 5\n','line 6\n','line 7\n','line 8\n','line 9\n',])
+f.close()
+
+ref =  ['line 0\n','line 1\n','line 2\n','line 3\n','line 4\n',]
+ref += ['line 5\n','line 6\n','line 7\n','line 8\n','line 9\n',]
+
+f = File("test.txt")
+x = f.read()
+print x
+if (x != ''.join(ref)): 
+	print "test failed: x != f"
+	Sys.exit(1)
+
+f.seek(7)
+x = f.read(7)
+print x
+if (x != 'line 1\n'): 
+	print "test failed: x != 'line 1'"
+	Sys.exit(1)
+
+f.seek(21)
+x = f.read(14)
+print x
+if (x != 'line 3\nline 4\n'): 
+	print "test failed: x != 'line 3line 4'"
+	Sys.exit(1)
+
+x = f.readlines()
+ref = ['line 5\n','line 6\n','line 7\n','line 8\n','line 9\n',]
+print x
+if (''.join(x) != ''.join(ref)): 
+	print "test failed: x != 'line 5','line 6','line 7','line 8','line 9'"
+	Sys.exit(1)
+
+print "\nall tests passed"
\ No newline at end of file

Modified: trunk/src/builtins.c
===================================================================
--- trunk/src/builtins.c	2004-03-28 04:01:57 UTC (rev 169)
+++ trunk/src/builtins.c	2004-03-28 05:46:41 UTC (rev 170)
@@ -404,6 +404,244 @@
 	return res;
 }
 
+// ***************************** 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(PR_TRUE);
+	else				 return OBJ(PR_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(float_self);  del_unlock(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(float_self); del_unlock(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(PR_FALSE);
+	}
+	if (Int_value(self) == Int_value(other)) return OBJ(PR_TRUE);
+	else                                     return OBJ(PR_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
+	apr_snprintf(str, sizeof(str),   "%d", Int_value(self));
+#else
+	apr_snprintf(str, sizeof(str), "%lld", (long long) Int_value(self));
+#endif
+	return new_string_obj(str);
+}
+
+DEF (INT_OBJ, __gen__, NULL) {
+	obj_p gen_obj = new_int_obj(0);
+	gen_obj->unmutable = PR_FALSE;
+	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;
+}
+
+DEF(INT_OBJ, chr, NULL){
+	char s[1];
+	s[0] = (char) Int_value(self);
+	return new_string_n_obj(s, 1);
+}
+
 // ***************************** STRING_PROTO *********************************
 #define STRING_OBJ   OBJ(STRING_PROTO)
 
@@ -777,248 +1015,6 @@
 	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(PR_TRUE);
-	else				 return OBJ(PR_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(float_self);  del_unlock(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(float_self); del_unlock(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(PR_FALSE);
-	}
-	if (Int_value(self) == Int_value(other)) return OBJ(PR_TRUE);
-	else                                     return OBJ(PR_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));
-}
-
-#ifndef WIN32
-#define LONG_LONG_CAST	(long long)
-#else
-#define LONG_LONG_CAST
-#endif
-
-DEF(INT_OBJ, __str__, NULL){
-	char str[24];
-
-	apr_snprintf(str, sizeof(str), "%lld", LONG_LONG_CAST Int_value(self));
-
-	return new_string_obj(str);
-}
-
-DEF (INT_OBJ, __gen__, NULL) {
-	obj_p gen_obj = new_int_obj(0);
-	gen_obj->unmutable = PR_FALSE;
-	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;
-}
-
-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)
 

Modified: trunk/status.txt
===================================================================
--- trunk/status.txt	2004-03-28 04:01:57 UTC (rev 169)
+++ trunk/status.txt	2004-03-28 05:46:41 UTC (rev 170)
@@ -3,7 +3,30 @@
 
 Python add-on C module adapter for Prothon
 
-Check out Guido's regret list for Python
+Guido's regret list for Python
+	drop 3-way compare? (but... comparing lists) (what is this MCH?)
+	use (...) continuation
+	drop lambda
+	drop map(), filter()
+	drop reduce()
+	drop `x`, repr()
+	print should've been a function
+		- write(x, y, z)
+		– writeln(x, y, z)
+		– spaces between items controlled by keyword arg
+	make range() return an iterator
+	drop buffer()
+	raw_input(): use sys.stdin.readline()
+	input(): use eval(sys.stdin.readline())
+	callable(): just call it, already
+	execfile(), reload(): use exec()
+	compile(): put in sys
+	exec should be a function
+	drop locals(), globals(), vars()
+	need more ways to convert float to int
+		(round, truncate-towards-zero, floor, ceil)
+	need differentiate to __int__ which
+		truncates and __int__ which doesn't
 
 add/replace int and float single decimal type with bounded precision and floating point. 
 				http://www.python.org/peps/pep-0327.html
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.