It's simple and practical - Visitor Combination and Traversal Control.
The main idea is to separate specific code that executes concrete actions from the code that effects visits check.
Identy | Do nothing |
Sequence(v1,v2) | Sequentially perform visitor v2 after v1. |
Fail | Raise exception. |
Choice(v1,v2) | Try visitor v1. If v1 fails, try v2 (left-biased choice). |
All(v) | Apply visitor v sequentially to every immediate subtree. |
One(v) | Apply visitor v sequentially to the immediate subtrees until it succeeds. |
So, it's rather flexible to implement different actions by combine it and inherit from it.
TopDown(v) = def Sequence(v,All(TopDown(v))) BottomUp(v) = def Sequence(All(BottomUp(v)),v) IfZeroAddOne = def TopDown(Try(Sequence(IsZero,AddOne)))
|
No comments:
Post a Comment