blib/lib/App/optex/rpn.pm

filter: Statementstmt 40.2% 33 / 82 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 50.0% 8 / 16 Podpod 14.2% 1 / 7 totaltotal 24.2% 42 / 173 CC 28 SCAR 40.9
CC
28
Cov
24%
rpn_pattern
156.0
rpn
56.0
convert
30.0
CRAP 59.8
↑ Summary
1 package App::optex::rpn;
2  
3 1 our $VERSION = "1.04";
4  
5 =encoding utf-8
6  
7 =head1 NAME
8  
9 rpn - optex module for Reverse Polish Notation calculation
10  
11 =head1 SYNOPSIS
12  
13 optex -Mrpn command ...
14  
15 =head1 DESCRIPTION
16  
17 B<rpn> is a module for the B<optex> command that detects arguments
18 that look like Reverse Polish Notation (RPN) expressions and replaces
19 them with their calculated results.
20  
21 By default, all arguments are processed automatically when the module
22 is loaded.
23  
24 =head1 MODULE OPTIONS
25  
26 Module options can be set via C<-Mrpn::config(...)> or C<--option>
27 before C<-->.
28  
29 =over 4
30  
31 =item B<--auto>, B<--no-auto>
32  
33 Enable or disable automatic processing of all arguments. Default is
34 enabled. Use C<--no-auto> to disable and process only arguments
35 specified by C<--rpn>.
36  
37 =item B<-p> I<name_or_regex>, B<--pattern> I<name_or_regex>
38  
39 Specify a pattern to match RPN expressions. The value can be either a
40 preset name (word characters only, prefix match supported) or a
41 custom regex pattern.
42  
43 When C<--pattern> is specified, C<--auto> is ignored.
44  
45 B<Preset patterns:>
46  
47 =over 4
48  
49 =item C<rpn>
50  
51 Matches C<rpn(...)> and extracts the content inside parentheses.
52  
53 =item C<equal>
54  
55 Matches C<...=> at the end and extracts the expression before C<=>.
56  
57 =back
58  
59 B<Custom patterns:>
60  
61 When the value contains non-word characters, it is treated as a
62 regex pattern. The pattern must contain a capture group C<(...)> that
63 captures the RPN expression. The entire matched portion is replaced
64 with the calculated result.
65  
66 Examples:
67  
68 # Use preset pattern 'rpn' (or -pr for short)
69 optex -Mrpn -pr -- echo '3600*5' '=' rpn(3600,5*)
70 # outputs: 3600*5 = 18000
71  
72 # Use preset pattern 'equal' (or -pe for short)
73 optex -Mrpn -pe -- echo '3600*5' '=' 3600,5*=
74 # outputs: 3600*5 = 18000
75  
76 # Use custom regex pattern
77 optex -Mrpn --pattern 'calc\[(.*)\]' -- echo calc[3600,5*]
78 # outputs: 18000
79  
80 =item B<--quiet>, B<--no-quiet>
81  
82 Suppress Math::RPN warning messages. Default is enabled. Use
83 C<--no-quiet> to see warnings for invalid expressions.
84  
85 =item B<--verbose>
86  
87 Print diagnostic messages.
88  
89 =back
90  
91 =head1 COMMAND OPTIONS
92  
93 These options are available after C<-->.
94  
95 =over 4
96  
97 =item B<--rpn> I<expression>
98  
99 Convert a single RPN expression.
100  
101 optex -Mrpn --no-auto -- printf '%s = %d\n' 3600,5* --rpn 3600,5*
102 # outputs: 3600,5* = 18000
103  
104 =back
105  
106 =head1 EXPRESSIONS
107  
108 An RPN expression requires at least two terms separated by commas or
109 colons. A single term like C<RAND> will not be converted, but
110 C<RAND,0+> will produce a random number.
111  
112 =head2 OPERATORS
113  
114 The following operators are supported (case-insensitive):
115  
116 =over 4
117  
118 =item Arithmetic
119  
120 C<+> (ADD), C<-> (SUB), C<*> (MUL), C</> (DIV), C<%> (MOD),
121 C<++> (INCR), C<--> (DECR), C<POW>, C<SQRT>
122  
123 =item Trigonometric
124  
125 C<SIN>, C<COS>, C<TAN>
126  
127 =item Logarithmic
128  
129 C<LOG>, C<EXP>
130  
131 =item Numeric
132  
133 C<ABS>, C<INT>
134  
135 =item Bitwise/Logical
136  
137 C<&> (AND), C<|> (OR), C<!> (NOT), C<XOR>, C<~>
138  
139 =item Comparison
140  
141 C<E<lt>> (LT), C<E<lt>=> (LE), C<=>/C<==> (EQ),
142 C<E<gt>> (GT), C<E<gt>=> (GE), C<!=> (NE)
143  
144 =item Conditional
145  
146 C<IF>
147  
148 =item Stack
149  
150 C<DUP>, C<EXCH>, C<POP>
151  
152 =item Other
153  
154 C<MIN>, C<MAX>, C<TIME>, C<RAND>, C<LRAND>
155  
156 =back
157  
158 See L<Math::RPN> for detailed descriptions of these operators.
159  
160 =head1 EXAMPLES
161  
162 Convert 5 hours to seconds (3600 * 5 = 18000):
163  
164 $ optex -Mrpn echo 3600,5*
165 18000
166  
167 Prevent macOS from sleeping for 5 hours:
168  
169 $ optex -Mrpn caffeinate -d -t 3600,5*
170  
171 Process multiple expressions:
172  
173 $ optex -Mrpn echo 1,2+ 10,3*
174 3 30
175  
176 Generate a random number:
177  
178 $ optex -Mrpn echo RAND,0+
179 0.316809834520431
180  
181 =head1 INSTALLATION
182  
183 =head2 CPANMINUS
184  
185 cpanm App::optex::rpn
186  
187 =head1 SEE ALSO
188  
189 L<App::optex>, L<https://github.com/kaz-utashiro/optex>
190  
191 L<App::optex::rpn>, L<https://github.com/kaz-utashiro/optex-rpn>
192  
193 L<Math::RPN>
194  
195 L<https://qiita.com/kaz-utashiro/items/2df8c7fbd2fcb880cee6>
196  
197 =head1 AUTHOR
198  
199 Kazumasa Utashiro
200  
201 =head1 LICENSE
202  
203 Copyright 2021-2025 Kazumasa Utashiro.
204  
205 This library is free software; you can redistribute it and/or modify
206 it under the same terms as Perl itself.
207  
208 =cut
209  
210 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
211 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
214 1 ▶use open IO => 'utf8', ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 1 ▶use Getopt::EX::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
217 1 my $config = Getopt::EX::Config->new(
218 auto => 1,
219 verbose => 0,
220 quiet => 1,
221 pattern => undef,
222 );
223  
224 1 my($mod, $argv);
225 0 ▶sub initialize { ($mod, $argv) = @_ }
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
226  
227 0 ▶sub finalize {
Subroutine finalize: not called CC=3 SCAR=24.8
Pod undocumented
228 0 $config->deal_with($argv, 'auto!', 'verbose!', 'quiet!', 'pattern|p=s');
229 0 ▶ if (defined $config->{pattern}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $config->{'pattern'}) { } 0 0 0
elsif ($config->{'auto'}) { } 0 0 0
230 0 rpn_pattern($config->{pattern});
231 } elsif ($config->{auto}) {
232 0 rpn();
233 }
234 }
235  
236 0 ▶sub argv (&) {
Subroutine argv: not called CC=1 SCAR=6.9
Pod undocumented
237 0 my $sub = shift;
238 0 @$argv = $sub->(@$argv);
239 }
240  
241 1 my @operator = sort { length $b <=> length $a } split /[,\s]+/, <<'END';
242 +,ADD ++,INCR -,SUB --,DECR *,MUL /,DIV %,MOD POW SQRT
243 SIN COS TAN
244 LOG EXP
245 ABS INT
246 &,AND |,OR !,NOT XOR ~
247 <,LT <=,LE =,==,EQ >,GT >=,GE !=,NE
248 IF
249 DUP EXCH POP
250 MIN MAX
251 TIME
252 RAND LRAND
253 END
254  
255 1 my $operator_re = join '|', map "\Q$_", @operator;
256 1 my $term_re = qr/(?:\d*\.)?\d+|$operator_re/i;
257 1 my $rpn_re = qr/(?: $term_re [,:]* ){2,}/xi;
258  
259 1 my %preset_pattern = (
260 rpn => qr/^rpn\((.+)\)$/,
261 equal => qr/^(.+)=$/,
262 );
263  
264 0 ▶sub rpn_calc {
Subroutine rpn_calc: not called CC=5 SCAR=30.5
Pod undocumented
265 1 ▶ use Math::RPN ();
Subroutine BEGIN: called CC=1 SCAR=0.0
266 0 my @terms = map { /$term_re/g } @_;
267 0 my @ans = do {
268 0 local $_;
269 0 ▶ local $SIG{__WARN__} = $config->{quiet} ? sub {} : undef;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$config->{'quiet'} ? : 0 0 0
270 0 Math::RPN::rpn @terms;
271 };
272 0 ▶ if (@ans == 1 && defined $ans[0] && $ans[0] !~ /[^\.\d]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ans == 1 and defined $ans[0] and not $ans[0] =~ /[^\.\d]/u) { } 0 0 0
Condition0%0/6
@ans == 1 and defined $ans[0] and not $ans[0] =~ /[^\.\d]/u
!ll&&!rl&&r
000
@ans == 1 and defined $ans[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@ans == 1 and defined $ans[0] and not $ans[0] =~ /[^\.\d]/u
@ans == 1 defined $ans[0] not $ans[0] =~ /[^\.\d]/u
Truth table0%0/4
A and B and not(C)
A= @ans == 1B= defined $ans[0]C= not $ans[0] =~ /[^\.\d]/u
ABCresult
0XX0
10X0
1100
1111
273 0 $ans[0];
274 } else {
275 0 return undef;
276 }
277 }
278  
279  
280 0 ▶sub rpn {
Subroutine rpn: not called CC=7 SCAR=40.3
Pod documented
281 0 my $count = 0;
282 0 for (@$argv) {
283 0 ▶ /^$rpn_re$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/^$rpn_re$/u or next 0 0 0
284 0 ▶ my $calc = rpn_calc($_) // next;
Condition0%0/2
rpn_calc($_) // (next)
l!l
00
MC/DC0%0/1
rpn_calc($_) // (next)
rpn_calc($_)
Truth table0%0/2
A
A= rpn_calc($_)
Aresult
11
00
285 0 ▶ if ($calc ne $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calc ne $_) 0 0 0
286 0 $count++;
287 0 $_ = $calc;
288 }
289 }
290 0 ▶ warn "rpn: converted $count expression(s)\n" if $config->{verbose} && $count;
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"verbose"} and $count 0 0 0
Condition0%0/3
$config->{"verbose"} and $count
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"verbose"} and $count
$config->{"verbose"} $count
Truth table0%0/3
A and B
A= $config->{"verbose"}B= $count
ABresult
0X0
100
111
291 }
292  
293 0 ▶sub rpn_pattern {
Subroutine rpn_pattern: not called CC=12 SCAR=50.5
Pod undocumented
294 0 my $pattern = shift;
295 0 my $re;
296 0 ▶ if ($pattern =~ /^\w+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^\w+$/u) { } 0 0 0
297 0 my @matches = grep { /^\Q$pattern/ } keys %preset_pattern;
298 0 ▶ @matches == 1 or die @matches == 0
Branch: 2 branches
BranchTrueFalseTotal
@matches == 1 or die @matches == 0 ? "rpn: unknown preset pattern '${pattern}'\n" : "rpn: ambiguous preset pattern '${pattern}' (matches: @matches)\n" 0 0 0
@matches == 0 ? : 0 0 0
299 ? "rpn: unknown preset pattern '$pattern'\n"
300 : "rpn: ambiguous preset pattern '$pattern' (matches: @matches)\n";
301 0 $re = $preset_pattern{$matches[0]};
302 } else {
303 0 $re = qr/$pattern/;
304 }
305 0 my $count = 0;
306 0 for (@$argv) {
307 0 ▶ /$re/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/$re/u or next 0 0 0
308 0 ▶ my $expr = $1 // next;
Condition0%0/2
$1 // (next)
l!l
00
MC/DC0%0/1
$1 // (next)
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
309 0 ▶ length $expr or next;
Branch: 1 branch
BranchTrueFalseTotal
length $expr or next 0 0 0
310 0 ▶ my $calc = rpn_calc($expr) // next;
Condition0%0/2
rpn_calc($expr) // (next)
l!l
00
MC/DC0%0/1
rpn_calc($expr) // (next)
rpn_calc($expr)
Truth table0%0/2
A
A= rpn_calc($expr)
Aresult
11
00
311 0 ▶ if ($calc ne $expr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($calc ne $expr) 0 0 0
312 0 $count++;
313 0 s/$re/$calc/;
314 }
315 }
316 0 ▶ warn "rpn: converted $count expression(s)\n" if $config->{verbose} && $count;
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"verbose"} and $count 0 0 0
Condition0%0/3
$config->{"verbose"} and $count
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"verbose"} and $count
$config->{"verbose"} $count
Truth table0%0/3
A and B
A= $config->{"verbose"}B= $count
ABresult
0X0
100
111
317 }
318  
319 0 ▶sub convert {
Subroutine convert: not called CC=5 SCAR=34.0
Pod undocumented
320 0 my %arg = @_;
321 0 ▶ my $target = $arg{rpn} // die "rpn: missing expression\n";
Condition0%0/2
$arg{"rpn"} // die("rpn: missing expression\n")
l!l
00
MC/DC0%0/1
$arg{"rpn"} // die("rpn: missing expression\n")
$arg{"rpn"}
Truth table0%0/2
A
A= $arg{"rpn"}
Aresult
11
00
322 0 ▶ if ($target =~ /^$rpn_re$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /^$rpn_re$/u) 0 0 0
323 0 my $calc = rpn_calc($target);
324 0 ▶ if (defined $calc && $calc ne $target) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $calc and $calc ne $target) 0 0 0
Condition0%0/3
defined $calc and $calc ne $target
!ll&&!rl&&r
000
MC/DC0%0/2
defined $calc and $calc ne $target
defined $calc $calc ne $target
Truth table0%0/3
A and B
A= defined $calcB= $calc ne $target
ABresult
0X0
100
111
325 0 $target = $calc;
326 }
327 }
328 0 unshift @$argv, $target;
329 }
330  
331 1 1;
332  
333 __DATA__
↑ Summary