rev 369 - in trunk: pr src

SVN User <[email protected]>
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-04-16 14:34:31 -0400 (Fri, 16 Apr 2004)
New Revision: 369

Modified:
   trunk/pr/listcomp.pr
   trunk/src/parser_routines.c
Log:
list comprehension finished and tested

Modified: trunk/pr/listcomp.pr
===================================================================
--- trunk/pr/listcomp.pr	2004-04-16 16:26:11 UTC (rev 368)
+++ trunk/pr/listcomp.pr	2004-04-16 18:34:31 UTC (rev 369)
@@ -1,18 +1,22 @@
 #!/usr/bin/env prothon
 
 # listcomp.pr
-print [(x,y) for x in 2 for y in 2]
 
+lst = [x for x in 4]
+if lst != [0, 1, 2, 3]:
+	print "err1"
+	Sys.exit(1)
+	
+lst = [(x,y) for x in 2 for y in 2]
+if lst != [(0, 0), (0, 1), (1, 0), (1, 1)]:
+	print "err2"
+	Sys.exit(1)
+	
+lst = [(x,y) for x in 4 if x%2==0 for y in 4 if y%2==1]
+if lst != [(0, 1), (0, 3), (2, 1), (2, 3)]:
+	print "err3"
+	Sys.exit(1)
+
 print
+print "all tests passed"
 print
-print
-print "The following line should be [0, 1, 2, 3]"
-print
-print [x for x in 4]
-print
-print
-print "The following line should be [(0, 0), (0, 1), (1, 0), (1, 1)]"
-print
-print [(x,y) for x in 2 for y in 2]
-print
-
Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c	2004-04-16 16:26:11 UTC (rev 368)
+++ trunk/src/parser_routines.c	2004-04-16 18:34:31 UTC (rev 369)
@@ -754,19 +754,77 @@
 	return new_clist_2(label1, label2);
 }
 
