rev 307 - 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-09 05:03:41 -0400 (Fri, 09 Apr 2004)
New Revision: 307

Modified:
   trunk/STATUS.txt
   trunk/include/prothon/prothon.h
   trunk/pr/chained.pr
   trunk/src/builtins-core.c
   trunk/src/bytecodes.h
   trunk/src/interp.c
   trunk/src/parser.h
   trunk/src/parser_routines.c
   trunk/src/symbol.c
Log:
fixed a < b < c so b only evaluates once,
chained compares are finally finished,
cleaned up chained.pr again

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/STATUS.txt	2004-04-09 09:03:41 UTC (rev 307)
@@ -1,8 +1,6 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
---- a < b < c evaluates b twice
-
 --- add list comprehension
 
 --- add thread object so prothon code can create threads

Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/include/prothon/prothon.h	2004-04-09 09:03:41 UTC (rev 307)
@@ -347,6 +347,8 @@
 
 	__INIT__,
 	__BOOL__QUES,	
+	__GETCMP__,
+	__CMPVALOBJ__,
 	__GEN__,
 	__DOC__,
 	__HASH__,

Modified: trunk/pr/chained.pr
===================================================================
--- trunk/pr/chained.pr	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/pr/chained.pr	2004-04-09 09:03:41 UTC (rev 307)
@@ -21,16 +21,13 @@
 
 print 1 >= 1 == 2 != 2 == 2 > 1 <= 1 <= 2
 
-
 def f(i):
 	print i,
 	return i
 	
-print "\n\nThis should print 0 1 1 2\n"
-
+print "\n\nThis should print 0 1 2\n"
 f(0) < f(1) < f(2)
 
 print "\n\nThis should print 0 1\n"
-
 f(0) > f(1) < f(2)
 
Modified: trunk/src/builtins-core.c
===================================================================
--- trunk/src/builtins-core.c	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/src/builtins-core.c	2004-04-09 09:03:41 UTC (rev 307)
@@ -299,8 +299,10 @@
 	else
 		return new_int_obj(ist, -1); 
 }
+DEF(True, __getcmp__, NULL) { 
+	return get_attr(ist, self, SYM(__CMPVALOBJ__)); 
+}
 
-
 // ***************************** NONE *****************************************
 
 MODULE_START(None)
@@ -606,6 +608,7 @@
 	MODULE_ADD_SYM(True, __str__);
 	MODULE_ADD_SYM(True, __bool__QUES);
 	MODULE_ADD_SYM(True, cmp);
+	MODULE_ADD_SYM(True, __getcmp__);
 
 	MODULE_SUB_INIT(None);
 	MODULE_ADD_SYM(None, __str__);

Modified: trunk/src/bytecodes.h
===================================================================
--- trunk/src/bytecodes.h	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/src/bytecodes.h	2004-04-09 09:03:41 UTC (rev 307)
@@ -227,10 +227,17 @@
 // |opcode|
 OP_POP,
 
+// SWAP
+// swap top two items on stap
+// stack: item1, item2 -> item2, item1
+// param: <unused>
+// |opcode|
+OP_SWAP,
+
 // DUPLICATE
-// duplicate item on top-of-stack
+// duplicate item on stack
 // stack: item -> item,item
-// param: <unused>
+// param: sp offset of item to duplicate, -1: tos, -2: second item on stack, ...
 // |opcode|
 OP_DUPLICATE,
 
@@ -254,6 +261,15 @@
 // |opcode|
 OP_PUSH_SELF,
 
+// OP_CHAIN_CMP
+// make chain compare object from items on stack
+// stack: VALUE_OBJ, True/False -> cmp_object
+// if True:  cmp_object = new_object with proto == True object, add __getcmp__(): return VALUE_OBJ
+// else: cmp_object = False object
+// param: <unused>
+// |opcode|
+OP_CHAIN_CMP,
+
 // OBJCALL(func_obj, params)
 // call unbound function object & params on stack
 // use locals container for self

Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/src/interp.c	2004-04-09 09:03:41 UTC (rev 307)
@@ -626,10 +626,24 @@
 				fr_push(temp);
 				break;
 			case OP_DUPLICATE: {
-				obj_p tmp = fr_tos;
+				obj_p tmp = fr_stack[fr_sp+param];
 				fr_push(tmp);
 				break;
 			}
