diff mbox series

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

Message ID 038cf8705f41ab960d573ab96f73afecfbdae9f4.1701746457.git.ehem+openwrt@m5p.com
State New
Headers show
Series Scripting tweaks | expand

Commit Message

Elliott Mitchell Nov. 30, 2023, 11:33 p.m. UTC
Ah, the wonders of pointers.  Simplifies the configuration parsing
as all these cases are otherwise identical.

Signed-off-by: Elliott Mitchell <ehem+openwrt@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 mbox series

Patch

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);
 	}