micropython

tests/visitor_explicit.py

440:cf329206154e
2011-07-03 Paul Boddie Introduced constant usage tracking.
     1 #!/usr/bin/env python     2      3 class Branch:     4     def __init__(self, nodes):     5         self.nodes = nodes     6      7     def visit(self, visitor):     8         return visitor.visitBranch(self)     9     10 class Leaf:    11     def __init__(self, value):    12         self.value = value    13     14     def visit(self, visitor):    15         return visitor.visitLeaf(self)    16     17 class Visitor:    18     def visit(self, node):    19         return node.visit(self)    20     21     # Visitor-specific functionality.    22     23     def visitBranch(self, node):    24         sum = 0    25         for node in node.nodes:    26             sum += self.visit(node)    27         return sum    28     29     def visitLeaf(self, node):    30         return node.value    31     32 tree = \    33     Branch((    34         Branch((    35             Leaf(10),    36             Leaf(5)    37             )),    38         Leaf(2)    39         ))    40     41 visitor = Visitor()    42 result_17 = visitor.visit(tree)    43     44 # vim: tabstop=4 expandtab shiftwidth=4