forked from dbb/scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpn_calc
executable file
·74 lines (56 loc) · 1.69 KB
/
rpn_calc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env perl
use 5.010;
# https://github.com/dbb
for my $arg ( @ARGV ) {
&help if $arg =~ /\-h/;
unless ( $arg =~ /\s/ ) {
die "Terms must be space-separated";
}
my $tmp;
my $orig = $arg;
while ( $arg =~ m{[+*/-]} ) {
my $expr = $arg;
if (
## 12 3 4 5 6
$arg =~
s{((-?(0[bx])?[\d_a-f]+)\s+(-?(0[bx])?[\d_a-f]+)\s+([+*/-]))}{PAT}i
)
{
$tmp = eval "$2 $6 $4";
$expr =~ s/PAT/$tmp/;
$arg =~ s/PAT/$tmp/;
print "$expr\t= $arg\n" if $arg =~ m{[+*/-]};
} ## end if ( $arg =~ ...)
else {
die "Bad regex.";
}
} ## end while ( $arg =~ m{[+*/-]})
print "$orig\t= $arg\n\n";
} ## end for my $arg ( @ARGV )
sub help {
print << 'EOF';
Usage:
% rpn_calc 'expr1' 'expr2'
Where any exprN is an arithmetic expression in Reverse Polish Notation.
The output is always in base 10, but input can be binary (0b...),
decimal, hexadecimal (0x...), or octal (0...). Valid operators are addition
(+), subtraction (-), multiplication (*), and division (/). Numbers and
operators must be space separated. An underscore may be used to make large
numbers more readable (e.g. 1_000_000) but commas may not.
Running rpn_calc with the option -h or --help will display this help text and
exit.
Examples:
% rpn_calc '0b010 0b100 +'
0b010 0b100 + = 6
% rpn_calc '0xaa 0x4f /'
0xaa 0x4f / = 2.15189873417722
% rpn_calc '1 2 + 3 +'
1 2 + 3 + = 3 3 +
1 2 + 3 + = 6
% rpn_calc '1 1 /' '2 1 -'
1 1 / = 1
2 1 - = 1
Report all issues at: https://github.com/dbb
EOF
exit;
} ## end sub help