-void calc_if_clause( code_p itemexpr, clist_p ifexpr, int lccp, clist_p lcc,
-				     int *len, int *stack_depth, int *max_stack_depth, clist_p loc_list  ) {
+void calc_for_clause( void* param, code_p itemexpr, clist_p forlst, int lccp, clist_p lcc,
+				      int *len, int *stack_depth, int *max_stack_depth, clist_p loc_list  );
 
+void add_for_clause( void* param, code_p res, int *k, code_p itemexpr, clist_p forlst, 
+				     int lccp, clist_p lcc, int stk_ofs, clist_p loc_list );
 
+void calc_if_clause( void* param, code_p itemexpr, code_p ifexpr, int lccp, clist_p lcc,
+				     int *len, int *stack_depth, int *max_stack_depth, clist_p loc_list  ) {
+	int len_lcc	= clist_len(lcc);
+	int end_loc;
+	
+	*stack_depth += 2;
+	*max_stack_depth = max(*max_stack_depth, *stack_depth);
 
+	calc_code(ifexpr, len, stack_depth, max_stack_depth);
+	*len += 5;
+	if (lccp < len_lcc) {
+		if (clist_len(clist_item(lcc, lccp)) == 1)
+			calc_if_clause( param, itemexpr, clist_item(clist_item(lcc, lccp), 0), lccp+1, lcc,
+				            len, stack_depth, max_stack_depth, loc_list );
+		else 
+			calc_for_clause( param, itemexpr, clist_item(lcc, lccp), lccp+1, lcc,
+				             len, stack_depth, max_stack_depth, loc_list );
+	} else {
+		calc_code(itemexpr, len, stack_depth, max_stack_depth);
+		(*len)++;
+	}
+	end_loc = *len;
+	(*len)++;
+	clist_push_num(loc_list,  end_loc);
 }
 
-void add_if_clause( code_p res, int *k, code_p itemexpr, clist_p ifexpr, 
+void add_if_clause( void* param, code_p res, int *k, code_p itemexpr, code_p ifexpr, 
 				    int lccp, clist_p lcc, int stk_ofs, clist_p loc_list  ) {
+	int len_lcc	= clist_len(lcc);
+	int end_loc;
 
+	assert(clist_len(loc_list) >= 1);
+	end_loc  = clist_pop_num(loc_list);
+
+	add_code_to(ifexpr, res, k);
+	res->code_data[(*k)  ].bytecode.opcode = OP_PUSH;
+	res->code_data[(*k)++].bytecode.param  = 2;
+	res->code_data[(*k)++].data = SYM(__BOOL__QUES);
+	res->code_data[(*k)  ].bytecode.opcode = OP_CALL;
+	res->code_data[(*k)++].bytecode.param  = 0;
+	res->code_data[(*k)  ].bytecode.opcode = OP_BEQ;
+	res->code_data[(*k)  ].bytecode.param  = end_loc - *k;
+	(*k)++;
+	res->code_data[(*k)++].data = OBJ(PR_FALSE);
+	if (lccp < len_lcc) {
+		if (clist_len(clist_item(lcc, lccp)) == 1)
+			add_if_clause( param, res, k, itemexpr, clist_item(clist_item(lcc, lccp), 0), 
+			               lccp+1, lcc, stk_ofs-1, loc_list );
+		else 
+			add_for_clause( param, res, k, itemexpr, clist_item(lcc, lccp),
+			                lccp+1, lcc, stk_ofs-1, loc_list );
+	} else {
+		add_code_to(itemexpr, res, k);
+		res->code_data[*k    ].bytecode.opcode = OP_APPEND;
+		res->code_data[(*k)++].bytecode.param  = stk_ofs-2;
+		assert(clist_len(loc_list) == 0);
+		free_clist(loc_list);
+	}
+	assert(end_loc == *k);
+	res->code_data[*k    ].bytecode.opcode = OP_POP;
+	res->code_data[(*k)++].bytecode.param  = 1;
+	res->stack_depth -=3;
 }
 
-void calc_for_clause( code_p itemexpr, clist_p forlst, int lccp, clist_p lcc,
+void calc_for_clause( void* param, code_p itemexpr, clist_p forlst, int lccp, clist_p lcc,
 				      int *len, int *stack_depth, int *max_stack_depth, clist_p loc_list  ) {
 	int len_lcc	 = clist_len(lcc);
 	int	llen	 = clist_len(forlst)-1;
@@ -781,10 +839,10 @@
 	*len += llen-1+6;
 	if (lccp < len_lcc) {
 		if (clist_len(clist_item(lcc, lccp)) == 1)
-			calc_if_clause( itemexpr, clist_item(clist_item(lcc, lccp), 0), lccp+1, lcc,
+			calc_if_clause( param, itemexpr, clist_item(clist_item(lcc, lccp), 0), lccp+1, lcc,
 				            len, stack_depth, max_stack_depth, loc_list );
 		else 
-			calc_for_clause( itemexpr, clist_item(lcc, lccp), lccp+1, lcc,
+			calc_for_clause( param, itemexpr, clist_item(lcc, lccp), lccp+1, lcc,
 				             len, stack_depth, max_stack_depth, loc_list );
 	} else {
 		calc_code(itemexpr, len, stack_depth, max_stack_depth);
@@ -844,7 +902,7 @@
 	res->code_data[(*k)++].bytecode.param  = llen-1;
 	if (lccp < len_lcc) {
 		if (clist_len(clist_item(lcc, lccp)) == 1)
-			add_if_clause( res, k, itemexpr, clist_item(clist_item(lcc, lccp), 0), 
+			add_if_clause( param, res, k, itemexpr, clist_item(clist_item(lcc, lccp), 0), 
 			               lccp+1, lcc, stk_ofs-1, loc_list );
 		else 
 			add_for_clause( param, res, k, itemexpr, clist_item(lcc, lccp),
@@ -885,7 +943,7 @@
 	code_p res;
 	clist_p loc_list = new_clist(9);
 
-	calc_for_clause(expr, forlst, 0, lcc, &len, &stack_depth, &max_stack_depth, loc_list); 
+	calc_for_clause(param, expr, forlst, 0, lcc, &len, &stack_depth, &max_stack_depth, loc_list); 
 	res = new_code(param, len, stack_depth, max_stack_depth, 0);
 	res->code_data[k  ].bytecode.opcode = OP_NEWLIST;
 	res->code_data[k++].bytecode.param  = 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.