micropython

tests/visitor_explicit.py

442:13aae946513b
2011-07-05 Paul Boddie Introduced Instance() in place of None as a result and for the value of the active expression where no definitive object can be deduced. Made all Instance values compare equal to each other in order to avoid duplication in sets. Improved Constant comparisons. Fixed assignment counting where many values are provided in a single assignment. Added inspection support for conditional expressions (since they are used in the standard library).
     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