[Python-checkins] python/nondist/sandbox/ast astmodule.c,1.4,1.5 test.py,1.2,1.3
jhylton@sourceforge.net
jhylton@sourceforge.net
Fri, 19 Apr 2002 15:15:14 -0700
Update of /cvsroot/python/python/nondist/sandbox/ast
In directory usw-pr-cvs1:/tmp/cvs-serv24712
Modified Files:
astmodule.c test.py
Log Message:
Several new statement types handled & misc changes.
Handle Delete(), Import(), ImportFrom(), If(), For().
ast_for_exprlist() also takes a context argument used to set the
context flag on the expressions.
Add tests for new statements.
Index: astmodule.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/astmodule.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** astmodule.c 16 Apr 2002 23:43:00 -0000 1.4
--- astmodule.c 19 Apr 2002 22:15:12 -0000 1.5
***************
*** 14,17 ****
--- 14,18 ----
static asdl_seq *seq_for_testlist(node *);
static expr_ty ast_for_expr(node *);
+ static stmt_ty ast_for_stmt(node *);
extern grammar _PyParser_Grammar; /* From graminit.c */
***************
*** 112,115 ****
--- 113,117 ----
}
else if (NCH(n) == 2) {
+ /* handle "not in" and "is not" */
switch (TYPE(CHILD(n, 0))) {
case NAME:
***************
*** 402,424 ****
}
! static stmt_ty
! ast_for_del_stmt(node *n)
{
- /* del_stmt: 'del' exprlist */
- int i;
asdl_seq *seq;
expr_ty e;
- node *ch;
! REQ(n, del_stmt);
! ch = CHILD(n, 1);
! REQ(ch, exprlist);
! seq = asdl_seq_new((NCH(ch) + 1) / 2);
! for (i = 0; i < NCH(ch); i += 2) {
! e = ast_for_expr(CHILD(ch, i));
! set_context(e, Del);
asdl_seq_append(seq, e);
}
! return Delete(seq);
}
--- 404,432 ----
}
! static asdl_seq *
! ast_for_exprlist(node *n, int context)
{
asdl_seq *seq;
+ int i;
expr_ty e;
! REQ(n, exprlist);
!
! seq = asdl_seq_new((NCH(n) + 1) / 2);
! for (i = 0; i < NCH(n); i += 2) {
! e = ast_for_expr(CHILD(n, i));
! if (context)
! set_context(e, context);
asdl_seq_append(seq, e);
}
! return seq;
! }
!
! static stmt_ty
! ast_for_del_stmt(node *n)
! {
! /* del_stmt: 'del' exprlist */
! REQ(n, del_stmt);
! return Delete(ast_for_exprlist(CHILD(n, 1), Del));
}
***************
*** 470,473 ****
--- 478,546 ----
}
+ static alias_ty
+ alias_for_import_name(node *n)
+ {
+ /*
+ import_as_name: NAME [NAME NAME]
+ dotted_as_name: dotted_name [NAME NAME]
+ dotted_name: NAME ('.' NAME)*
+ */
+ loop:
+ switch (TYPE(n)) {
+ case import_as_name:
+ if (NCH(n) == 3)
+ return alias(PyString_InternFromString(STR(CHILD(n, 0))),
+ PyString_InternFromString(STR(CHILD(n, 2))));
+ else
+ return alias(PyString_InternFromString(STR(CHILD(n, 0))),
+ NULL);
+ break;
+ case dotted_as_name:
+ if (NCH(n) == 1) {
+ n = CHILD(n, 0);
+ goto loop;
+ } else {
+ alias_ty a = alias_for_import_name(CHILD(n, 0));
+ assert(!a->asname);
+ a->asname = PyString_InternFromString(STR(CHILD(n, 2)));
+ return a;
+ }
+ break;
+ case dotted_name:
+ if (NCH(n) == 1)
+ return alias(PyString_InternFromString(STR(CHILD(n, 0))), NULL);
+ else {
+ /* Create a string of the form "a.b.c" */
+ int i, len;
+ PyObject *str;
+ char *s;
+
+ len = 0;
+ for (i = 0; i < NCH(n); i += 2)
+ /* length of string plus one for the dot */
+ len += strlen(STR(CHILD(n, i))) + 1;
+ len--; /* the last name doesn't have a dot */
+ str = PyString_FromStringAndSize(NULL, len);
+ s = PyString_AS_STRING(str);
+ for (i = 0; i < NCH(n); i += 2) {
+ char *sch = STR(CHILD(n, i));
+ strcpy(s, STR(CHILD(n, i)));
+ s += strlen(sch);
+ *s++ = '.';
+ }
+ --s;
+ *s = '\0';
+ PyString_InternInPlace(&str);
+ return alias(str, NULL);
+ }
+ break;
+ case STAR:
+ return alias(PyString_InternFromString("*"), NULL);
+ default:
+ return NULL;
+ }
+ return NULL;
+ }
+
static stmt_ty
ast_for_import_stmt(node *n)
***************
*** 477,490 ****
| 'from' dotted_name 'import' ('*'
| import_as_name (',' import_as_name)*)
- import_as_name: NAME [NAME NAME]
- dotted_as_name: dotted_name [NAME NAME]
- dotted_name: NAME ('.' NAME)*
*/
!
! /* XXX how to represent dotted names? */
REQ(n, import_stmt);
if (STR(CHILD(n, 0))[0] == 'i') { /* import */
} else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
}
return NULL;
--- 550,569 ----
| 'from' dotted_name 'import' ('*'
| import_as_name (',' import_as_name)*)
*/
! int i;
! asdl_seq *aliases;
REQ(n, import_stmt);
if (STR(CHILD(n, 0))[0] == 'i') { /* import */
+ aliases = asdl_seq_new(NCH(n) / 2);
+ for (i = 1; i < NCH(n); i += 2)
+ asdl_seq_append(aliases, alias_for_import_name(CHILD(n, i)));
+ return Import(aliases);
} else if (STR(CHILD(n, 0))[0] == 'f') { /* from */
+ alias_ty mod = alias_for_import_name(CHILD(n, 1));
+ aliases = asdl_seq_new((NCH(n) - 2) / 2);
+ for (i = 3; i <= NCH(n); i += 2)
+ asdl_seq_append(aliases, alias_for_import_name(CHILD(n, i)));
+ return ImportFrom(mod->name, aliases);
}
return NULL;
***************
*** 541,548 ****
--- 620,709 ----
}
+ static asdl_seq *
+ ast_for_suite(node *n)
+ {
+ /* suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT */
+ asdl_seq *seq;
+ int i;
+ fprintf(stderr, "ast_for_suite(%d)\n", TYPE(n));
+ REQ(n, suite);
+
+ fprintf(stderr, "ast_for_suite(CHILD(n, 0)==%d)\n", TYPE(CHILD(n, 0)));
+
+ if (NCH(n) == 1) {
+ /* XXX punt on stmt; stmt */
+ REQ(CHILD(n, 0), simple_stmt);
+ seq = asdl_seq_new(1);
+ asdl_seq_append(seq, ast_for_stmt(CHILD(n, 0)));
+ return seq;
+ }
+ fprintf(stderr, "\t%d\n", NCH(n));
+ /* one child node for each stmt, plus 3 children for NL, {, & } */
+ seq = asdl_seq_new(NCH(n) - 3);
+ for (i = 2; i < NCH(n) - 1; i++) {
+ fprintf(stderr, "\t%d: %d %d\n", i, TYPE(CHILD(n, i)),
+ NCH(CHILD(n, i)));
+ REQ(CHILD(n, i), stmt);
+ asdl_seq_append(seq, ast_for_stmt(CHILD(n, i)));
+ }
+ return seq;
+ }
+
static stmt_ty
ast_for_if_stmt(node *n)
{
+ /* if_stmt: 'if' test ':' suite ('elif' test ':' suite)*
+ ['else' ':' suite]
+ */
REQ(n, if_stmt);
+ char *s;
+
+ if (NCH(n) == 4)
+ return If(ast_for_expr(CHILD(n, 1)),
+ ast_for_suite(CHILD(n, 3)), NULL);
+ s = STR(CHILD(n, 4));
+ /* s[2], the third character in the string, will be
+ 's' for el_s_e, or
+ 'i' for el_i_f
+ */
+ if (s[2] == 's')
+ return If(ast_for_expr(CHILD(n, 1)),
+ ast_for_suite(CHILD(n, 3)),
+ ast_for_suite(CHILD(n, 6)));
+ else {
+ int i, n_elif, has_else = 0;
+ asdl_seq *orelse = NULL;
+ n_elif = NCH(n) - 4;
+ if (TYPE(CHILD(n, n_elif)) == NAME
+ && STR(CHILD(n, n_elif))[2] == 's') {
+ has_else = 1;
+ n_elif -= 3;
+ }
+ n_elif /= 4;
+
+ if (has_else) {
+ orelse = asdl_seq_new(1);
+ asdl_seq_append(orelse,
+ If(ast_for_expr(CHILD(n, NCH(n) - 6)),
+ ast_for_suite(CHILD(n, NCH(n) - 4)),
+ ast_for_suite(CHILD(n, NCH(n) - 1))));
+ /* the just-created orelse handled the last elif */
+ n_elif--;
+ } else
+ orelse = NULL;
+
+ for (i = 0; i < n_elif; i++) {
+ int off = 5 + (n_elif - i - 1) * 4;
+ orelse = asdl_seq_new(1);
+ asdl_seq_append(orelse,
+ If(ast_for_expr(CHILD(n, off)),
+ ast_for_suite(CHILD(n, off + 2)),
+ orelse));
+ }
+ return If(ast_for_expr(CHILD(n, 1)),
+ ast_for_suite(CHILD(n, 3)),
+ orelse);
+ }
+
return NULL;
}
***************
*** 551,555 ****
--- 712,720 ----
ast_for_while_stmt(node *n)
{
+ /* while_stmt: 'while' test ':' suite ['else' ':' suite] */
REQ(n, while_stmt);
+
+
+
return NULL;
}
***************
*** 558,562 ****
--- 723,746 ----
ast_for_for_stmt(node *n)
{
+ asdl_seq *_target = NULL, *seq = NULL;
+ expr_ty target;
+ /* for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite] */
REQ(n, for_stmt);
+
+ if (NCH(n) == 9)
+ seq = ast_for_suite(CHILD(n, 8));
+
+ _target = ast_for_exprlist(CHILD(n, 1), 0);
+ if (asdl_seq_LEN(_target) == 1) {
+ target = asdl_seq_get(_target, 0);
+ asdl_seq_free(_target);
+ }
+ else
+ target = Tuple(_target, Store);
+
+ return For(target,
+ ast_for_testlist(CHILD(n, 3)),
+ ast_for_suite(CHILD(n, 5)),
+ seq);
return NULL;
}
Index: test.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/test.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** test.py 16 Apr 2002 23:43:00 -0000 1.2
--- test.py 19 Apr 2002 22:15:12 -0000 1.3
***************
*** 25,27 ****
--- 25,62 ----
raise x, y
raise x, y, b
+ import a
+ import a, b
+ import a as b
+ import a as b, c
+ import as as as, c
+ import a, b as c
+ from a import b
+ from a import b as c
+ from a import b, c
+ from a import b as c, d
+ from a import b, c as d
+ import a.b
+ from a.b import c
+ if a:
+ print b
+ a
+ if a:
+ a
+ else:
+ print b
+ c
+ if a:
+ a
+ elif b:
+ b
+ if a == 1:
+ a
+ elif b == 1:
+ b
+ elif c == c:
+ a
+ b
+ c
+ else:
+ pass
""")