[Python-checkins] python/dist/src/Python newcompile.c,1.1.2.26,1.1.2.27

jhylton@users.sourceforge.net jhylton@users.sourceforge.net
Tue, 25 Mar 2003 07:54:52 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv31187/Python

Modified Files:
      Tag: ast-branch
	newcompile.c 
Log Message:
Random motion towards augmented assignments.


Index: newcompile.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/Attic/newcompile.c,v
retrieving revision 1.1.2.26
retrieving revision 1.1.2.27
diff -C2 -d -r1.1.2.26 -r1.1.2.27
*** newcompile.c	25 Mar 2003 15:44:55 -0000	1.1.2.26
--- newcompile.c	25 Mar 2003 15:54:45 -0000	1.1.2.27
***************
*** 82,85 ****
--- 82,86 ----
  static int compiler_visit_stmt(struct compiler *, stmt_ty);
  static int compiler_visit_expr(struct compiler *, expr_ty);
+ static int compiler_augassign(struct compiler *, stmt_ty);
  static int compiler_visit_slice(struct compiler *, slice_ty);
  
***************
*** 87,90 ****
--- 88,93 ----
  static void compiler_pop_fblock(struct compiler *, enum fblocktype, int);
  
+ static int inplace_binop(struct compiler *, operator_ty);
+ 
  static PyCodeObject *assemble(struct compiler *);
  
***************
*** 886,889 ****
--- 889,893 ----
          case AugAssign_kind:
  		return compiler_augassign(c, s);
+ 		break;
          case Print_kind:
  		return compiler_print(c, s);
***************
*** 1015,1018 ****
--- 1019,1058 ----
  }
  
+ static int 
+ inplace_binop(struct compiler *c, operator_ty op)
+ {
+ 	switch (op) {
+ 	case Add:
+ 		return INPLACE_ADD;
+ 	case Sub:
+ 		return INPLACE_SUBTRACT;
+ 	case Mult: 
+ 		return INPLACE_MULTIPLY;
+ 	case Div:
+ 		if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
+ 			return INPLACE_TRUE_DIVIDE;
+ 		else
+ 			return INPLACE_DIVIDE;
+ 	case Mod:
+ 		return INPLACE_MODULO;
+ 	case Pow:
+ 		return INPLACE_POWER;
+ 	case LShift: 
+ 		return INPLACE_LSHIFT;
+ 	case RShift:
+ 		return INPLACE_RSHIFT;
+ 	case BitOr:
+ 		return INPLACE_OR;
+ 	case BitXor: 
+ 		return INPLACE_XOR;
+ 	case BitAnd:
+ 		return INPLACE_AND;
+ 	case FloorDiv:
+ 		return INPLACE_FLOOR_DIVIDE;
+ 	}
+ 	assert(0);
+ 	return 0;
+ }
+ 
  static int
  compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
***************
*** 1227,1233 ****
--- 1267,1279 ----
  		VISIT(c, expr, e->v.Attribute.value);
  		switch (e->v.Attribute.ctx) {
+ 		case AugLoad:
+ 			ADDOP(c, DUP_TOP);
+ 			/* Fall through to load */
  		case Load:
  			ADDOP_O(c, LOAD_ATTR, e->v.Attribute.attr, names);
  			break;
+ 		case AugStore:
+ 			ADDOP(c, ROT_TWO);
+ 			/* Fall through to save */
  		case Store:
  			ADDOP_O(c, STORE_ATTR, e->v.Attribute.attr, names);
***************
*** 1236,1242 ****
  			ADDOP_O(c, DELETE_ATTR, e->v.Attribute.attr, names);
  			break;
- 		case AugStore:
- 			/* XXX */
- 			break;
  		case Param:
  			assert(0);
--- 1282,1285 ----
***************
*** 1260,1263 ****
--- 1303,1340 ----
  		ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(e->v.Tuple.elts));
  		break;
+ 	}
+ 	return 1;
+ }
+ 
+ static int 
+ compiler_augassign(struct compiler *c, stmt_ty s)
+ {
+ 	expr_ty e = s->v.AugAssign.target;
+ 	expr_ty auge;
+ 
+ 	assert(s->kind == AugAssign_kind);
+ 
+ 	switch (e->kind) {
+ 	case Attribute_kind:
+ 		auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
+ 				 AugLoad);
+ 		VISIT(c, expr, auge);
+ 		VISIT(c, expr, s->v.AugAssign.value);
+ 		ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
+ 		auge->v.Attribute.ctx = AugStore;
+ 		VISIT(c, expr, auge);
+ 		free(auge);
+ 		break;
+ 	case Subscript_kind:
+ 	    break;
+ 	case Name_kind:
+ 		VISIT(c, expr, s->v.AugAssign.target);
+ 		VISIT(c, expr, s->v.AugAssign.value);
+ 		ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
+ 		return compiler_nameop(c, e->v.Name.id, Store);
+ 		break;
+ 	default:
+ 	    fprintf(stderr, "invalid node type for augmented assignment\n");
+ 	    return 0;
  	}
  	return 1;