First try
This commit is contained in:
48
Project-02-03-04/cfg/CFG.py
Normal file
48
Project-02-03-04/cfg/CFG.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from .CFG_Node import *
|
||||
|
||||
|
||||
class CFG:
|
||||
def __init__(self, in_node: CFG_Node, out_node: CFG_Node):
|
||||
self.in_node = in_node
|
||||
self.out_node = out_node
|
||||
|
||||
def to_dot(self) -> str:
|
||||
visited = set()
|
||||
lines = ["digraph CFG {"]
|
||||
|
||||
# optionale Defaults
|
||||
lines.append(' node [fontname="Helvetica"];')
|
||||
|
||||
def node_label(node: CFG_Node) -> str:
|
||||
# Basislabel aus dem Knoten
|
||||
base = node.dot_label() if hasattr(node, "dot_label") else ""
|
||||
|
||||
# semantisches Label aus AST
|
||||
if node.ast_node is not None:
|
||||
semantic = str(node.ast_node)
|
||||
return f"{base}\\n{semantic}" if base else semantic
|
||||
|
||||
return base
|
||||
|
||||
def node_shape(node: CFG_Node) -> str:
|
||||
return node.dot_shape() if hasattr(node, "dot_shape") else "box"
|
||||
|
||||
def visit(node: CFG_Node):
|
||||
if node.id in visited:
|
||||
return
|
||||
visited.add(node.id)
|
||||
|
||||
label = node_label(node)
|
||||
shape = node_shape(node)
|
||||
|
||||
lines.append(
|
||||
f' n{node.id} [label="{label}", shape={shape}];'
|
||||
)
|
||||
|
||||
for child in sorted(node.children, key=lambda n: n.id):
|
||||
lines.append(f" n{node.id} -> n{child.id};")
|
||||
visit(child)
|
||||
|
||||
visit(self.in_node)
|
||||
lines.append("}")
|
||||
return "\n".join(lines)
|
||||
Reference in New Issue
Block a user