2 * prompt - eemonstration of some uses of prompt() and eval()
4 * Copyright (C) 1999 Ernest Bowen
6 * Calc is open software; you can redistribute it and/or modify it under
7 * the terms of the version 2.1 of the GNU Lesser General Public License
8 * as published by the Free Software Foundation.
10 * Calc is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
13 * Public License for more details.
15 * A copy of version 2.1 of the GNU Lesser General Public License is
16 * distributed with calc under the filename COPYING-LGPL. You should have
17 * received a copy with calc; if not, write to Free Software Foundation, Inc.
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * @(#) $Revision: 30.1 $
21 * @(#) $Id: prompt.cal,v 30.1 2007/03/16 11:09:54 chongo Exp $
22 * @(#) $Source: /usr/local/src/bin/calc/cal/RCS/prompt.cal,v $
24 * Under source code control: 1995/12/18 04:43:25
25 * File existed as early as: 1995
27 * Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
31 * Demonstration of some uses of prompt() and eval().
33 * adder() simulates a simple adding machine: starting with sum = 0,
34 * each number entered in response to the ? prompt is added to sum
35 * and the result displayed. Operation of adder() is ended by
36 * entering "end", "exit" or "quit"; "end" returns to the level from
37 * which adder() is called, e.g. with:
41 * entering "end" would start a new edition with sum = 0; "quit" and
42 * "exit" return to the top level.
44 * Each response to ? is read as
45 * a string terminated by a newline; the statements and expressions
46 * in this string are compiled and evaluated as in function evaluation;
47 * thus the string may include variables, assignments, functions, etc.
53 * local x = 2; while (x < 100) x *= 2; x % 100
60 * (Here the second line creates x as a global variable; the local
61 * variable x in the fourth line has no effect on the global x. In
62 * the last three lines, sum is the sum of numbers already entered, so
63 * the third last line doubles the value of sum. The value returned
64 * by "print sum^2;" is the null value, so the second last line adds
65 * nothing to sum. The last line returns the value 3, i.e. the last
66 * non-null value found for the expressions separated by semicolons,
67 * so sum will be increased by 3 after the "print sum^2;" command
68 * is executed. xxx The terminating semicolon is essential in the
69 * last two lines. A command like eval("print 7;") is acceptable to
70 * calc but eval("print 7") causes an exit from calc. xxx)
72 * If the value returned is not a number (e.g. the name of a list or matrix,
73 * or if the string has syntax errors as in "2 + ", in which case the
74 * value returned is an error value), the compile error messages and a
75 * request for another number are displayed.
77 * Calling showvalues(str) assumes str defines a function of x as in:
79 * "sin(x)", "x^2 + 3*x", "exp(x, 1e-5)".
81 * Values of the function so defined are returned for values of x
82 * entered in reponse to the ? prompt. Operation is terminated by
83 * entering "end", "exit" or "quit".
96 print "Please enter a number";
106 define showvalues(str) {
113 if (!isnum(prompt_x)) {
114 print "Please enter a number";
117 print "\t":eval(str);