Implemented format and fixed parse error.
This commit is contained in:
parent
15cf3dcafb
commit
4e77d495e7
13
rpn
13
rpn
|
@ -3,14 +3,16 @@ use warnings;
|
||||||
use strict;
|
use strict;
|
||||||
use Term::ANSIColor;
|
use Term::ANSIColor;
|
||||||
use Math::Trig ':pi';
|
use Math::Trig ':pi';
|
||||||
|
use Scalar::Util qw(looks_like_number);
|
||||||
|
|
||||||
my @stack;
|
my @stack;
|
||||||
my $input_color = 'red';
|
my $input_color = 'red';
|
||||||
my $stack_color = 'blue';
|
my $stack_color = 'blue';
|
||||||
|
my $format;
|
||||||
print color $input_color;
|
print color $input_color;
|
||||||
while (<>) {
|
while (<>) {
|
||||||
for (split) {
|
for (split) {
|
||||||
if (m{^[-+*/%]|\*\*$}) {
|
if (m{^([-+*/%]|\*\*)$}) {
|
||||||
my $y = pop @stack;
|
my $y = pop @stack;
|
||||||
my $x = pop @stack;
|
my $x = pop @stack;
|
||||||
my $z = eval "$x $_ $y";
|
my $z = eval "$x $_ $y";
|
||||||
|
@ -41,13 +43,20 @@ while (<>) {
|
||||||
} elsif (m{^ld$}) {
|
} elsif (m{^ld$}) {
|
||||||
my $x = pop @stack;
|
my $x = pop @stack;
|
||||||
push @stack, log($x) / log(2);
|
push @stack, log($x) / log(2);
|
||||||
|
} elsif (m{^format$}) {
|
||||||
|
$format = pop @stack;
|
||||||
} else {
|
} else {
|
||||||
push @stack, $_;
|
push @stack, $_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (@stack) {
|
for (@stack) {
|
||||||
print color $stack_color;
|
print color $stack_color;
|
||||||
print "$_\n";
|
if ($format && looks_like_number($_)) {
|
||||||
|
printf($format, $_);
|
||||||
|
print "\n";
|
||||||
|
} else {
|
||||||
|
print "$_\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
print color $input_color;
|
print color $input_color;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue