10c8ef08472dfdb4f6a7d93a6cab38f1b698cdf4
commit 10c8ef08472dfdb4f6a7d93a6cab38f1b698cdf4
Author: spesk <spesk@pm.me>
Date: Mon Mar 21 14:08:27 2022 -0400

PoC compiler and grapher working, need to test with non trivial patches/cases

diff --git a/pl_proto.pl b/pl_proto.pl
index 57a1494..df13256 100755
--- a/pl_proto.pl
+++ b/pl_proto.pl
@@ -3,6 +3,8 @@
use strict;
use warnings;
use Data::Dumper;
+use lib "/home/swatson/perl5/lib/perl5";
+use GraphViz2;

my $SOURCE_FILE = $ARGV[0];

@@ -357,5 +359,92 @@ foreach my $line ( split("\n", $src_content) ) {
line_parse($line);
}

- # Dump the AST for now. This is where we'd render the output
+# Dump the AST for now. This is where we'd render the output
print Dumper %AST;
+
+my $graph = GraphViz2->new(
+ edge => {color => 'grey'},
+ global => {directed => 1},
+ graph => {label => $AST{'Title'}, rankdir => 'TB'},
+ node => {shape => 'square'},
+);
+
+# Given a mod_ref, construct a graph object
+# that represents a module
+sub module_node_constructor($$) {
+ my $graph = shift;
+ my $mod_ref = shift;
+ $graph->push_subgraph(
+ name => "cluster_$$mod_ref{'Module'}",
+ graph => {label => "$$mod_ref{'Module'}"},
+ node => {color => 'black', shape => 'circle'},
+ );
+ foreach my $node_name ( keys %{$mod_ref} ) {
+ if ( $node_name =~ m/Module|Manufacturer|Rev/ ) {
+ $graph->add_node(name => "$node_name : $$mod_ref{$node_name}", shape => 'oval');
+ }
+ }
+
+ if ( defined $AST{'Sets'}->{$$mod_ref{'Module'}} ) {
+ my $sets_ref = $AST{'Sets'}->{$$mod_ref{'Module'}};
+ foreach my $set ( keys %{$sets_ref} ) {
+ my $name = "cluster" . "_" . "$set";
+ my $label = "$set" . "_" . "settings";
+ $graph->push_subgraph(
+ name => $name,
+ graph => {label => $label},
+ node => {color => "red", shape => 'square'},
+ );
+ $graph->add_node(name => "$set : $$sets_ref{$set}->{'Param'} : $$sets_ref{$set}->{'Value'}");
+ $graph->pop_subgraph;
+ }
+ }
+
+ # Handle connections
+ if ( defined $AST{'Connections'} ) {
+ my $conn_ref = $AST{'Connections'};
+ foreach my $conn ( keys %{$conn_ref} ) {
+ if ($$mod_ref{'Module'} eq $$conn_ref{$conn}->{'Output_Module'} ) {
+ my $name = "cluster" . "_" . "$$conn_ref{$conn}->{'Output_Port'}";
+ my $label = "$$conn_ref{$conn}->{'Output_Port'}";
+ $graph->push_subgraph(
+ name => $name,
+ graph => {label => $label},
+ node => {color => "blue"},
+ );
+ $graph->add_node(name => "$$conn_ref{$conn}->{'Output_Port'}");
+ $graph->pop_subgraph;
+ } elsif ($$mod_ref{'Module'} eq $$conn_ref{$conn}->{'Input_Module'} ) {
+ my $name = "cluster" . "_" . "$$conn_ref{$conn}->{'Input_Mod_Dst'}";
+ my $label = "$$conn_ref{$conn}->{'Input_Mod_Dst'}";
+ $graph->push_subgraph(
+ name => $name,
+ graph => {label => $label},
+ node => {color => 'purple'},
+ );
+ $graph->add_node(name => "$$conn_ref{$conn}->{'Input_Mod_Dst'}");
+ $graph->pop_subgraph;
+ }
+
+ }
+ }
+
+ $graph->pop_subgraph;
+}
+
+sub connection_node_constructor($) {
+ my $graph = shift;
+}
+
+foreach my $mod_ref ( @{$AST{'Modules'}} ) {
+ module_node_constructor($graph,$mod_ref);
+}
+# Draw connections
+foreach my $conn_ref ( keys %{$AST{'Connections'}} ) {
+ my $from = $AST{'Connections'}->{$conn_ref}->{'Output_Port'};
+ my $to = $AST{'Connections'}->{$conn_ref}->{'Input_Mod_Dst'};
+ $graph->add_edge(from => $from, to => $to);
+}
+
+my $format = 'svg';
+$graph->run(format => $format, output_file => "test.svg");