[Libre-soc-bugs] [Bug 1094] insndb instruction database visitor-walker is needed

bugzilla-daemon at libre-soc.org bugzilla-daemon at libre-soc.org
Sat Jun 10 13:23:19 BST 2023


https://bugs.libre-soc.org/show_bug.cgi?id=1094

--- Comment #90 from Luke Kenneth Casson Leighton <lkcl at lkcl.net> ---
okok i have an idea.  first, add visitor and match to visit fn:

 123 def visit(visitor, node, match=None):
         if not match(node): return
 124     with visitor(node=node):
 125         if hasattr(node, "walk"):
 126             for subnode in node.walk(visitor, match):
 127                 visit(visitor=visitor, node=subnode, match=match)

(match missing would have been spotted soon enough, the
 key is adding visitor as an arg to Node.walk() and to def visit())

now add it to walk:

  92 class Dataclass(metaclass=DataclassMeta):
  93     @walkmethod
  94     def walk(clsself, visitor, match=None):

and now, ta-daaa! this can be done:

         if not match(clsself): return # not the clever bit yet...
         yield clsself
         yield from filter(match, visitor.get_any_child_nodes(clsself))

ta-daaa! and the rest moves into Visitor:


class Visitor:
     ....

     def get_any_child_nodes(self, node):
         if type(node) in [bool, str, float, complex, bytes]:
             yield node
         elif ...

and in this way anyone may override that, do whatever the hell they like,
we don't care :)

ahh this is good, it moves the existence of Node out where it
belongs: in Visitor, which is what knows about the format of
the Database.

as long as nobody calls any Database class "get_any_child_nodes"
it is fine :)

but we also start to see why python HTMLlib used "do_" as a prefix,
and why ast.py has "visit_....." as a prefix.

hmmm that's probably a good practice, with *all* Visitors.
otherwise a search you wonder "what the heck def Record and
class Record, moo??"

-- 
You are receiving this mail because:
You are on the CC list for the bug.


More information about the libre-soc-bugs mailing list