Skip to content

Instantly share code, notes, and snippets.

@jmdugan
Last active December 27, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmdugan/7305550 to your computer and use it in GitHub Desktop.
Save jmdugan/7305550 to your computer and use it in GitHub Desktop.
Command line "calc" supporting line references, simple variables, and PERL functions
#!/usr/bin/perl
##################################################################
# CONSOLE CALC
#
# simple console calcualtor supporting line references
# and simple, one-character variables.
#
# Copyright (c) 2001, 2002 all rights reserved.
# Jonathan Dugan
#
# LICENSE:
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
#
# Updated 11/2013
#
# (PS wrote this more than a decade ago, and has some terrible PERL in it.)
use Term::ReadLine;
#use strict;
sub printhelp {
print <<EOF;
This is a console mode calulator written in Perl. Simple
math expressions and Perl functions are available to use.
Valid functions include: `abs`, `atan2`, `cos`, `exp`,
`hex`, `int`, `log`, `oct`, `rand`, `sin`, `sqrt`,
`system`, `time`, etc etc
Results from previous lines are referenced with the "\\" character
followed by the line number.
Single lowercase letters are translated into local variables - you can
both write to them and read from them.
Up and down arrows move you though the command history (both typed
lines and lines translated by references to other lines are saved)
Examples:
\\4 is translated into the result of line 4.
\\ is the result of the last line
t=6 assigns 6 to variable \$t
r=\\ assigns r as the last calculated value.
please send comments and updates to Jonathan Dugan
dugan\@biocontact.org
EOF
;
}
# MAIN
if ($ARGV[0]){if ($ARGV[0]=~ /help|-h/) {printhelp();exit(1);}}
if ($ARGV[0]) {
my $in = join("",@ARGV);
my $out = eval($in);
if ($out) {print $out, "\n";} else {print "Error: $in\n"; exit(2)}
exit (0);
}
package ConsoleCalc;
my $term = new Term::ReadLine 'Simple Perl Calc';
my $i = 1;
my $prompt = "$i>";
my $OUT = $term->OUT || \*STDOUT;
my @rlist;
my $res;
my $l_in;
while ( defined ($l_in = $term->readline($prompt)) ) {
if ($l_in eq "." || $l_in eq "q" || $l_in eq "Q"){exit 0;}
if ($l_in =~ /help|-h/) {&printhelp;last;}
my $repl = ($l_in =~ /\\(\d*)/);
while ($repl) {
# print "reference to line $1, ",$rlist[$1-1],"\n";
my $ln = $1;
if ($ln){
if ($ln > $i-1){
print "abort: Line reference $ln too large!\n";$l_in="";last;
}
} else {
$ln = 0;
}
$l_in =~ s/\\$1/$rlist[$ln-1]/e;
$repl = ($l_in =~ /\\(\d*)/);
#DEBUG print " I: $l_in\n" unless $repl;
}
my $hasvar = ($l_in =~ /(?<![a-z\$])([a-df-z])(?![a-z])/);
while ($hasvar) {
my $var = $1;
if (! defined $ {$var}){
my $create = "our ConsoleCalc::\$$1;\n";
eval $create; # creates small cap variable
print "Defined \$$var\n";
}
$l_in =~ s/$var/\$$var/g; # substitute $t for t
$hasvar = ($l_in =~ /(?<![a-z\$])([a-df-z])(?![a-z])/); # ck
}
#DEBUG print " V: $l_in\n" unless $hasvar;
#process line
if (length($l_in)){
if ($l_in eq "." || $l_in eq "q" || $l_in eq "Q"){exit 0;}
$res = eval($l_in);
warn $@ if $@;
if ($@) {
$l_in = "";
} else {
print $OUT " =", $res , "\n";
if ($l_in =~ /\S/){
$term->addhistory($l_in);
$i++;$prompt = "$i>";
push (@rlist,$res);
}
}
}
}
# The original version of this calculator came from an
# example / FAQ on the WWW. I added line references and
# simple, one-character variables. I don\'t know the
# original author, but will gladly give them credit if
# there is an eariler version of this available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment