Skip to content

Instantly share code, notes, and snippets.

@MattOates
Created December 8, 2017 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattOates/cb90873a330f72669605d4681a4bf739 to your computer and use it in GitHub Desktop.
Save MattOates/cb90873a330f72669605d4681a4bf739 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use English;
use v5.10;
my %pins;
#Read lines from files given as cli arguments
while (my $line = <<>>) {
#Trim the newline
chomp $line;
#Use a named capture regex to breakup the line into tokens populating %+
$line =~ /(?<io>input|output|inout) (?<size>\[[^\]]+\] )?(?<name>.)/ or next;
my $pin = $LAST_PAREN_MATCH{name};
if (exists $pins{$pin}) {
#Handle the case of promoting to 'inout' if we've already seen one of the other directions
$pins{$pin}{io} = ($pins{$pin}{io} ne $LAST_PAREN_MATCH{io}) ? 'inout' : $pins{$pin}{io};
} else {
$pins{$pin}{io} = $LAST_PAREN_MATCH{io};
#Keep track of the first line we saw this pin name and treat that as the output order
$pins{$pin}{sort_order} = $INPUT_LINE_NUMBER;
}
#Overwrite the size each time with the last seen pin
$pins{$pin}{size} = $LAST_PAREN_MATCH{size} || '';
}
#For the pins in the original order we first saw them in the file
foreach my $pin (sort {$pins{$a}{sort_order} <=> $pins{$b}{sort_order}} keys %pins) {
say "$pins{$pin}{io} $pins{$pin}{size}$pin";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment