Files
calc/cal/prompt.cal
2017-05-21 15:38:38 -07:00

120 lines
3.7 KiB
Plaintext

/*
* prompt - eemonstration of some uses of prompt() and eval()
*
* Copyright (C) 1999 Ernest Bowen
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* @(#) $Revision: 29.2 $
* @(#) $Id: prompt.cal,v 29.2 2000/06/07 14:02:25 chongo Exp $
* @(#) $Source: /usr/local/src/cmd/calc/cal/RCS/prompt.cal,v $
*
* Under source code control: 1995/12/18 04:43:25
* File existed as early as: 1995
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
/*
* Demonstration of some uses of prompt() and eval().
*
* adder() simulates a simple adding machine: starting with sum = 0,
* each number entered in response to the ? prompt is added to sum
* and the result displayed. Operation of adder() is ended by
* entering "end", "exit" or "quit"; "end" returns to the level from
* which adder() is called, e.g. with:
*
* for (;;) adder()
*
* entering "end" would start a new edition with sum = 0; "quit" and
* "exit" return to the top level.
*
* Each response to ? is read as
* a string terminated by a newline; the statements and expressions
* in this string are compiled and evaluated as in function evaluation;
* thus the string may include variables, assignments, functions, etc.
* as in:
*
* 2 + 3
* x = 2 + 3, x^3
* x^2
* local x = 2; while (x < 100) x *= 2; x % 100
* x
* exp(2, 1e-5)
* sum
* print sum^2;
* 3; print sum^2;
*
* (Here the second line creates x as a global variable; the local
* variable x in the fourth line has no effect on the global x. In
* the last three lines, sum is the sum of numbers already entered, so
* the third last line doubles the value of sum. The value returned
* by "print sum^2;" is the null value, so the second last line adds
* nothing to sum. The last line returns the value 3, i.e. the last
* non-null value found for the expressions separated by semicolons,
* so sum will be increased by 3 after the "print sum^2;" command
* is executed. xxx The terminating semicolon is essential in the
* last two lines. A command like eval("print 7;") is acceptable to
* calc but eval("print 7") causes an exit from calc. xxx)
*
* If the value returned is not a number (e.g. the name of a list or matrix,
* or if the string has syntax errors as in "2 + ", in which case the
* value returned is an error value), the compile error messages and a
* request for another number are displayed.
*
* Calling showvalues(str) assumes str defines a function of x as in:
*
* "sin(x)", "x^2 + 3*x", "exp(x, 1e-5)".
*
* Values of the function so defined are returned for values of x
* entered in reponse to the ? prompt. Operation is terminated by
* entering "end", "exit" or "quit".
*/
define adder() {
global sum = 0;
local s, t;
for (;;) {
s = prompt("? ");
if (s == "end")
break;
t = eval(s);
if (!isnum(t)) {
print "Please enter a number";
continue;
}
sum += t;
print "\t":sum;
}
}
global x;
define showvalues(str) {
local s;
for (;;) {
s = prompt("? ");
if (s == "end")
break;
x = eval(s);
if (!isnum(x)) {
print "Please enter a number";
continue;
}
print "\t":eval(str);
}
}