[PATCH 3/5] scripts/kconfig.pl: switch to using function references/hash for operators

Elliott Mitchell ehem+openwrt at m5p.com
Thu Nov 30 15:33:21 PST 2023


Ah, the wonders of pointers.  Simplifies the configuration parsing
as all these cases are otherwise identical.

Signed-off-by: Elliott Mitchell <ehem+openwrt at m5p.com>
---
Oy vey.  I only spotted passing the second arg to parse_expr() *just*
before I was initially planning to send this.  That is quite the boody
trap lurking there.
---
 scripts/kconfig.pl | 35 ++++++++++++-----------------------
 1 file changed, 12 insertions(+), 23 deletions(-)

diff --git a/scripts/kconfig.pl b/scripts/kconfig.pl
index dd286479b3..5a53e2154b 100755
--- a/scripts/kconfig.pl
+++ b/scripts/kconfig.pl
@@ -130,32 +130,21 @@ sub parse_expr {
 	my $mod_plus = shift;
 	my $arg = $arg[$$pos++];
 
+	my %ops = (
+		'&'	=> [\&config_and, undef],
+		'+'	=> [\&config_add, 0],
+		'm+'	=> [\&config_add, 1],
+		'>'	=> [\&config_diff, 0],
+		'>+'	=> [\&config_diff, 1],
+		'-'	=> [\&config_sub, undef],
+	);
+
 	die "Parse error" if (!$arg);
 
-	if ($arg eq '&') {
-		my $arg1 = parse_expr($pos);
-		my $arg2 = parse_expr($pos);
-		return config_and($arg1, $arg2, undef);
-	} elsif ($arg =~ /^\+/) {
-		my $arg1 = parse_expr($pos);
-		my $arg2 = parse_expr($pos);
-		return config_add($arg1, $arg2, 0);
-	} elsif ($arg =~ /^m\+/) {
-		my $arg1 = parse_expr($pos);
-		my $arg2 = parse_expr($pos, 1);
-		return config_add($arg1, $arg2, 1);
-	} elsif ($arg eq '>') {
-		my $arg1 = parse_expr($pos);
-		my $arg2 = parse_expr($pos);
-		return config_diff($arg1, $arg2, 0);
-	} elsif ($arg eq '>+') {
-		my $arg1 = parse_expr($pos);
-		my $arg2 = parse_expr($pos);
-		return config_diff($arg1, $arg2, 1);
-	} elsif ($arg eq '-') {
+	if (exists($ops{$arg})) {
 		my $arg1 = parse_expr($pos);
-		my $arg2 = parse_expr($pos);
-		return config_sub($arg1, $arg2, undef);
+		my $arg2 = parse_expr($pos, ($arg eq 'm+') ? 1 : 0);
+		return &{$ops{$arg}->[0]}($arg1, $arg2, $ops{$arg}->[1]);
 	} else {
 		return load_config($arg, $mod_plus);
 	}
-- 
(\___(\___(\______          --=> 8-) EHM <=--          ______/)___/)___/)
 \BS (    |       ehem+openwrt at m5p.com     PGP 87145445       |    )   /
  \_CS\   |  _____  -O #include <stddisclaimer.h> O-   _____  |   /  _/
8A19\___\_|_/58D2 7E3D DDF4 7BA6 <-PGP-> 41D1 B375 37D0 8714\_|_/___/5445






More information about the openwrt-devel mailing list