[Python-checkins] python/dist/src/Python symtable.c,2.10.8.13,2.10.8.14 newcompile.c,1.1.2.42,1.1.2.43
jhylton@users.sourceforge.net
jhylton@users.sourceforge.net
Fri, 28 Mar 2003 09:22:32 -0800
Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv5708
Modified Files:
Tag: ast-branch
symtable.c newcompile.c
Log Message:
Add lambdas.
Fix symtable bug where lambda args were visited in callers' namespace.
Add debugging dump of symbol table in compiler.
Index: symtable.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/symtable.c,v
retrieving revision 2.10.8.13
retrieving revision 2.10.8.14
diff -C2 -d -r2.10.8.13 -r2.10.8.14
*** symtable.c 28 Mar 2003 02:05:28 -0000 2.10.8.13
--- symtable.c 28 Mar 2003 17:22:24 -0000 2.10.8.14
***************
*** 761,774 ****
VISIT(st, expr, e->v.UnaryOp.operand);
break;
! case Lambda_kind:
if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
return 0;
! VISIT(st, arguments, e->v.Lambda.args);
/* XXX how to get line numbers for expressions */
symtable_enter_block(st, GET_IDENTIFIER(lambda),
FunctionBlock, (void *)e, 0);
VISIT(st, expr, e->v.Lambda.body);
symtable_exit_block(st, (void *)e);
break;
case Dict_kind:
VISIT_SEQ(st, expr, e->v.Dict.keys);
--- 761,777 ----
VISIT(st, expr, e->v.UnaryOp.operand);
break;
! case Lambda_kind: {
if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
return 0;
! if (e->v.Lambda.args->defaults)
! VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
/* XXX how to get line numbers for expressions */
symtable_enter_block(st, GET_IDENTIFIER(lambda),
FunctionBlock, (void *)e, 0);
+ VISIT(st, arguments, e->v.Lambda.args);
VISIT(st, expr, e->v.Lambda.body);
symtable_exit_block(st, (void *)e);
break;
+ }
case Dict_kind:
VISIT_SEQ(st, expr, e->v.Dict.keys);
Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.42
retrieving revision 1.1.2.43
diff -C2 -d -r1.1.2.42 -r1.1.2.43
*** newcompile.c 28 Mar 2003 16:45:43 -0000 1.1.2.42
--- newcompile.c 28 Mar 2003 17:22:26 -0000 1.1.2.43
***************
*** 227,230 ****
--- 227,265 ----
}
+ static void
+ compiler_display_symbols(PyObject *name, PyObject *symbols)
+ {
+ PyObject *key, *value;
+ int flags, pos = 0;
+
+ fprintf(stderr, "block %s\n", PyString_AS_STRING(name));
+ while (PyDict_Next(symbols, &pos, &key, &value)) {
+ flags = PyInt_AsLong(value);
+ fprintf(stderr, "var %s:", PyString_AS_STRING(key));
+ if (flags & DEF_GLOBAL)
+ fprintf(stderr, " declared_global");
+ if (flags & DEF_LOCAL)
+ fprintf(stderr, " local");
+ if (flags & DEF_PARAM)
+ fprintf(stderr, " param");
+ if (flags & DEF_STAR)
+ fprintf(stderr, " stararg");
+ if (flags & DEF_DOUBLESTAR)
+ fprintf(stderr, " starstar");
+ if (flags & DEF_INTUPLE)
+ fprintf(stderr, " tuple");
+ if (flags & DEF_FREE)
+ fprintf(stderr, " free");
+ if (flags & DEF_FREE_GLOBAL)
+ fprintf(stderr, " global");
+ if (flags & DEF_FREE_CLASS)
+ fprintf(stderr, " free/class");
+ if (flags & DEF_IMPORT)
+ fprintf(stderr, " import");
+ fprintf(stderr, "\n");
+ }
+ fprintf(stderr, "\n");
+ }
+
static int
compiler_enter_scope(struct compiler *c, identifier name, void *key)
***************
*** 257,260 ****
--- 292,298 ----
return 0;
+ /* A little debugging output */
+ compiler_display_symbols(name, u->u_ste->ste_symbols);
+
/* Push the old compiler_unit on the stack. */
if (c->u) {
***************
*** 625,631 ****
assert(s->kind == FunctionDef_kind);
- fprintf(stderr, "function %s\n",
- PyString_AS_STRING(s->v.FunctionDef.name));
-
if (args->defaults)
VISIT_SEQ(c, expr, args->defaults);
--- 663,666 ----
***************
*** 649,652 ****
--- 684,720 ----
static int
+ compiler_lambda(struct compiler *c, expr_ty e)
+ {
+ PyCodeObject *co;
+ identifier name;
+ arguments_ty args = e->v.Lambda.args;
+ assert(e->kind == Lambda_kind);
+
+ name = PyString_InternFromString("lambda");
+ if (!name)
+ return 0;
+
+ if (args->defaults)
+ VISIT_SEQ(c, expr, args->defaults);
+ if (!compiler_enter_scope(c, name, (void *)e))
+ return 0;
+ c->u->u_argcount = asdl_seq_LEN(e->v.Lambda.args->args);
+ VISIT(c, expr, e->v.Lambda.body);
+ ADDOP(c, RETURN_VALUE);
+ co = assemble(c);
+ if (co == NULL)
+ return 0;
+ compiler_exit_scope(c);
+
+ /* XXX closure */
+ ADDOP_O(c, LOAD_CONST, (PyObject *)co, consts);
+ ADDOP_I(c, MAKE_FUNCTION, c->u->u_argcount);
+
+ Py_DECREF(name);
+
+ return 1;
+ }
+
+ static int
compiler_print(struct compiler *c, stmt_ty s)
{
***************
*** 1416,1420 ****
break;
case Lambda_kind:
! break;
case Dict_kind:
/* XXX get rid of arg? */
--- 1484,1488 ----
break;
case Lambda_kind:
! return compiler_lambda(c, e);
case Dict_kind:
/* XXX get rid of arg? */