#!/usr/bin/env python3

import string

print("digraph hal_nets {")
print("""


graph [
rankdir = "LR"
];
node [
fontsize = "8"
];


""")

# defs
component_hash = {}
pin_to_comp = {}

with open("pin.out", "r") as f:
    for line in f:
        parts = line.split()
        if len(parts) < 5:
            continue
        comp_name, pin_type, pin_dir, pin_value, pin_name = parts[:5]
        if comp_name not in component_hash:
            component_hash[comp_name] = []
        component_hash[comp_name].append(pin_name)
        pin_to_comp[pin_name] = comp_name

for comp in component_hash.keys():
    comp_labels = ["<" + c + "> " + c for c in component_hash[comp]]
    print("\"" + comp + "\" [")
    print("\tlabel = \"" + " | ".join(comp_labels) + "\"")
    print("\tshape = \"record\"")
    print("]")
    print("\n\n\n")

# nets
net_list = []

with open("pin.out", "r") as f:
    for line in f:
        sig_list = line.split()
        if len(sig_list) < 3:
            continue
        sig_type, sig_value = sig_list[0:2]
        sig_declarations = sig_list[2:]
        for count in range(len(sig_declarations)):
            if sig_declarations[count] in ["<==", "==>"]:
                if count - 1 < 0 or count + 1 >= len(sig_declarations):
                    continue
                pin1 = sig_declarations[count - 1]
                pin2 = sig_declarations[count + 1]

                comp1 = pin_to_comp.get(pin1)
                comp1 = ("\"" + comp1 + "\":" ) if comp1 is not None else ""

                comp2 = pin_to_comp.get(pin2)
                comp2 = ("\"" + comp2 + "\":" ) if comp2 is not None else ""

                comp_pin1 = comp1 + "\"" + pin1 + "\""
                comp_pin2 = comp2 + "\"" + pin2 + "\""

                if sig_declarations[count] == "<==":
                    # "a <== b" ===>> "b -> a"
                    net_list.append(comp_pin2 + " -> " + comp_pin1)
                if sig_declarations[count] == "==>":
                    # "a ==> b" ===>> "a -> b"
                    net_list.append(comp_pin1 + " -> " + comp_pin2)

for line in net_list:
    print(line + ";")

print("}")
