diff --git a/src/FrontEnd/IntermediateCode/fe_icblock.py b/src/FrontEnd/IntermediateCode/fe_icblock.py index d62fcf7..8ede3f5 100644 --- a/src/FrontEnd/IntermediateCode/fe_icblock.py +++ b/src/FrontEnd/IntermediateCode/fe_icblock.py @@ -32,7 +32,6 @@ class FEICBlock( FEICNode ): def __init__(self, parent:FEICNode=None): ''' Constructor. - ''' super().__init__( [] ) ## content is an empty list (of FEICNode-s) self.parent = parent diff --git a/src/FrontEnd/IntermediateCode/fe_icleaf.py b/src/FrontEnd/IntermediateCode/fe_icleaf.py index 87bf614..6df7066 100644 --- a/src/FrontEnd/IntermediateCode/fe_icleaf.py +++ b/src/FrontEnd/IntermediateCode/fe_icleaf.py @@ -40,6 +40,13 @@ def __init__(self, content): content: a reference to the content opf this leaf ''' super().__init__( content ) + + #------------------------------------------------------------------------- + def set_parent(self, parent:FEICNode): + ''' + Sets the parent of this node. + ''' + pass #------------------------------------------------------------------------- def __iter__(self): diff --git a/src/FrontEnd/IntermediateCode/fe_icnode.py b/src/FrontEnd/IntermediateCode/fe_icnode.py index ffcb924..478cf58 100644 --- a/src/FrontEnd/IntermediateCode/fe_icnode.py +++ b/src/FrontEnd/IntermediateCode/fe_icnode.py @@ -39,6 +39,13 @@ def __init__(self, content=None): ''' self.content = content + #------------------------------------------------------------------------- + def set_parent(self, parent:FEICNode): + ''' + Sets the parent of this node. + ''' + self.parent = parent + #------------------------------------------------------------------------- def walk(self): ''' diff --git a/src/FrontEnd/IntermediateCode/fe_ictree.py b/src/FrontEnd/IntermediateCode/fe_ictree.py index ac370b8..1583dda 100644 --- a/src/FrontEnd/IntermediateCode/fe_ictree.py +++ b/src/FrontEnd/IntermediateCode/fe_ictree.py @@ -24,11 +24,46 @@ #============================================================================= from FrontEnd.IntermediateCode.fe_icblock import FEICBlock +from FrontEnd.IntermediateCode.fe_icnode import FEICNode #============================================================================= -FEICTree = FEICBlock -""" -The class of Intermediate Code Trees. -""" +class FEICTree: + """ + The class of Intermediate Code trees. + """ + + #------------------------------------------------------------------------- + def __init__(self): + ''' + Constructor. + ''' + self._root = FEICBlock() + self._current = self._root + + #------------------------------------------------------------------------- + def __iadd__(self, ic_node:(FEICBlock,FEICNode)): + ''' + Appends a new node into this block. + ''' + ic_node.set_parent( self._current ) + self._current += ic_node + if isinstance( ic_node, FEICBlock ): + self._current = ic_node + + #------------------------------------------------------------------------- + def up(self): + ''' + Goes back to parent block in IC Tree. + ''' + self._current = self._current.parent + + #------------------------------------------------------------------------- + def walk(self): + ''' + Walks through this Intermediate Code tree. + Walk-through is depth-first implemented. + ''' + self._root.walk() + #===== end of FrontEnd.IntermediateCode.fe_ictree =====#