60 lines
1.3 KiB
Perl
Executable File
60 lines
1.3 KiB
Perl
Executable File
#!/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);
|
|
} elsif (m{^0x[0-9a-fA-F]+$}) {
|
|
no warnings 'portable';
|
|
push @stack, hex($_);
|
|
} elsif (m{^0[0-7]+$}) {
|
|
no warnings 'portable';
|
|
push @stack, oct($_);
|
|
} else {
|
|
push @stack, $_;
|
|
}
|
|
}
|
|
for (@stack) {
|
|
print color $stack_color;
|
|
print "$_\n";
|
|
}
|
|
print color $input_color;
|
|
}
|