From 04c2cf8f937c579c2709fcda6e7336ceb2d71158 Mon Sep 17 00:00:00 2001 From: Not Zed Date: Wed, 14 Sep 2022 11:12:05 +0930 Subject: [PATCH] Move the config functions to config.pm --- src/notzed.nativez/bin/generate-api | 8 +- src/notzed.nativez/lib/api.pm | 131 ++++++++-------------------- src/notzed.nativez/lib/code.pm | 5 +- src/notzed.nativez/lib/config.pm | 32 +++++++ 4 files changed, 73 insertions(+), 103 deletions(-) diff --git a/src/notzed.nativez/bin/generate-api b/src/notzed.nativez/bin/generate-api index f185237..3c4c903 100755 --- a/src/notzed.nativez/bin/generate-api +++ b/src/notzed.nativez/bin/generate-api @@ -142,8 +142,8 @@ sub formatItems { push @{$res->{func}}, $inc->{literal}; } elsif ($inc->{type} eq 'func') { my $def = $api->{index}->{'func:'}; - my $func = api::optionValue('func:template', 'code:method=invoke', $inc, $res->{template}, $def); - my $init = api::optionValue('init:template', undef, $inc, $res->{template}, $def); + my $func = config::optionValue('func:template', 'code:method=invoke', $inc, $res->{template}, $def); + my $init = config::optionValue('init:template', undef, $inc, $res->{template}, $def); my $funct = $api->findTemplateName($func); my $initt = $api->findTemplateName($init) if defined $init; @@ -230,7 +230,7 @@ sub formatStruct { } @list; } $m->{access}; - } split(/,/,api::optionValue('template', undef, $type)); + } split(/,/,config::optionValue('template', undef, $type)); } @membersOutput; my $res = { @@ -285,7 +285,7 @@ sub exportLibraries { next if $api->{output}->{"$obj->{type}:$obj->{name}"}; next if $obj->{output} != 1; - my $library = $api->findTemplateName(api::optionValue('template', 'code:class=library', $obj, $def)); + my $library = $api->findTemplateName(config::optionValue('template', 'code:class=library', $obj, $def)); my $res = { ctx => $obj, library => [], diff --git a/src/notzed.nativez/lib/api.pm b/src/notzed.nativez/lib/api.pm index 6b46c70..7bbe8f6 100644 --- a/src/notzed.nativez/lib/api.pm +++ b/src/notzed.nativez/lib/api.pm @@ -245,11 +245,11 @@ sub initType { $type->{items}->{$inc->{match}} = $inc->{literal}; } - my $v = optionValue('select', undef, $obj); + my $v = config::optionValue('select', undef, $obj); $type->{select} = $v if defined($v); #print "init $obj->{name}\n"; - foreach my $c (split /,/,optionValue('copy', undef, $obj)) { + foreach my $c (split /,/,config::optionValue('copy', undef, $obj)) { my $copy = $index->{$c}; if ($copy) { @@ -309,7 +309,7 @@ sub queryTemplate { my $s = shift; my $sname = $s->{"$s->{type}:template"}; my $def = $api->{index}->{"$s->{type}:"}; - my $name = $sname ? $sname : api::optionValue('template', $s->{size} == 0 ? 'code:class=handle' : 'code:class=struct', $obj, $def); + my $name = $sname ? $sname : config::optionValue('template', $s->{size} == 0 ? 'code:class=handle' : 'code:class=struct', $obj, $def); return $name; } @@ -320,79 +320,13 @@ sub findTemplate { my $s = shift; my $sname = $s->{"$s->{type}:template"}; my $def = $api->{index}->{"$s->{type}:"}; - my $name = $sname ? $sname : api::optionValue('template', $s->{size} == 0 ? 'code:class=handle' : 'code:class=struct', $obj, $def); + my $name = $sname ? $sname : config::optionValue('template', $s->{size} == 0 ? 'code:class=handle' : 'code:class=struct', $obj, $def); print "template: $s->{name} -> $name\n" if ($api->{vars}->{verbose} > 1); return findTemplateName($api, $name); } -# find value of first option of the given name -# TBD: moved to config.pm -sub optionValue { - my $name = shift; - my $or = shift; - - #my $match = sub { $_ =~ m/^\Q$name\E=(.*)$/on ? $1 : undef }; - - #print "optionValue $name\n"; - foreach my $obj (@_) { - foreach my $opt (@{$obj->{options}}) { - #print "? $name $opt = ".($opt =~m/^\Q$name\E=(.*)$/)."\n"; - #print " = $opt\n" if ($opt =~m/^\Q$name\E=(.*)$/); - return $1 if ($opt =~m/^\Q$name\E=(.*)$/); - #my $x = $match->($opt); - #return $x if defined($x); - } - } - $or; -} - -# look for all matching options of the given name -# multiple objects are searched, the first one with -# the given parameter overrides the rest. -# name, object, object * -sub optionValues { - my $name = shift; - my $rx = qr/$name/; - my @list; - - foreach my $obj (@_) { - foreach my $opt (@{$obj->{options}}) { - push @list, $1 if ($opt =~m/^$rx=(.*)$/); - } - last if ($#list >= 0); - } - @list; -} - -# same as above but doesn't short-circuit -sub optionValuesAll { - my $name = shift; - my $rx = qr/$name/; - my @list; - - foreach my $obj (@_) { - foreach my $opt (@{$obj->{options}}) { - push @list, $1 if ($opt =~m/^$rx=(.*)$/); - } - } - @list; -} - -# find first occurance of a flag -# TBD: moved to config.pm -sub optionFlag { - my $name = shift; - - foreach my $obj (@_) { - foreach my $opt (@{$obj->{options}}) { - return 1 if ($opt eq $name); - } - } - undef; -} - sub findItem { my $s = shift; my $name = shift; @@ -433,7 +367,7 @@ sub findAllItems { } } - if (optionValue('default', undef, $obj, $api->{index}->{'struct:'}) eq 'all') { + if (config::optionValue('default', undef, $obj, $api->{index}->{'struct:'}) eq 'all') { #print "* add all items\n"; foreach my $d (@all) { next if $visited{$d->{type}.':'.$d->{name}}++; @@ -524,7 +458,8 @@ sub findDependencies { print "Root types\n"; foreach my $obj (@{$api->{api}}) { - if ($obj->{type} eq 'library') { + # Objects that can include others + if ($obj->{type} =~ m/^(library|struct|union)$/) { foreach my $inc (@{$obj->{items}}) { next if ($inc->{type} eq 'library'); @@ -539,7 +474,9 @@ sub findDependencies { addDependencies($api, $obj, $s, $setdeps); } } - } elsif ($obj->{type} =~ m/^(struct|union|call|func|enum|define)$/) { + } + # Add all matching objects + if ($obj->{type} =~ m/^(struct|union|call|func|enum|define)$/) { #foreach my $n (grep { $_ =~ m/$obj->{regex}/ } keys %data) { print "? $obj->{name}\n"; @@ -808,21 +745,21 @@ sub processField { print " $m->{name}\n" if ($api->{vars}->{verbose} > 1); foreach my $flag (@itemFlags) { - my $value = optionFlag("$flag", $inc); - $value = optionFlag("$flag:$m->{name}", $obj, $def) if !defined($value); + my $value = config::optionFlag("$flag", $inc); + $value = config::optionFlag("$flag:$m->{name}", $obj, $def) if !defined($value); $m->{$flag} = $value if (defined($value)); $m->{select}->{$flag} = 1 if (defined($value)); } foreach my $option (@itemOptions) { - my $value = optionValue("$option", undef, $inc); - $value = optionValue("$option:$m->{name}", undef, $obj, $def) if !defined($value); + my $value = config::optionValue("$option", undef, $inc); + $value = config::optionValue("$option:$m->{name}", undef, $obj, $def) if !defined($value); $m->{$option} = $value if (defined($value)); $m->{select}->{$option} = 1 if (defined($value)); } foreach my $option (@itemAnyOptions) { - my $value = optionValue("$option", undef, $inc); - $value = optionValue("$option:$m->{name}", undef, $obj, $def) if !defined($value); - $value = optionValue("$option", undef, $obj, $def) if !defined($value); + my $value = config::optionValue("$option", undef, $inc); + $value = config::optionValue("$option:$m->{name}", undef, $obj, $def) if !defined($value); + $value = config::optionValue("$option", undef, $obj, $def) if !defined($value); $m->{$option} = $value if (defined($value)); } @@ -851,8 +788,8 @@ sub processTypeFunc { } $s->{rename} = (first { defined $_ } $obj->{"$s->{type}:rename"}, $renameTable{identity})->($s->{name}, $s); - $s->{access} = optionValue('access', '', $obj, $def); - $v = optionValue('onsuccess', undef, $obj, $def); + $s->{access} = config::optionValue('access', '', $obj, $def); + $v = config::optionValue('onsuccess', undef, $obj, $def); $s->{onsuccess} = $v if defined $v; postProcessType($s); @@ -880,24 +817,24 @@ sub processFunc { #print "[$index] [$lindex] $m->{name}\n"; foreach my $flag (@itemFlags) { - my $value = optionFlag("$flag:$m->{name}", $inc, $obj, $def); - $value = optionFlag("$flag:$index", $inc, $obj, $def) if !defined($value); - $value = optionFlag("$flag:$lindex", $inc, $obj, $def) if !defined($value); + my $value = config::optionFlag("$flag:$m->{name}", $inc, $obj, $def); + $value = config::optionFlag("$flag:$index", $inc, $obj, $def) if !defined($value); + $value = config::optionFlag("$flag:$lindex", $inc, $obj, $def) if !defined($value); $m->{$flag} = $value if (defined($value)); $m->{select}->{$flag} = 1 if (defined($value)); } foreach my $option (@itemOptions) { - my $value = optionValue("$option:$m->{name}", undef, $inc, $obj, $def); - $value = optionValue("$option:$index", undef, $inc, $obj, $def) if !defined($value); - $value = optionValue("$option:$lindex", undef, $inc, $obj, $def) if !defined($value); + my $value = config::optionValue("$option:$m->{name}", undef, $inc, $obj, $def); + $value = config::optionValue("$option:$index", undef, $inc, $obj, $def) if !defined($value); + $value = config::optionValue("$option:$lindex", undef, $inc, $obj, $def) if !defined($value); $m->{$option} = $value if (defined($value)); $m->{select}->{$option} = 1 if (defined($value)); } foreach my $option (@itemAnyOptions) { - my $value = optionValue("$option:$m->{name}", undef, $inc, $obj, $def); - $value = optionValue("$option:$index", undef, $inc, $obj, $def) if !defined($value); - $value = optionValue("$option:$lindex", undef, $inc, $obj, $def) if !defined($value); - $value = optionValue("$option", undef, $inc, $obj, $def) if !defined($value); + my $value = config::optionValue("$option:$m->{name}", undef, $inc, $obj, $def); + $value = config::optionValue("$option:$index", undef, $inc, $obj, $def) if !defined($value); + $value = config::optionValue("$option:$lindex", undef, $inc, $obj, $def) if !defined($value); + $value = config::optionValue("$option", undef, $inc, $obj, $def) if !defined($value); $m->{$option} = $value if (defined($value)); } $m->{output} = 1; @@ -906,8 +843,8 @@ sub processFunc { } $s->{rename} = (first { defined $_ } $inc->{"$s->{type}:rename"}, $obj->{"$s->{type}:rename"}, $renameTable{identity})->($s->{name}, $s); - $s->{access} = optionValue('access', '', $inc, $obj, $def); - $v = optionValue('onsuccess', undef, $inc, $obj, $def); + $s->{access} = config::optionValue('access', '', $inc, $obj, $def); + $v = config::optionValue('onsuccess', undef, $inc, $obj, $def); $s->{onsuccess} = $v if defined ($v); postProcessType($s); @@ -994,7 +931,7 @@ sub processType { processFields($api, $memberseen, $obj, $inc, $s, @list); } - if (optionValue('default', undef, $obj, $def) eq 'all') { + if (config::optionValue('default', undef, $obj, $def) eq 'all') { print " + adding all fields\n" if ($api->{vars}->{verbose} > 1); processFields($api, $memberseen, $obj, undef, $s, @{$s->{items}}); } @@ -1059,8 +996,8 @@ sub processLibrary { } } - if (defined optionValue('import', undef, $lib)) { - my @x = map { split /,/,$_ } optionValues('import', $lib); + if (defined config::optionValue('import', undef, $lib)) { + my @x = map { split /,/,$_ } config::optionValues('import', $lib); $lib->{imports} = \@x; } } diff --git a/src/notzed.nativez/lib/code.pm b/src/notzed.nativez/lib/code.pm index 9383602..9adb04a 100644 --- a/src/notzed.nativez/lib/code.pm +++ b/src/notzed.nativez/lib/code.pm @@ -7,7 +7,8 @@ use File::Basename; use Data::Dumper; use List::Util qw(first); -require api; +use api; +use config; my %typeSizes = ( i8 => 'byte', u8 => 'byte', @@ -270,7 +271,7 @@ sub applyTemplate { #print "template: $code->{match}\n"; - foreach my $set (api::optionValuesAll('set', $code)) { + foreach my $set (config::optionValuesAll('set', $code)) { $vars->{$1} = $2 if ($set =~ m/^(\w+)=(.*)/); } diff --git a/src/notzed.nativez/lib/config.pm b/src/notzed.nativez/lib/config.pm index 21a3973..3180a92 100644 --- a/src/notzed.nativez/lib/config.pm +++ b/src/notzed.nativez/lib/config.pm @@ -67,6 +67,38 @@ sub optionValue { $or; } +# look for all matching options of the given name +# multiple objects are searched, the first one with +# the given parameter overrides the rest. +# name, object, object * +sub optionValues { + my $name = shift; + my $rx = qr/$name/; + my @list; + + foreach my $obj (@_) { + foreach my $opt (@{$obj->{options}}) { + push @list, $1 if ($opt =~m/^$rx=(.*)$/); + } + last if ($#list >= 0); + } + @list; +} + +# same as above but doesn't short-circuit +sub optionValuesAll { + my $name = shift; + my $rx = qr/$name/; + my @list; + + foreach my $obj (@_) { + foreach my $opt (@{$obj->{options}}) { + push @list, $1 if ($opt =~m/^$rx=(.*)$/); + } + } + @list; +} + sub findInclude { my $self = shift; my $file = shift; -- 2.39.2