Merge branch 'master' of ssh://hrunkner.hjp.at//home/hjp/wrk/rpn
Conflicts: rpn resolved trivial conflicts.
This commit is contained in:
commit
0f0027d73c
11
rpn
11
rpn
|
@ -3,14 +3,16 @@ use warnings;
|
|||
use strict;
|
||||
use Term::ANSIColor;
|
||||
use Math::Trig ':pi';
|
||||
use Scalar::Util qw(looks_like_number);
|
||||
|
||||
my @stack;
|
||||
my $input_color = 'red';
|
||||
my $stack_color = 'blue';
|
||||
my $format;
|
||||
print color $input_color;
|
||||
while (<>) {
|
||||
for (split) {
|
||||
if (m{^[-+*/]|\*\*$}) {
|
||||
if (m{^([-+*/%]|\*\*)$}) {
|
||||
my $y = pop @stack;
|
||||
my $x = pop @stack;
|
||||
my $z = eval "$x $_ $y";
|
||||
|
@ -47,13 +49,20 @@ while (<>) {
|
|||
} elsif (m{^0[0-7]+$}) {
|
||||
no warnings 'portable';
|
||||
push @stack, oct($_);
|
||||
} elsif (m{^format$}) {
|
||||
$format = pop @stack;
|
||||
} else {
|
||||
push @stack, $_;
|
||||
}
|
||||
}
|
||||
for (@stack) {
|
||||
print color $stack_color;
|
||||
if ($format && looks_like_number($_)) {
|
||||
printf($format, $_);
|
||||
print "\n";
|
||||
} else {
|
||||
print "$_\n";
|
||||
}
|
||||
}
|
||||
print color $input_color;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue