Before refactoring

This commit is contained in:
Jan-Niclas Loosen
2026-01-22 10:02:16 +01:00
parent c66222050b
commit 489f385161
8 changed files with 137 additions and 232 deletions

View File

@@ -1,11 +1,12 @@
class CFG_Node:
__counter = 1
def __init__(self, ast_node = None):
def __init__(self, ast_node=None):
self.ast_node = ast_node
self.children = set()
self.parents = set()
self.label = None # Optional label for the node
self.id = CFG_Node.__counter
CFG_Node.__counter += 1
@@ -15,26 +16,37 @@ class CFG_Node:
def get_parents(self):
return self.parents
def add_child(self, child: CFG_Node, propagate = True):
def add_child(self, child: 'CFG_Node', propagate=True):
if propagate:
child.parents.add(self)
self.children.add(child)
def add_parent(self, parent: CFG_Node, propagate = True):
def add_parent(self, parent: 'CFG_Node', propagate=True):
if propagate:
parent.add_child(self)
self.parents.add(parent)
def remove_child(self, child: CFG_Node, propagate = True):
def remove_child(self, child: 'CFG_Node', propagate=True):
if propagate:
child.parents.remove(self)
self.children.remove(child)
def remove_parent(self, parent: CFG_Node, propagate = True):
def remove_parent(self, parent: 'CFG_Node', propagate=True):
if propagate:
parent.children.remove(self)
self.parents.remove(parent)
def __str__(self):
if self.label:
return f"CFG_Node({self.id}, label='{self.label}')"
elif self.ast_node:
return f"CFG_Node({self.id}, ast={type(self.ast_node).__name__})"
else:
return f"CFG_Node({self.id})"
def __repr__(self):
return self.__str__()
class CFG_START(CFG_Node):
def dot_shape(self):