mirror of
https://codeberg.org/scip/rpnc.git
synced 2025-12-17 04:21:01 +01:00
added perl function support
This commit is contained in:
16
README.md
16
README.md
@@ -156,10 +156,26 @@ function name:
|
|||||||
470
|
470
|
||||||
220
|
220
|
||||||
res2vcc
|
res2vcc
|
||||||
|
=> 2.79
|
||||||
|
|
||||||
You can also put the function definition in the config file
|
You can also put the function definition in the config file
|
||||||
`~/.rpnc`. Empty lines and lines beginning with `#` will be ignored.
|
`~/.rpnc`. Empty lines and lines beginning with `#` will be ignored.
|
||||||
|
|
||||||
|
Another way to define a function is to use perl code directly. The
|
||||||
|
perl code must be a closure string and surrounded by braces. You can
|
||||||
|
access the stack via `@_`. Here's an example:
|
||||||
|
|
||||||
|
f pr { return "1.0 / (" . join(' + ', map { "1.0 / $_"} @_) . ")" }
|
||||||
|
|
||||||
|
This function calculates the parallel resistance of a number of
|
||||||
|
resistors. It adds up all values from the stack. Usage:
|
||||||
|
|
||||||
|
22
|
||||||
|
47
|
||||||
|
330
|
||||||
|
pr
|
||||||
|
=> 41.14
|
||||||
|
|
||||||
|
|
||||||
## Using STDIN via a PIPE
|
## Using STDIN via a PIPE
|
||||||
|
|
||||||
|
|||||||
54
rpnc
54
rpnc
@@ -157,12 +157,6 @@ my %func = (
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
'pr' => sub {
|
|
||||||
# parallel resistance, maybe add ~/.rpncrc support
|
|
||||||
# where to add such custom functions...
|
|
||||||
return "1 / (" . join(' + ', map { "1 / $_"} @_) . ")";
|
|
||||||
},
|
|
||||||
|
|
||||||
'm' => sub {
|
'm' => sub {
|
||||||
# median
|
# median
|
||||||
if (scalar @_ >= 2) {
|
if (scalar @_ >= 2) {
|
||||||
@@ -571,32 +565,39 @@ sub defun {
|
|||||||
|
|
||||||
$custom{$name} = "@tokens";
|
$custom{$name} = "@tokens";
|
||||||
|
|
||||||
$func{$name} = sub {
|
if ($custom{$name} =~ /^\{.*\}$/) {
|
||||||
my $max = scalar @_;
|
# perl code
|
||||||
my @args = reverse(@_);
|
$func{$name} = sub { return eval "@tokens" };
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
# rpnc code
|
||||||
|
$func{$name} = sub {
|
||||||
|
my $max = scalar @_;
|
||||||
|
my @args = reverse(@_);
|
||||||
|
|
||||||
# replace N1..NN with actual args
|
# replace N1..NN with actual stack items
|
||||||
my @body;
|
my @body;
|
||||||
foreach my $item (@tokens) {
|
foreach my $item (@tokens) {
|
||||||
if ($item =~ /^([A-Z])(\d+)$/) {
|
if ($item =~ /^([A-Z])(\d+)$/) {
|
||||||
my $letter = $1;
|
my $letter = $1;
|
||||||
my $i = $2;
|
my $i = $2;
|
||||||
if ($i <= $max) {
|
if ($i <= $max) {
|
||||||
push @body, $args[$i-1];
|
push @body, $args[$i-1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
print "undefined variable ${letter}${i}!\n";
|
||||||
|
push @body, 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
print "undefined variable ${letter}${i}!\n";
|
push @body, $item;
|
||||||
push @body, 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
push @body, $item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# execute @body
|
# execute @body
|
||||||
looptokenize("@body");
|
looptokenize("@body");
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
print "function $name() defined.\n" unless $silent;
|
print "function $name() defined.\n" unless $silent;
|
||||||
}
|
}
|
||||||
@@ -646,5 +647,6 @@ Various Commands: Functions:
|
|||||||
h show history of past operations fs show list of defined functions
|
h show history of past operations fs show list of defined functions
|
||||||
q finish (C-d works as well) Using register: enter R + index, e.g. R1
|
q finish (C-d works as well) Using register: enter R + index, e.g. R1
|
||||||
? print help Constants: PI V2 V3
|
? print help Constants: PI V2 V3
|
||||||
|
|
||||||
~;
|
~;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user