First acceptable solution
This commit is contained in:
@@ -5,7 +5,10 @@ class CFG_Node:
|
||||
self.ast_node = ast_node
|
||||
self.children = set()
|
||||
self.parents = set()
|
||||
self.label = None # Optional label for the node
|
||||
|
||||
self.label = None
|
||||
self.dot_shape = 'box'
|
||||
self.dot_style = ''
|
||||
|
||||
self.id = CFG_Node.__counter
|
||||
CFG_Node.__counter += 1
|
||||
@@ -36,44 +39,58 @@ class CFG_Node:
|
||||
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 dot_label(self):
|
||||
# Prioritize custom label
|
||||
if self.label is not None:
|
||||
return self.label
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
# Build label from AST node
|
||||
if self.ast_node is not None:
|
||||
return str(self.ast_node)
|
||||
|
||||
return None
|
||||
|
||||
def is_filled(self):
|
||||
return not self.is_empty()
|
||||
|
||||
def is_empty(self):
|
||||
# Node is empty if it has no label and no related AST node
|
||||
if self.label is None or self.label == "None":
|
||||
if self.ast_node is not None:
|
||||
# Node belongs to a ast node
|
||||
return False
|
||||
return True
|
||||
# Node is required for the control flow
|
||||
return False
|
||||
|
||||
class CFG_START(CFG_Node):
|
||||
def dot_shape(self):
|
||||
return "box"
|
||||
|
||||
def dot_label(self):
|
||||
return "START"
|
||||
|
||||
def __init__(self, ast_node=None):
|
||||
super().__init__(ast_node)
|
||||
self.dot_shape = "ellipse"
|
||||
self.dot_style = 'style=filled, color=green'
|
||||
self.label = "START"
|
||||
|
||||
class CFG_END(CFG_Node):
|
||||
def dot_shape(self):
|
||||
return "box"
|
||||
|
||||
def dot_label(self):
|
||||
return "END"
|
||||
|
||||
def __init__(self, ast_node=None):
|
||||
super().__init__(ast_node)
|
||||
self.dot_shape = "ellipse"
|
||||
self.dot_style = 'style=filled, color=green'
|
||||
self.label = "END"
|
||||
|
||||
class CFG_DIAMOND(CFG_Node):
|
||||
def dot_shape(self):
|
||||
return "diamond"
|
||||
|
||||
def __init__(self, ast_node=None):
|
||||
super().__init__(ast_node)
|
||||
self.dot_shape = "diamond"
|
||||
self.label = "<?>"
|
||||
|
||||
class CFG_CALL(CFG_Node):
|
||||
def dot_shape(self):
|
||||
return "box"
|
||||
|
||||
def __init__(self, ast_node=None):
|
||||
super().__init__(ast_node)
|
||||
self.dot_style = 'style=filled, color=orange'
|
||||
self.dot_shape = "box"
|
||||
|
||||
class CFG_RETURN(CFG_Node):
|
||||
def dot_shape(self):
|
||||
return "box"
|
||||
def __init__(self, ast_node=None):
|
||||
super().__init__(ast_node)
|
||||
self.dot_style = 'style=filled, color=orange'
|
||||
self.dot_shape = "box"
|
||||
|
||||
Reference in New Issue
Block a user