+			case OP_CHAIN_CMP:
+				fr_sp -= 2;
+				if (fr_stack[fr_sp+1] == OBJ(PR_TRUE)) {
+					obj_p cmp_obj = new_object(ist, OBJ(PR_TRUE));
+					set_attr(ist, cmp_obj, SYM(__CMPVALOBJ__), fr_stack[fr_sp]);
+					fr_push(cmp_obj);
+				} else 
+					fr_push(OBJ(PR_FALSE));
+				break;
+			case OP_SWAP: { 
+				obj_p tos = fr_tos;
+				fr_tos = fr_stack[fr_sp-2];
+				fr_stack[fr_sp-2] = tos;
+			}	break;
 			case OP_POP: 
 				for(i=0; i < param; i++) 
 					return_value = fr_pop; 
@@ -1512,6 +1526,10 @@
 		break;
 	case OP_PUSH: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_PUSH", param, codedata(str, code, op, param, pc));
 		break;
+	case OP_CHAIN_CMP: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_CHAIN_CMP", param, codedata(str, code, op, param, pc));
+		break;
+	case OP_SWAP: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_SWAP", param, codedata(str, code, op, param, pc));
+		break;
 	case OP_DEF: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_DEF", param, codedata(str, code, op, param, pc));
 		break;
 	case OP_PUSH_LOCAL_REF: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_PUSH_LOCAL_REF", param, codedata(str, code, op, param, pc));

Modified: trunk/src/parser.h
===================================================================
--- trunk/src/parser.h	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/src/parser.h	2004-04-09 09:03:41 UTC (rev 307)
@@ -80,7 +80,7 @@
 	int 		stack_depth;
 	int 		max_stack_depth;
 	int			assign_parm_cnt;
-	code_p		cmp_expr;
+	int			cmp_expr;
 	char*		file_path;
 	clist_p		srcmap;
 	int			doc_flg;

Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/src/parser_routines.c	2004-04-09 09:03:41 UTC (rev 307)
@@ -179,8 +179,6 @@
 	add_all_srcmap(res, newcode, *k, free_flg);
 	*k += newcode->len;
 	if (free_flg) {
-		if (newcode->cmp_expr)
-			pr_free(newcode->cmp_expr);
 		pr_free(newcode);
 	}
 }
@@ -266,8 +264,6 @@
 			p1->max_stack_depth = max(p1->max_stack_depth, p1->stack_depth + p3->max_stack_depth);
 			p1->stack_depth = p1->stack_depth + p3->stack_depth;
 			p1->doc_flg = p1->doc_flg || p3->doc_flg;
-			if (p3->cmp_expr)
-				pr_free(p3->cmp_expr);
 			pr_free(p3);
 			return debug_retrn(__LINE__, p1);
 		} else if (size) {
@@ -572,45 +568,65 @@
 	parm->code_data[parm->len-1].bytecode.param  = 0;
 	return debug_retrn(__LINE__, parm);
 }
