Author: mark
Date: 2004-04-16 02:56:07 -0400 (Fri, 16 Apr 2004)
New Revision: 366
Modified:
trunk/src/bytecodes.h
trunk/src/clist.h
trunk/src/interp.c
trunk/src/interp.h
trunk/src/object.c
trunk/src/parser_routines.c
trunk/src/src.vcproj
Log:
Modified: trunk/src/bytecodes.h
===================================================================
--- trunk/src/bytecodes.h 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/bytecodes.h 2004-04-16 06:56:07 UTC (rev 366)
@@ -311,10 +311,11 @@
OP_ASSIGN,
// OP_SEQ_ASSIGN(sequence, symbol refs)
-// assign values in sequence on stack to references on stack
+// assign values in sequence on stack to local references on stack
// if not a sequence treat as sequence of one object
// sequence length must match param length or error
// pop obj and references when done
+// used in for loop and list comprehension
// stack: sequence-obj, reference, reference, ... -> <empty>
// param: number of references
// |opcode|
@@ -356,6 +357,14 @@
// |opcode|
OP_NEWSLICE,
+// APPEND
+// append item object on top of stack to list lower in stack
+// pop item after appending, used in list comprehension
+// stack: list, ..., item -> list, ...
+// param: offset of list from top of stack, -2 -> list is immediately under
item, etc.
+// |opcode|
+OP_APPEND,
+
// PRINT(objects)
// print objects on top of stack
// 0 at top-of-stack means no return at end of line
Modified: trunk/src/clist.h
===================================================================
--- trunk/src/clist.h 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/clist.h 2004-04-16 06:56:07 UTC (rev 366)
@@ -102,5 +102,6 @@
#define clist_push(list, item) ins_clist(list, item,
CLIST_APPEND)
#define clist_push_num(list, num) ins_clist_num(list, num,
CLIST_APPEND)
#define clist_pop(list)
((list)->items[--(list)->len].ptr)
+#define clist_pop_num(list)
((list)->items[--(list)->len].num)
#endif // #define CLIST_H
Modified: trunk/src/interp.c
===================================================================
--- trunk/src/interp.c 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/interp.c 2004-04-16 06:56:07 UTC (rev 366)
@@ -851,6 +851,10 @@
fr_push(ret);
break;
}
+ case OP_APPEND: {
+ obj_p list = fr_stack[fr_sp+param];
+ list_append(ist, list, fr_pop);
+ } break;
case OP_GEN:
case OP_DEF: {
int fparams_len = fr_data_int(1)*2;
@@ -1738,6 +1742,8 @@
break;
case OP_NEWSLICE: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_NEWSLICE",
param, codedata(str, code, op, param, pc));
break;
+ case OP_APPEND: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_APPEND",
param, codedata(str, code, op, param, pc));
+ break;
case OP_PRINT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_PRINT", param,
codedata(str, code, op, param, pc));
break;
case OP_TRYEXCEPT: fprintf(fout,"%4d %20s(%4d) %s", pc, "OP_TRYEXCEPT",
param, codedata(str, code, op, param, pc));
Modified: trunk/src/interp.h
===================================================================
--- trunk/src/interp.h 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/interp.h 2004-04-16 06:56:07 UTC (rev 366)
@@ -134,7 +134,7 @@
#define fr_stack (frame->stack)
#define fr_tos (frame->stack[frame->stack_ptr-1])
#define fr_push(w) frame->stack[frame->stack_ptr++] = w
-#define fr_pop (frame->stack[--frame->stack_ptr])
+#define fr_pop (frame->stack[--(frame->stack_ptr)])
void dump_code(isp ist, obj_p func, code_p code, char* filename);
Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/object.c 2004-04-16 06:56:07 UTC (rev 366)
@@ -450,6 +450,8 @@
void set_attr(isp ist, obj_p obj, obj_p key, obj_p value) {
attr_p attrs, ptr, hole=0;
attr_key_t i, akey, tgt = key->data.key;
+ if (tgt%3 != 0)
+ ptr = NULL;
assert((tgt%3)==0);
wrlock_rtrn(obj);
if (obj->has_attrs) attrs = obj->attr_proto.attrs;
Modified: trunk/src/parser_routines.c
===================================================================
--- trunk/src/parser_routines.c 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/parser_routines.c 2004-04-16 06:56:07 UTC (rev 366)
@@ -753,24 +753,154 @@
clist_p label_to_forrefs(void* param, char* label1, char* label2){
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 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 ) {
+
+}
+
+void calc_for_clause( 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;
+ code_p expr = clist_item(forlst, llen);
+
+ *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);
+ *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,
+ len, stack_depth, max_stack_depth,
loc_list );
+ else
+ calc_for_clause( 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)++;
+ }
+ (*len)++;
+ clist_append_num(loc_list, *len);
+ *len += 6;
+ clist_append_num(loc_list, *len);
+}
+
+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 ) {
+ 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++);
+
+ add_code_to(expr, res, k);
+ res->code_data[*k ].bytecode.opcode = OP_PUSH;
+ res->code_data[(*k)++].bytecode.param = 2;
+ res->code_data[(*k)++].data = SYM(__ITER__);
+ res->code_data[*k ].bytecode.opcode = OP_CALL;
+ res->code_data[(*k)++].bytecode.param = 0;
+ res->code_data[*k ].bytecode.opcode = OP_TRYEXCEPT;
+ res->code_data[*k ].bytecode.param = exc_loc - *k;
+ (*k)++;
+ assert(loop_loc == *k);
+ 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);
+ res->code_data[*k ].bytecode.opcode = OP_CALL;
+ res->code_data[(*k)++].bytecode.param = 0;
+ res->code_data[*k ].bytecode.opcode = OP_PUSH;
+ res->code_data[(*k)++].bytecode.param = llen;
+ for (i=1; i < llen; i++)
+ res->code_data[(*k)++].data = clist_item(forlst,i);
+ res->code_data[*k ].bytecode.opcode = OP_SEQ_ASSIGN;
+ 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),
+ lccp+1, lcc, stk_ofs-1, llp, loc_list );
+ else
+ add_for_clause( res, k, itemexpr, clist_item(lcc, lccp),
+ lccp+1, lcc, stk_ofs-1, llp, 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);
+ }
+ res->code_data[*k ].bytecode.opcode = OP_BR;
+ res->code_data[*k ].bytecode.param = loop_loc - *k;
+ (*k)++;
+ assert(exc_loc == *k);
+ res->code_data[*k ].bytecode.opcode = OP_PUSH;
+ res->code_data[(*k)++].bytecode.param = 3;
+ res->code_data[(*k)++].data = OBJ(STOP_ITERATION_EXC);
+ res->code_data[(*k)++].data = NULL;
+ res->code_data[*k ].bytecode.opcode = OP_EXCEPT;
+ res->code_data[(*k)++].bytecode.param = 1;
+ res->code_data[(*k)++].bytecode.opcode = OP_RERAISE;
+ res->code_data[*k ].bytecode.opcode = OP_POP;
+ res->code_data[(*k)++].bytecode.param = 1;
+ res->stack_depth -=4;
+ assert(end_loc == *k);
+}
+
+// lcc is list of lists
+// lcc list with one entry is if expr
+// lcc list with >= 3 entries is forlst:
+// "tag: FOR tgts in expr", first is tag, last is expr, rest are tgts
+// forlst param is the same forlst format: "tag: FOR tgts in expr"
+// 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;
+ code_p res;
+ clist_p loc_list = new_clist(20);
- return debug_retrn(__LINE__, expr);
+ 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);
+ return debug_retrn(__LINE__, res);
}
-code_p for_lcc_body_else(void* param, clist_p locals, code_p body, code_p els){
- code *res;
+code_p for_lcc_body_else(void* param, clist_p targets, code_p body, code_p
els){
+ code_p res;
int i, k=0, len=0, stack_depth=0, max_stack_depth=0;
- int llen = clist_len(locals)-1, loop_loc, body_loc, els_loc,
end_els_loc, end_loc;
- code_p expr = clist_item(locals, llen);
- patch_break_continues(clist_item(locals,0), body);
+ int llen = clist_len(targets)-1, loop_loc, body_loc, els_loc,
end_els_loc, end_loc;
+ code_p expr = clist_item(targets, llen);
+ patch_break_continues(clist_item(targets,0), body);
for (i=1; i < llen; i++){
- char* s = (char*)clist_item(locals,i);
+ char* s = (char*)clist_item(targets,i);
if ( !(*s >= 'a' && *s <= 'z') && ! *s == '_'){
printf("For variable not local: %s\n",s);
pr_exit(1);
}
- clist_item(locals,i) = sym(INTRP, clist_item(locals,i));
+ clist_item(targets,i) = sym(INTRP, clist_item(targets,i));
}
calc_code(expr, &len, &stack_depth, &max_stack_depth);
len += 4;
@@ -807,7 +937,7 @@
res->code_data[k ].bytecode.opcode = OP_PUSH;
res->code_data[k++].bytecode.param = llen;
for (i=1; i < llen; i++)
- res->code_data[k++].data = clist_item(locals,i);
+ res->code_data[k++].data = clist_item(targets,i);
res->code_data[k ].bytecode.opcode = OP_SEQ_ASSIGN;
res->code_data[k++].bytecode.param = llen-1;
assert(body_loc == k);
Modified: trunk/src/src.vcproj
===================================================================
--- trunk/src/src.vcproj 2004-04-16 02:18:20 UTC (rev 365)
+++ trunk/src/src.vcproj 2004-04-16 06:56:07 UTC (rev 366)
@@ -268,6 +268,9 @@
RelativePath="init.pth">
</File>
<File
+ RelativePath="..\pr\listcomp.pr">
+ </File>
+ <File
RelativePath="..\pr\meow.pr">
</File>
<File
|