additions:

toggle showstack
fixed collector display
better help
This commit is contained in:
Thomas von Dein
2019-02-19 19:50:09 +01:00
parent 49851b7815
commit 481d751444

59
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.03dev'; my $VERSION = '1.03';
my $sub = 0; my $sub = 0;
my $op; my $op;
@@ -59,9 +59,11 @@ my %commands = (
q => sub { exit; }, q => sub { exit; },
'?' => sub { help(); }, '?' => sub { help(); },
s => sub { dumpstack(); }, s => sub { dumpstack(); },
sa => sub { dumpstack(1); },
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; },
u => sub { undo(); dumpstack(); }, u => sub { undo(); dumpstack(); },
rs => sub { reversestack(); }, rs => sub { reversestack(); },
Rs => sub { rotatestack(); }, Rs => sub { rotatestack(); },
@@ -238,21 +240,30 @@ sub pushstack {
sub dumpstack { sub dumpstack {
return unless $showstack; return unless $showstack;
my $max = shift;
my $x = ' ';
my $prefix = 'stack'; my $prefix = 'stack';
my @all; my @all;
if ($sub) { if ($sub) {
@all = @substack; @all = @substack;
$prefix = 'collectorstack';
} }
else { else {
@all = @stack; @all = @stack;
} }
my $abs = scalar @all;
if (! $max && $abs > 10) {
@all = @all[-10 .. -1];
printf "%s [..]\n", $prefix;
}
my $p = scalar @all; my $p = scalar @all;
foreach my $n (@all) { foreach my $n (@all) {
printf "%s %4d: %s\n", $prefix, $p--, $n; $x = 'X' if($p == 1);
printf "%s %s %4d: %s\n", $prefix, $x, $p--, $n;
} }
print "\n"; print "\n";
@@ -382,34 +393,22 @@ Commandline: rpn [-d] [<operator>]
If <operator> is provided, read numbers from STDIN, If <operator> is provided, read numbers from STDIN,
otherwise runs interactively. otherwise runs interactively.
Available commands: Configure: Available operators:
cx clear X (last stack element) d toggle debugging (-d) ( enter collect mode
c clear stack S toggle display of stack (-n) ) leave collect mode
s show the stack
d toggle debugging (current setting: $debug)
rs reverse the stack
Rs rotate the stack
( enter collect mode
) leave collect mode
u undo last operation
h show history of past operations
q finish (C-d works as well)
? print help
Available operators:
+ add + add
- substract Stack Management: - substract
/ divide s show the stack / divide
* multiply sa show the whole stack * multiply
^ expotentiate cx clear X (last stack element) ^ expotentiate
% percent c clear stack % percent
%d percentual difference rs reverse the stack %d percentual difference
& bitwise AND Rs rotate the stack & bitwise AND
| bitwise OR | bitwise OR
x bitwise XOR Various Commands x bitwise XOR
V pull root (2nd if stack==1) u undo last operation V pull root (2nd if stack==1)
m median h show history of past operations m median
a average q finish (C-d works as well) a average
? print help
~; ~;
} }