gray/src/gray_params.awk
Michele Guerini Rocco c5a4b180bc
src/gray_params.f90: replace magic numbers with enums
1. Introduces enumerations (and some booleans) intended to replace all
   the magic numbers used throughout the code to represent multiple
   choices.

2. Replace the gray_params.sh script a new one that automatically
   generates code for all the GRAY parameters by parsing
   gray_params.f90.

3. Also generate extra code to accept the enum identifiers as valid
   values in the configuration files and command line arguments.

4. Set sensible default values for parameters that are rarely changes.
2024-02-09 11:16:18 +01:00

70 lines
1.5 KiB
Awk

#/usr/bin/env awk -f
# This script generates the very repetitive code for parsing the
# GRAY parameters from a string. The output is embedded
# into gray_params.f90 using the #include cpp directive.
BEGIN {
# set file to open
ARGV[1] = "src/gray_params.f90"
ARGC = 2
# start enum conversion switch
print "select case (value)"
}
# match an enum constant
/enumerator :: [A-Z0-9_]+/ {
# generate conversion case
print " case ('"$3"')"
print " temp = '"$5"'"
}
# match start of a parameters set
/type [[:alnum:]_]+_parameters$/ {
if (!done_enum) {
# close enum switch
print " case default"
print " temp = value"
print "end select\n"
# start params switch
print "select case (name)"
done_enum = 1
}
# set the current set
split($2, words, "_parameters")
if (words[1] != "gray") set = words[1]
# change separator for the next step
FS = "::"
}
# match a parameter
/ :: [[:alnum:]_]+/ {
if (!set) next
# strip comments
sub(/!.*$/, "")
for (i = 1; i <= split($2, ids, ","); i++) {
# extract identifier
match(ids[i], /^ *[[:alnum:]_]+/)
id = substr(ids[i], RSTART + 1, RLENGTH - 1)
# generate parameter case
print " case ('"set"."id"')"
print " read (temp, *, iostat=err) params%"set"%"id
print " if (err > 0) err = ERR_VALUE"
}
}
# match end of a parameters set
/end type$/ {
set = ""
FS = " "
}
END {
# close the parameters switch
print " case default"
print " ! unknown parameter"
print " err = ERR_UNKNOWN"
print "end select"
}