-code_p bool_and_expr(void* param, code_p lbool, code_p rparm){
-	int k=0;
-	code_p res = new_code( param,  lbool->len + 3 + rparm->len, 1, 
-				          max( max( lbool->max_stack_depth, 
-				          lbool->stack_depth+1+rparm->max_stack_depth ), 3 ), 0 );
-	add_code_to(lbool, res, &k);
-	res->code_data[k  ].bytecode.opcode = OP_BEQ;
-	res->code_data[k++].bytecode.param  = rparm->len+3;
-	res->code_data[k++].data            = OBJ(PR_FALSE);
-	res->code_data[k  ].bytecode.opcode = OP_POP;
-	res->code_data[k++].bytecode.param  = 1;
-	add_code_no_free(rparm, res, &k);
-	return debug_retrn(__LINE__, res);
-}
 code_p binary(void* param, code_p lparm, int op, code_p rparm){
 	code_p res;
-	int k=0, have_cmp_op = (op >= __LT__QUES && op <= __EQ__QUES);
+	int have_lparm = FALSE;
+	int k=0, len=0, stack_depth=4, max_stack_depth = 4;
+	int have_cmp_op = (op >= __LT__QUES && op <= __EQ__QUES);
 	if (lparm->cmp_expr && have_cmp_op) {
-		code_p and_rparm, bin_lparm = lparm->cmp_expr;
-		lparm->cmp_expr = NULL;
-		and_rparm = binary(param, bin_lparm, op, rparm);
-		res = bool_and_expr(param, lparm, and_rparm);
-		res->cmp_expr = rparm;
-	} else {
-		res = new_code( param, lparm->len + 3 + rparm->len + 1, 
-						lparm->stack_depth, max( max( lparm->max_stack_depth, 
-						lparm->stack_depth+2+rparm->max_stack_depth), 4), 0  );
+		lparm->cmp_expr = FALSE;
+		calc_code(lparm, &len, &stack_depth, &max_stack_depth);
+		len += 5;
+		calc_code(rparm, &len, &stack_depth, &max_stack_depth);
+		len += 7;
+		res = new_code(param, len, stack_depth, max_stack_depth, 0);
+		res->cmp_expr = TRUE;
 		add_code_to(lparm, res, &k);
+		res->code_data[k  ].bytecode.opcode = OP_BEQ;
+		res->code_data[k  ].bytecode.param  = res->len-k;
+		k++;
+		res->code_data[k++].data            = OBJ(PR_FALSE);
 		res->code_data[k  ].bytecode.opcode = OP_PUSH;
+		res->code_data[k++].bytecode.param  = 2;
+		res->code_data[k++].data = SYM(__GETCMP__);
+		res->code_data[k  ].bytecode.opcode = OP_CALL;
+		res->code_data[k++].bytecode.param  = 0;
+		have_lparm = TRUE;
+		goto got_lparm;
+	} else {
+		calc_code(lparm, &len, &stack_depth, &max_stack_depth);
+		if (have_cmp_op) {
+			calc_code(rparm, &len, &stack_depth, &max_stack_depth);
+			len++;
+		}
+		len += 3;
+		if (have_cmp_op) len++;
+		else calc_code(rparm, &len, &stack_depth, &max_stack_depth);
+		len++;
+		if (have_cmp_op) len++;
+		res = new_code(param, len, stack_depth, max_stack_depth, 0);
+got_lparm:
+		if (!have_lparm)
+			add_code_to(lparm, res, &k);			
+		if (have_cmp_op) {
+			res->cmp_expr = TRUE;
+			add_code_to(rparm, res, &k);
+			res->code_data[k++].bytecode.opcode = OP_SWAP;
+		}
+		res->code_data[k  ].bytecode.opcode = OP_PUSH;
 		res->code_data[k++].bytecode.param  = 3;
 		res->code_data[k++].data = sym_id_table[op].id;
 		res->code_data[k++].data = PARAM_NORMAL;
 		if (have_cmp_op) {
-			res->cmp_expr = rparm;
-			add_code_no_free(rparm, res, &k);
+			res->code_data[k  ].bytecode.opcode = OP_DUPLICATE;
+			res->code_data[k++].bytecode.param  = -4;
 		} else
 			add_code_to(rparm, res, &k);
 		res->code_data[k  ].bytecode.opcode = OP_CALL;
 		res->code_data[k++].bytecode.param  = 1;
+		if (have_cmp_op)
+			res->code_data[k++].bytecode.opcode = OP_CHAIN_CMP;
+		res->stack_depth = 1;
 		assert(k==res->len);
 	}
 	return debug_retrn(__LINE__, res);
@@ -621,7 +637,8 @@
 				          max( max( lparm->max_stack_depth, 
 				          lparm->stack_depth+1+rparm->max_stack_depth ), 3 ), 0 );
 	add_code_to(lparm, res, &k);
-	res->code_data[k++].bytecode.opcode = OP_DUPLICATE;
+	res->code_data[k  ].bytecode.opcode = OP_DUPLICATE;
+	res->code_data[k++].bytecode.param  = -1;
 	res->code_data[k  ].bytecode.opcode = OP_PUSH;
 	res->code_data[k++].bytecode.param  = 2;
 	res->code_data[k++].data = SYM(__BOOL__QUES);
@@ -773,7 +790,8 @@
 	res->code_data[k ].bytecode.param  = els_loc - k;
 	k++;
 	assert(loop_loc == k);
-	res->code_data[k++].bytecode.opcode = OP_DUPLICATE;
+	res->code_data[k  ].bytecode.opcode = OP_DUPLICATE;
+	res->code_data[k++].bytecode.param  = -1;
 	res->code_data[k  ].bytecode.opcode = OP_PUSH;
 	res->code_data[k++].bytecode.param  = 2;
 	res->code_data[k++].data = SYM(NEXT);

Modified: trunk/src/symbol.c
===================================================================
--- trunk/src/symbol.c	2004-04-09 06:58:27 UTC (rev 306)
+++ trunk/src/symbol.c	2004-04-09 09:03:41 UTC (rev 307)
@@ -117,6 +117,8 @@
 	{ "__rcmp__",		0 },
 	{ "__init__",		0 }, 
 	{ "__bool__QUES",	0 }, 
+	{ "__getcmp__",		0 }, 
+	{ "__cmpvalobj__",	0 }, 
 	{ "__gen__",		0 }, 
 	{ "__doc__",		0 }, 
 	{ "__hash__",		0 },
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.