File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11#!/usr/bin/env python
22# -*- coding: utf-8 -*-
33
4- import types
5-
64from typing import NamedTuple
75from typing import Optional
86from typing import Set
@@ -35,6 +33,7 @@ class GenericVisitor(NodeVisitor):
3533 """ A slightly different visitor, that just traverses an AST, but does not return
3634 a translation of it. Used to examine the AST or do transformations
3735 """
36+ node_type = ToVisit
3837
3938 @property
4039 def O_LEVEL (self ):
@@ -56,25 +55,9 @@ def TYPE(type_):
5655 return gl .SYMBOL_TABLE .basic_types [type_ ]
5756
5857 def visit (self , node ):
59- stack = [ToVisit (node )]
60- last_result = None
61-
62- while stack :
63- try :
64- last = stack [- 1 ]
65- if isinstance (last , types .GeneratorType ):
66- stack .append (last .send (last_result ))
67- last_result = None
68- elif isinstance (last , ToVisit ):
69- stack .append (self ._visit (stack .pop ()))
70- else :
71- last_result = stack .pop ()
72- except StopIteration :
73- stack .pop ()
74-
75- return last_result
76-
77- def _visit (self , node ):
58+ return super ().visit (ToVisit (node ))
59+
60+ def _visit (self , node : ToVisit ):
7861 if node .obj is None :
7962 return None
8063
Original file line number Diff line number Diff line change 99# the GNU General License
1010# ----------------------------------------------------------------------
1111
12- from typing import Callable , Any
12+ from typing import Any , Callable , Type
1313
1414import types
1515from .tree import Tree
@@ -27,6 +27,8 @@ def token(self):
2727
2828
2929class NodeVisitor :
30+ node_type : Type = Ast
31+
3032 def visit (self , node ):
3133 stack = [node ]
3234 last_result = None
@@ -37,7 +39,7 @@ def visit(self, node):
3739 if isinstance (last , types .GeneratorType ):
3840 stack .append (last .send (last_result ))
3941 last_result = None
40- elif isinstance (last , Ast ):
42+ elif isinstance (last , self . node_type ):
4143 stack .append (self ._visit (stack .pop ()))
4244 else :
4345 last_result = stack .pop ()
@@ -47,9 +49,7 @@ def visit(self, node):
4749 return last_result
4850
4951 def _visit (self , node ):
50- methname = 'visit_' + node .token
51- meth = getattr (self , methname , self .generic_visit )
52-
52+ meth = getattr (self , f"visit_{ node .token } " , self .generic_visit )
5353 return meth (node )
5454
5555 @staticmethod
You can’t perform that action at this time.
0 commit comments