From ac9719ad57af37ad7f4a62dde8b864faad0d4e04 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Wed, 26 Sep 2012 16:17:39 +0200 Subject: [PATCH] Initial version --- rpn | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 rpn diff --git a/rpn b/rpn new file mode 100755 index 0000000..70606be --- /dev/null +++ b/rpn @@ -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; +}