[Python-checkins] python/nondist/sandbox/ast astmodule.c,1.7,1.8 test.py,1.4,1.5

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Thu, 30 May 2002 13:09:16 -0700


Update of /cvsroot/python/python/nondist/sandbox/ast
In directory usw-pr-cvs1:/tmp/cvs-serv2919

Modified Files:
	astmodule.c test.py 
Log Message:
Add some preliminary support for subscripts and slices.

Track change of except -> excepthandler.


Index: astmodule.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/astmodule.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** astmodule.c	22 Apr 2002 04:25:19 -0000	1.7
--- astmodule.c	30 May 2002 20:09:13 -0000	1.8
***************
*** 428,431 ****
--- 428,513 ----
  }
  
+ static slice_ty
+ ast_for_slice(node *n)
+ {
+     node *ch;
+     expr_ty lower = NULL, upper = NULL, step = NULL;
+ 
+     REQ(n, subscript);
+     /*
+        subscript: '.' '.' '.' | test | [test] ':' [test] [sliceop]
+        sliceop: ':' [test]
+     */
+     ch = CHILD(n, 0);
+     if (TYPE(ch) == DOT)
+ 	return Ellipsis();
+     if (NCH(n) == 1 && TYPE(ch) == test)
+ 	return Index(ast_for_expr(ch));
+     
+     if (TYPE(ch) == test)
+ 	lower = ast_for_expr(ch);
+ 
+     /* If there's an upper bound it's in the second or third position. */
+     if (TYPE(ch) == COLON) {
+ 	if (NCH(n) > 1) {
+ 	    node *n2 = CHILD(n, 1);
+ 	    if (TYPE(n2) == test)
+ 		upper = ast_for_expr(n2);
+ 	}
+     } else if (NCH(n) > 2) {
+ 	node *n2 = CHILD(n, 2);
+ 	if (TYPE(n2) == test)
+ 	    upper = ast_for_expr(n2);
+     }
+ 
+     ch = CHILD(n, NCH(n) - 1);
+     if (TYPE(ch) == sliceop) {
+ 	if (NCH(ch) == 1)
+ 	    ch = CHILD(ch, 0);
+ 	else
+ 	    ch = CHILD(ch, 1);
+ 	if (TYPE(ch) == test)
+ 	    step = ast_for_expr(ch);
+     }
+     
+     return Slice(lower, upper, step);
+ }
+ 
+ static expr_ty
+ ast_for_subscript(node *n)
+ {
+     node *t, *s;
+     int nch;
+ 
+     /* power: atom trailer* ('**' factor)* 
+        trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME 
+        subscriptlist: subscript (',' subscript)* [',']
+     */
+ 
+     REQ(n, power);
+     REQ(CHILD(n, 0), atom);
+     t = CHILD(n, 1);
+     REQ(t, trailer);
+ 
+     REQ(CHILD(t, 0), LSQB);
+     s = CHILD(t, 1);
+     REQ(CHILD(t, 2), RSQB);
+ 
+     nch = NCH(s);
+     if (nch <= 2) {
+ 	return Subscript(ast_for_atom(CHILD(n, 0)), 
+ 			 ast_for_slice(CHILD(s, 0)), Load);
+     } else {
+ 	int i;
+ 	asdl_seq *slices = asdl_seq_new(nch / 2);
+ 	for (i = 0; i < nch ; i += 2)
+ 	    asdl_seq_append(slices, ast_for_slice(CHILD(s, i)));
+ 	return Subscript(ast_for_atom(CHILD(n, 0)),
+ 			 ExtSlice(slices), Load);
+     }
+ 
+     return NULL;
+ }
+ 
  static expr_ty
  ast_for_expr(node *n)
***************
*** 541,545 ****
  		/* XXX a call */
  	    } else if (TYPE(CHILD(ch, 0)) == LSQB) {
! 		/* XXX a subscript */
  	    } else if (TYPE(CHILD(ch, 0)) == DOT) {
  		return Attribute(ast_for_atom(CHILD(n, 0)),
--- 623,627 ----
  		/* XXX a call */
  	    } else if (TYPE(CHILD(ch, 0)) == LSQB) {
! 		return ast_for_subscript(n);
  	    } else if (TYPE(CHILD(ch, 0)) == DOT) {
  		return Attribute(ast_for_atom(CHILD(n, 0)),
***************
*** 878,881 ****
--- 960,964 ----
      */
      char *s;
+ 
      REQ(n, if_stmt);
  
***************
*** 973,977 ****
  }
  
! static except_ty
  ast_for_except_clause(node *exc, node *body)
  {
--- 1056,1060 ----
  }
  
! static excepthandler_ty
  ast_for_except_clause(node *exc, node *body)
  {
***************
*** 981,991 ****
  
      if (NCH(exc) == 1)
! 	return except(NULL, NULL, ast_for_suite(body));
      else if (NCH(exc) == 2)
! 	return except(ast_for_expr(CHILD(exc, 1)), NULL, ast_for_suite(body));
      else {
  	expr_ty e = ast_for_expr(CHILD(exc, 3));
  	set_context(e, Store);
! 	return except(ast_for_expr(CHILD(exc, 1)), e,
  		      ast_for_suite(body));
      }
--- 1064,1075 ----
  
      if (NCH(exc) == 1)
! 	return excepthandler(NULL, NULL, ast_for_suite(body));
      else if (NCH(exc) == 2)
! 	return excepthandler(ast_for_expr(CHILD(exc, 1)), NULL, 
! 			     ast_for_suite(body));
      else {
  	expr_ty e = ast_for_expr(CHILD(exc, 3));
  	set_context(e, Store);
! 	return excepthandler(ast_for_expr(CHILD(exc, 1)), e,
  		      ast_for_suite(body));
      }

Index: test.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/test.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** test.py	22 Apr 2002 04:25:19 -0000	1.4
--- test.py	30 May 2002 20:09:14 -0000	1.5
***************
*** 123,125 ****
--- 123,137 ----
  [x for x in x]
  obj.attr
+ x[0]
+ x[:]
+ x[1:]
+ x[:1]
+ x[::]
+ x[1:1]
+ x[1::]
+ x[:1:]
+ x[::1]
+ x[1:1:]
+ x[1:1:1]
+ x[1:2,2:3]
  """)