File: prompt.cal

package info (click to toggle)
apcalc 2.12.1.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,548 kB
  • ctags: 4,129
  • sloc: ansic: 53,374; makefile: 5,589; awk: 96; sed: 33; sh: 13
file content (119 lines) | stat: -rw-r--r-- 3,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * 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.3 $
 * @(#) $Id: prompt.cal,v 29.3 2006/06/20 09:29:16 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 prompt_x;

define showvalues(str) {
	local s;
	for (;;) {
		s = prompt("? ");
		if (s == "end")
			break;
		prompt_x = eval(s);
		if (!isnum(prompt_x)) {
			print "Please enter a number";
			continue;
		}
		print "\t":eval(str);
	}
}