added constants support

This commit is contained in:
Thomas von Dein
2019-02-20 08:17:43 +01:00
parent 481d751444
commit 7e57dc179e

47
rpnc
View File

@@ -12,7 +12,7 @@ my $term = Term::ReadLine->new('rpn calc');
my $debug = 0; my $debug = 0;
my $showstack = 1; my $showstack = 1;
my $tty = 1; my $tty = 1;
my $VERSION = '1.03'; my $VERSION = '1.04';
my $sub = 0; my $sub = 0;
my $op; my $op;
@@ -54,7 +54,7 @@ if ($op) {
exit; exit;
} }
# management commands # management commands, always lower case letters or words
my %commands = ( my %commands = (
q => sub { exit; }, q => sub { exit; },
'?' => sub { help(); }, '?' => sub { help(); },
@@ -63,10 +63,10 @@ my %commands = (
c => sub { clearstack(); }, c => sub { clearstack(); },
cx => sub { clearstack(1); dumpstack(); }, cx => sub { clearstack(1); dumpstack(); },
d => sub { $debug ^= 1; }, d => sub { $debug ^= 1; },
S => sub { $showstack ^= 1; }, sd => sub { $showstack ^= 1; },
u => sub { undo(); dumpstack(); }, u => sub { undo(); dumpstack(); },
rs => sub { reversestack(); }, sr => sub { reversestack(); },
Rs => sub { rotatestack(); }, st => sub { rotatestack(); },
h => sub { showhist(); }, h => sub { showhist(); },
'(' => sub { $sub = 1 }, '(' => sub { $sub = 1 },
')' => sub { $sub = 0 }, ')' => sub { $sub = 0 },
@@ -100,7 +100,7 @@ my %func = (
return 0; return 0;
} }
}, },
'V' => sub { 'v' => sub {
if (scalar @_ == 2) { if (scalar @_ == 2) {
my ($a, $b) = @_; my ($a, $b) = @_;
return "$a ** (1 / $b)"; return "$a ** (1 / $b)";
@@ -139,10 +139,15 @@ my %func = (
} }
); );
# math constants, always upper case letters, usable via eval{}
use constant PI => 3.141592653589793;
use constant V2 => 1.414213562373095;
use constant V3 => 1.732050807568877;
my $OUT = $term->OUT || \*STDOUT; my $OUT = $term->OUT || \*STDOUT;
while ( defined ($_ = $term->readline(prompt())) ) { while ( defined ($_ = $term->readline(prompt())) ) {
foreach my $tok (split /\s+/) { foreach my $tok (split /\s+/) {
if ($tok =~ /^-?[\.\d]+?$/) { if ($tok =~ /^-?[A-Z\.\d]+?$/) {
# number # number
pushstack($tok); pushstack($tok);
dumpstack(); dumpstack();
@@ -395,20 +400,20 @@ otherwise runs interactively.
Configure: Available operators: Configure: Available operators:
d toggle debugging (-d) ( enter collect mode d toggle debugging (-d) ( enter collect mode
S toggle display of stack (-n) ) leave collect mode sd toggle display of stack (-n) ) leave collect mode
+ add + add
Stack Management: - substract Stack Management: - substract
s show the stack / divide s show the stack / divide
sa show the whole stack * multiply sa show the whole stack * multiply
cx clear X (last stack element) ^ expotentiate cx clear X (last stack element) ^ expotentiate
c clear stack % percent c clear stack % percent
rs reverse the stack %d percentual difference sr reverse the stack %d percentual difference
Rs rotate the stack & bitwise AND st rotate the stack & bitwise AND
| bitwise OR | bitwise OR
Various Commands x bitwise XOR Various Commands x bitwise XOR
u undo last operation V pull root (2nd if stack==1) u undo last operation v pull root (2nd if stack==1)
h show history of past operations m median h show history of past operations m median
q finish (C-d works as well) a average q finish (C-d works as well) a average
? print help ? print help
~; ~;
} }