Changeset 174
- Timestamp:
- 12/27/06 23:33:05 (2 years ago)
- Files:
-
- pybeast/trunk/dtest/py2py.py (modified) (2 diffs)
- pybeast/trunk/src/py2py.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
pybeast/trunk/dtest/py2py.py
r173 r174 17 17 formatter = Pep8Formatter() 18 18 walk(ast, formatter) 19 msg = 'Failed to reformat "%s" correctly.\n AST: %s\nResult: %s'19 msg = 'Failed to reformat "%s" correctly.\n AST: %s\nResult: "%s"' 20 20 self.failUnless(formatter.dump()==expr, 21 21 msg % (expr, ast, formatter.dump())) 22 23 class Bool(Expr): 24 'Test empty and BoolOp expr handling' 22 25 23 26 def test_empty_expression(self): … … 37 40 self.reformat_and_test('a = 1 or 2') 38 41 42 class BinOp(Expr): 43 'Test BinOp expr handling' 44 45 def test_add(self): 46 'Test Add expr' 47 self.reformat_and_test('a = a + 1') 48 49 def test_sub(self): 50 'Test Sub expr' 51 self.reformat_and_test('a = a - 1') 52 53 def test_mult(self): 54 'Test Mult expr' 55 self.reformat_and_test('a = a * 1') 56 57 # todo - implement 58 # def test_add_and_mult_with_subexpr(self): 59 # 'Test combined add and mult expr' 60 # self.reformat_and_test('(a + b) * c') 61 62 def test_div(self): 63 'Test Div expr' 64 self.reformat_and_test('a = a / 1') 65 66 def test_mod(self): 67 'Test Mod expr' 68 self.reformat_and_test('a = a % 2') 69 70 def test_power(self): 71 'Test Power expr' 72 self.reformat_and_test('a = a ** 2') 73 74 def test_lshift(self): 75 'Test LShift expr' 76 self.reformat_and_test('a = a << 2') 77 78 def test_rshift(self): 79 'Test RShift expr' 80 self.reformat_and_test('a = a >> 2') 81 82 def test_bitor(self): 83 'Test Bitor expr' 84 self.reformat_and_test('a = a | 2') 85 86 def test_bitxor(self): 87 'Test Bitxor expr' 88 self.reformat_and_test('a ^ 2') 89 90 def test_bitand(self): 91 'Test Bitand expr' 92 self.reformat_and_test('a & 1') 93 94 def test_floordiv(self): 95 'Test FloorDiv expr' 96 self.reformat_and_test('a // 5') 97 39 98 pass 40 99 pybeast/trunk/src/py2py.py
r173 r174 5 5 6 6 import compiler 7 import compiler.ast8 7 9 8 class Pep8Formatter: … … 13 12 self.code = '' 14 13 15 def visitAnd(self, node, walker): 16 'visit And node' 17 for subnode in node.nodes: 14 def visit_binop(self, node, walker, code): 15 'visit BinOp node' 16 walker.dispatch(node.left) 17 self.code += ' %s' % code 18 walker.dispatch(node.right) 19 20 def visit_list(self, node, walker, code): 21 'visit list join node' 22 for subnode in node.getChildNodes(): 18 23 walker.dispatch(subnode) 19 self.code += ' and'24 self.code += ' %s' % code 20 25 continue 21 self.code = self.code[:- 4]26 self.code = self.code[:-len(' %s' %code)] 22 27 23 def visit AssName(self, node, *args):28 def visit_assname(self, node, *args): 24 29 'visit AssName node' 25 30 self.code += str(node.name) … … 28 33 return 29 34 30 def visit Const(self, node, *args):35 def visit_const(self, node, *args): 31 36 'visit Const node' 32 37 self.code += ' ' + str(node.value) 33 38 34 def visit Name(self, node, *args):39 def visit_name(self, node, *args): 35 40 'visit Name node' 36 41 self.code += ' ' + str(node.name) 37 42 38 def visitOr(self, node, walker):39 'visit Or node'40 for subnode in node.nodes:41 walker.dispatch(subnode)42 self.code += ' or'43 continue44 self.code = self.code[:-3]45 46 43 def dump(self): 47 44 'Return accumulated source code' 48 return self.code 45 return self.code.strip() 46 47 def map_operation(self, name, default): 48 'Maps node name to the operation' 49 opmap = { 50 'Add' : lambda n, w: self.visit_binop(n, w, '+'), 51 'And' : lambda n, w: self.visit_list(n, w, 'and'), 52 'Bitand' : lambda n, w: self.visit_list(n, w, '&'), 53 'Bitor' : lambda n, w: self.visit_list(n, w, '|'), 54 'Bitxor' : lambda n, w: self.visit_list(n, w, '^'), 55 'Div' : lambda n, w: self.visit_binop(n, w, '/'), 56 'FloorDiv' : lambda n, w: self.visit_binop(n, w, '//'), 57 'LeftShift' : lambda n, w: self.visit_binop(n, w, '<<'), 58 'Mod' : lambda n, w: self.visit_binop(n, w, '%'), 59 'Mul' : lambda n, w: self.visit_binop(n, w, '*'), 60 'Power' : lambda n, w: self.visit_binop(n, w, '**'), 61 'Or' : lambda n, w: self.visit_list(n, w, 'or'), 62 'RightShift' : lambda n, w: self.visit_binop(n, w, '>>'), 63 'Sub' : lambda n, w: self.visit_binop(n, w, '-') 64 } 65 method = opmap.get(name, getattr(self, 'visit_'+name.lower(), default)) 66 return method 67 49 68 pass 50 69 … … 61 80 meth = self._cache.get(klass, None) 62 81 if meth is None: 63 meth = getattr(self.visitor, 'visit' +klass.__name__, self.default)82 meth = self.visitor.map_operation(klass.__name__, self.default) 64 83 self._cache[klass] = meth 65 84 return meth(node, self) … … 67 86 def walk(ast, formatter): 68 87 'Walk AST using supplied formatter and ASTDeepWalker' 69 compiler.walk(ast, formatter, ASTDeepWalker()) 88 walker = ASTDeepWalker() 89 walker.preorder(ast, formatter) 90 return walker.visitor 91 70 92
