fix stack pop order, added stack2collector mode, fixed bit funcs

This commit is contained in:
Thomas von Dein
2020-04-16 20:44:16 +02:00
parent 334297f645
commit a9b4272ce9

55
rpnc
View File

@@ -70,7 +70,7 @@ my %commands = (
srt => sub { rotatestack(); },
# collector
'(' => sub { $sub = 1 },
')' => sub { $sub = 0 },
')' => sub { stack2sub(); },
# register stuff
r => sub { last_to_reg(); dumpstack(); },
rcx => sub { clearreg(1); dumpstack(); },
@@ -178,29 +178,6 @@ my %func = (
}
},
'x' => sub {
# XOR
my ($a, $b) = getlast(2);
if (defined $b) {
return "$a ^ $b";
}
},
'|' => sub {
# OR
my ($a, $b) = getlast(2);
if (defined $b) {
return "$a | $b";
}
},
'|' => sub {
# XOR
my ($a, $b) = getlast(2);
if (defined $b) {
return "$a ^ $b";
}
},
# converters:
# gallons to liters
'tl' => sub { return convert("* 3.785") },
@@ -441,20 +418,13 @@ sub getlast {
if (scalar @stack == 1) {
if ($request > 1) {
print "At least $request variables must be on the stack!\n";
return 0;
}
else {
@all = pop @stack;
}
}
else {
if ($request == 1) {
@all = pop @stack;
}
else {
# return 2 elements, as we do not support more than 2 anyway
@all = reverse (pop @stack, pop @stack);
}
elsif (scalar @stack >= 2) {
@all = splice(@stack, -1 * $request, $request);
}
}
else {
@@ -507,6 +477,20 @@ sub clearreg {
}
}
sub stack2sub {
if (! $sub && scalar @substack == 0 && scalar @stack > 1) {
# not in collector mode, empty substack, move stack to substack, enter collect
backup();
@substack = @stack;
@stack = ();
$sub = 1;
}
else {
# leave collector mode
$sub = 0;
}
}
sub prompt {
my $count;
my $prompt;
@@ -654,7 +638,7 @@ otherwise runs interactively.
Configure: Available math operators:
td toggle debugging (-d) ( enter collect mode
ts toggle display of stack (-n) ) leave collect mode
ts toggle display of stack (-n) ) leave collect || stack => collect
+ add
Stack Management: - substract
s show the stack / divide
@@ -665,7 +649,8 @@ Stack Management: - substract
srt rotate the stack & bitwise AND
| bitwise OR
Register Management: x bitwise XOR
r put X to register v square root
r put X to register < > bitwise shift left or right
R1-9 push value of register to stack v square root
rcx clear X (last register element) m median
rc clear register a average