Changeset 174

Show
Ignore:
Timestamp:
12/27/06 23:33:05 (2 years ago)
Author:
akhavr
Message:

ticket:370:

  • BinOp implemented, but more complex expressions like '(a + b) * c'
    still fail
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • pybeast/trunk/dtest/py2py.py

    r173 r174  
    1717        formatter = Pep8Formatter() 
    1818        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"
    2020        self.failUnless(formatter.dump()==expr, 
    2121                        msg % (expr, ast, formatter.dump())) 
     22     
     23class Bool(Expr): 
     24    'Test empty and BoolOp expr handling' 
    2225     
    2326    def test_empty_expression(self): 
     
    3740        self.reformat_and_test('a = 1 or 2') 
    3841 
     42class 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 
    3998    pass 
    4099 
  • pybeast/trunk/src/py2py.py

    r173 r174  
    55 
    66import compiler 
    7 import compiler.ast 
    87 
    98class Pep8Formatter: 
     
    1312        self.code = '' 
    1413 
    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(): 
    1823            walker.dispatch(subnode) 
    19             self.code += ' and' 
     24            self.code += ' %s' % code 
    2025            continue 
    21         self.code = self.code[:-4
     26        self.code = self.code[:-len(' %s' %code)
    2227 
    23     def visitAssName(self, node, *args): 
     28    def visit_assname(self, node, *args): 
    2429        'visit AssName node' 
    2530        self.code += str(node.name) 
     
    2833        return 
    2934 
    30     def visitConst(self, node, *args): 
     35    def visit_const(self, node, *args): 
    3136        'visit Const node' 
    3237        self.code += ' ' + str(node.value) 
    3338 
    34     def visitName(self, node, *args): 
     39    def visit_name(self, node, *args): 
    3540        'visit Name node' 
    3641        self.code += ' ' + str(node.name) 
    3742 
    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             continue 
    44         self.code = self.code[:-3] 
    45  
    4643    def dump(self): 
    4744        '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 
    4968    pass 
    5069 
     
    6180        meth = self._cache.get(klass, None) 
    6281        if meth is None: 
    63             meth = getattr(self.visitor, 'visit' + klass.__name__, self.default) 
     82            meth = self.visitor.map_operation(klass.__name__, self.default) 
    6483            self._cache[klass] = meth 
    6584        return meth(node, self) 
     
    6786def walk(ast, formatter): 
    6887    '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 
    7092