Initial version

This commit is contained in:
Peter J. Holzer 2012-09-26 16:17:39 +02:00
commit ac9719ad57
1 changed files with 53 additions and 0 deletions

53
rpn Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/perl
use warnings;
use strict;
use Term::ANSIColor;
use Math::Trig ':pi';
my @stack;
my $input_color = 'red';
my $stack_color = 'blue';
print color $input_color;
while (<>) {
for (split) {
if (m{^[-+*/]|\*\*$}) {
my $y = pop @stack;
my $x = pop @stack;
my $z = eval "$x $_ $y";
push @stack, $z;
} elsif (m{^sqrt$}) {
my $x = pop @stack;
my $z = eval "$_($x)";
push @stack, $z;
} elsif (m{^pi$}) {
push @stack, pi;
} elsif (m{^swap$}) {
my $y = pop @stack;
my $x = pop @stack;
push @stack, $y;
push @stack, $x;
} elsif (m{^pop$}) {
pop @stack;
} elsif (m{^dup$}) {
my $x = pop @stack;
push @stack, $x;
push @stack, $x;
} elsif (m{^log$}) {
my $x = pop @stack;
push @stack, log($x) / log(10);
} elsif (m{^ln$}) {
my $x = pop @stack;
push @stack, log($x);
} elsif (m{^ld$}) {
my $x = pop @stack;
push @stack, log($x) / log(2);
} else {
push @stack, $_;
}
}
for (@stack) {
print color $stack_color;
print "$_\n";
}
print color $input_color;
}