rev 368 - in trunk: pr src
SVN User <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-04-16 12:26:11 -0400 (Fri, 16 Apr 2004)
New Revision: 368
Modified:
trunk/pr/listcomp.pr
trunk/src/parser_routines.c
Log:
list comps now work with multiple FORs, IFs not working yet
Modified: trunk/pr/listcomp.pr
===================================================================
--- trunk/pr/listcomp.pr 2004-04-16 06:58:41 UTC (rev 367)
+++ trunk/pr/listcomp.pr 2004-04-16 16:26:11 UTC (rev 368)
@@ -1,16 +1,18 @@
#!/usr/bin/env prothon
# listcomp.pr
+print [(x,y) for x in 2 for y in 2]
print
+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
-*/
\ No newline at end of file
+
Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c 2004-04-16 06:58:41 UTC (rev 367)
+++ trunk/src/parser_routines.c 2004-04-16 16:26:11 UTC (rev 368)
@@ -762,7 +762,7 @@
}
void add_if_clause( code_p res, int *k, code_p itemexpr, clist_p ifexpr,
- int lccp, clist_p lcc, int stk_ofs, int llp, clist_p loc_list ) {
+ int lccp, clist_p lcc, int stk_ofs, clist_p loc_list ) {
}
@@ -771,12 +771,13 @@
int len_lcc = clist_len(lcc);
int llen = clist_len(forlst)-1;
code_p expr = clist_item(forlst, llen);
+ int loop_loc, exc_loc, end_loc;
*stack_depth += 3;
*max_stack_depth = max(*max_stack_depth, *stack_depth);
calc_code(expr, len, stack_depth, max_stack_depth);
*len += 4;
- clist_append_num(loc_list, *len);
+ loop_loc = *len;
*len += llen-1+6;
if (lccp < len_lcc) {
if (clist_len(clist_item(lcc, lccp)) == 1)
@@ -790,23 +791,34 @@
(*len)++;
}
(*len)++;
- clist_append_num(loc_list, *len);
+ exc_loc = *len;
*len += 6;
- clist_append_num(loc_list, *len);
+ end_loc = *len;
+ clist_push_num(loc_list, end_loc);
+ clist_push_num(loc_list, exc_loc);
+ clist_push_num(loc_list, loop_loc);
}
-void add_for_clause( code_p res, int *k, code_p itemexpr, clist_p forlst,
- int lccp, clist_p lcc, int stk_ofs, int llp, 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 ) {
int i, len_lcc = clist_len(lcc);
int llen = clist_len(forlst)-1;
code_p expr = clist_item(forlst, llen);
int loop_loc, exc_loc, end_loc;
- assert(clist_len(loc_list) >= llp+3);
- loop_loc = clist_num(loc_list, llp++);
- exc_loc = clist_num(loc_list, llp++);
- end_loc = clist_num(loc_list, llp++);
+ assert(clist_len(loc_list) >= 3);
+ loop_loc = clist_pop_num(loc_list);
+ exc_loc = clist_pop_num(loc_list);
+ end_loc = clist_pop_num(loc_list);
+ for (i=1; i < llen; i++){
+ char* s = (char*)clist_item(forlst,i);
+ if ( !(*s >= 'a' && *s <= 'z') && ! *s == '_'){
+ printf("For variable not local: %s\n",s);
+ pr_exit(1);
+ }
+ clist_item(forlst,i) = sym(INTRP, clist_item(forlst,i));
+ }
add_code_to(expr, res, k);
res->code_data[*k ].bytecode.opcode = OP_PUSH;
res->code_data[(*k)++].bytecode.param = 2;
@@ -833,15 +845,16 @@
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),
- lccp+1, lcc, stk_ofs-1, llp, loc_list );
+ lccp+1, lcc, stk_ofs-1, loc_list );
else
- add_for_clause( res, k, itemexpr, clist_item(lcc, lccp),
- lccp+1, lcc, stk_ofs-1, llp, loc_list );
+ 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) == llp);
+ assert(clist_len(loc_list) == 0);
+ free_clist(loc_list);
}
res->code_data[*k ].bytecode.opcode = OP_BR;
res->code_data[*k ].bytecode.param = loop_loc - *k;
@@ -868,23 +881,16 @@
// expr param is list comprehension expr
code_p expr_for_lcc_to_listcomp(void* param, code_p expr, clist_p forlst, clist_p lcc) {
int k=0, len=1, stack_depth=1, max_stack_depth=1;
- int i, llen = clist_len(forlst)-1;
+ int llen = clist_len(forlst)-1;
code_p res;
- clist_p loc_list = new_clist(20);
+ clist_p loc_list = new_clist(9);
- for (i=1; i < llen; i++){
- char* s = (char*)clist_item(forlst,i);
- if ( !(*s >= 'a' && *s <= 'z') && ! *s == '_'){
- printf("For variable not local: %s\n",s);
- pr_exit(1);
- }
- clist_item(forlst,i) = sym(INTRP, clist_item(forlst,i));
- }
calc_for_clause(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;
- add_for_clause(res, &k, expr, forlst, 0, lcc, -1, 0, loc_list);
+ add_for_clause(param, res, &k, expr, forlst, 0, lcc, -1, loc_list);
+ res->stack_depth -=1;
return debug_retrn(__LINE__, res);
}