Skip to content

Commit

Permalink
feat: Add a function to return descriptions of individual enumeration…
Browse files Browse the repository at this point in the history
… members
  • Loading branch information
abensonca committed Jun 7, 2024
1 parent 6e05898 commit 1720517
Showing 1 changed file with 70 additions and 1 deletion.
71 changes: 70 additions & 1 deletion perl/Galacticus/Build/SourceTree/Process/Enumeration.pm
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,83 @@ sub Process_Enumerations {
&Galacticus::Build::SourceTree::SetVisibility($node->{'parent'},$decodeFunctionName,$visibility);
}
# Create description function.
if ( exists($node->{'directive'}->{'decodeFunction'}) && $node->{'directive'}->{'decodeFunction'} eq "yes" ) {
my $functionName = "enumeration".ucfirst($node->{'directive'}->{'name'})."Description";
my $interface;
$interface .= " interface ".$functionName."\n";
$interface .= " module procedure ".$functionName."Enumerator\n";
$interface .= " module procedure ".$functionName."ID\n";
$interface .= " end interface ".$functionName."\n\n";
my $descriptorFunctionWrapper;
$descriptorFunctionWrapper .= "\n";
$descriptorFunctionWrapper .= " ! Auto-generated enumeration function\n";
$descriptorFunctionWrapper .= " function ".$functionName."Enumerator(enumerationValue)\n";
$descriptorFunctionWrapper .= " !!{\n";
$descriptorFunctionWrapper .= " Return a description of a {\\normalfont \\ttfamily ".$node->{'directive'}->{'name'}."} enumeration member.\n";
$descriptorFunctionWrapper .= " !!}\n";
$descriptorFunctionWrapper .= " use ISO_Varying_String\n";
$descriptorFunctionWrapper .= " implicit none\n\n";
$descriptorFunctionWrapper .= " type (varying_string) :: ".$functionName."Enumerator\n";
$descriptorFunctionWrapper .= " type (enumeration".$node->{'directive'}->{'name'}."Type), intent(in ) :: enumerationValue\n\n";
$descriptorFunctionWrapper .= " ".$functionName."Enumerator=".$functionName."(enumerationValue%ID)\n";
$descriptorFunctionWrapper .= " return\n";
$descriptorFunctionWrapper .= " end function ".$functionName."Enumerator\n";
my $descriptorFunction;
$descriptorFunction .= " function ".$functionName."ID(enumerationValue) result(description)\n";
$descriptorFunction .= " !!{\n";
$descriptorFunction .= " Return a description of a {\\normalfont \\ttfamily ".$node->{'directive'}->{'name'}."} enumeration value.\n";
$descriptorFunction .= " !!}\n";
$descriptorFunction .= " use :: ISO_Varying_String, only : varying_string, assignment(=)\n";
$descriptorFunction .= " implicit none\n";
$descriptorFunction .= " type(varying_string) :: description\n";
$descriptorFunction .= " integer, intent(in) :: enumerationValue\n\n";
my $description = " select case (enumerationValue)\n";
my @entries = &List::ExtraUtils::as_array($node->{'directive'}->{'entry'});
my $i = $indexing-1;
foreach my $entry ( @entries ) {
$description .= " case (".++$i.")\n";
$description .= " description='".(exists($entry->{'description'}) ? $entry->{'description'} : "")."'\n";
}
$description .= " end select\n";
$description .= " return\n";
$descriptorFunction .= " end function ".$functionName."ID\n";
$descriptorFunction .= " ! End auto-generated enumeration function\n";
# Insert into the module.
my $descriptorWrapperTree = &Galacticus::Build::SourceTree::ParseCode($descriptorFunctionWrapper,"Galacticus::Build::SourceTree::Process::Enumeration()", instrument => 0);
my $descriptorTree = &Galacticus::Build::SourceTree::ParseCode($descriptorFunction ,"Galacticus::Build::SourceTree::Process::Enumeration()", instrument => 0);
my @descriptorNodes = &Galacticus::Build::SourceTree::Children ($descriptorTree );
my @descriptorWrapperNodes = &Galacticus::Build::SourceTree::Children ($descriptorWrapperTree );
my $newNode = $descriptorNodes[0];
while ( $newNode->{'type'} ne "function" ) {
$newNode = $newNode->{'sibling'};
}
$newNode = $newNode->{'firstChild'};
while ( defined($newNode->{'sibling'}) ) {
$newNode = $newNode->{'sibling'};
}
my $describeNode =
{
type => "code",
content => $description
};
&Galacticus::Build::SourceTree::InsertAfterNode($newNode,[$describeNode]);
&Galacticus::Build::SourceTree::InsertPostContains($node->{'parent'},\@descriptorNodes );
&Galacticus::Build::SourceTree::InsertPostContains($node->{'parent'},\@descriptorWrapperNodes);
my $interfaceTree = &Galacticus::Build::SourceTree::ParseCode($interface,"Galacticus::Build::SourceTree::Process::Enumeration()");
my @interfaceNodes = &Galacticus::Build::SourceTree::Children($interfaceTree);
&Galacticus::Build::SourceTree::InsertPreContains($node->{'parent'},\@interfaceNodes);
# Set the visibility.
&Galacticus::Build::SourceTree::SetVisibility($node->{'parent'},$functionName,$visibility);
}
# Create describe function.
{
my $functionName = "enumeration".ucfirst($node->{'directive'}->{'name'})."Describe";
my $descriptorFunction;
$descriptorFunction .= "\n";
$descriptorFunction .= " ! Auto-generated enumeration function\n";
$descriptorFunction .= " function ".$functionName."() result(description)\n";
$descriptorFunction .= " !!{\n";
$descriptorFunction .= " Return a description of a {\\normalfont \\ttfamily ".$node->{'directive'}->{'name'}."} enumeration value.\n";
$descriptorFunction .= " Return a description of the {\\normalfont \\ttfamily ".$node->{'directive'}->{'name'}."} enumeration.\n";
$descriptorFunction .= " !!}\n";
$descriptorFunction .= " use :: ISO_Varying_String, only : varying_string, var_str, operator(//)\n";
$descriptorFunction .= " implicit none\n";
Expand Down

0 comments on commit 1720517

Please sign in to comment.