2
/* A simple integer desk calculator using yacc and gmp.
4
Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
6
This file is part of the GNU MP Library.
8
This program is free software; you can redistribute it and/or modify it under
9
the terms of the GNU General Public License as published by the Free Software
10
Foundation; either version 2 of the License, or (at your option) any later
13
This program is distributed in the hope that it will be useful, but WITHOUT ANY
14
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15
PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License along with
18
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19
Place - Suite 330, Boston, MA 02111-1307, USA. */
22
/* This is a simple program, meant only to show one way to use GMP for this
23
sort of thing. There's few features, and error checking is minimal.
24
Standard input is read, calc_help() below shows the inputs accepted.
26
Expressions are evaluated as they're read. If user defined functions
27
were wanted it'd be necessary to build a parse tree like pexpr.c does, or
28
a list of operations for a stack based evaluator. That would also make
29
it possible to detect and optimize evaluations "mod m" like pexpr.c does.
31
A stack is used for intermediate values in the expression evaluation,
32
separate from the yacc parser stack. This is simple, makes error
33
recovery easy, minimizes the junk around mpz calls in the rules, and
34
saves initializing or clearing "mpz_t"s during a calculation. A
35
disadvantage though is that variables must be copied to the stack to be
36
worked on. A more sophisticated calculator or language system might be
37
able to avoid that when executing a compiled or semi-compiled form.
39
Avoiding repeated initializing and clearing of "mpz_t"s is important. In
40
this program the time spent parsing is obviously much greater than any
41
possible saving from this, but a proper calculator or language should
42
take some trouble over it. Don't be surprised if an init/clear takes 3
43
or more times as long as a 10 limb addition, depending on the system (see
44
the mpz_init_realloc_clear example in tune/README). */
51
#define NO_CALC_H /* because it conflicts with normal calc.c stuff */
52
#include "calc-common.h"
55
#define numberof(x) (sizeof (x) / sizeof ((x)[0]))
61
printf ("Examples:\n");
62
printf (" 2+3*4 expressions are evaluated\n");
63
printf (" x=5^6 variables a to z can be set and used\n");
64
printf ("Operators:\n");
65
printf (" + - * arithmetic\n");
66
printf (" / %% division and remainder (rounding towards negative infinity)\n");
67
printf (" ^ exponentiation\n");
68
printf (" ! factorial\n");
69
printf (" << >> left and right shifts\n");
70
printf (" <= >= > \\ comparisons, giving 1 if true, 0 if false\n");
71
printf (" == != < /\n");
72
printf (" && || logical and/or, giving 1 if true, 0 if false\n");
73
printf ("Functions:\n");
74
printf (" abs(n) absolute value\n");
75
printf (" bin(n,m) binomial coefficient\n");
76
printf (" fib(n) fibonacci number\n");
77
printf (" gcd(a,b,..) greatest common divisor\n");
78
printf (" kron(a,b) kronecker symbol\n");
79
printf (" lcm(a,b,..) least common multiple\n");
80
printf (" lucnum(n) lucas number\n");
81
printf (" nextprime(n) next prime after n\n");
82
printf (" powm(b,e,m) modulo powering, b^e%%m\n");
83
printf (" root(n,r) r-th root\n");
84
printf (" sqrt(n) square root\n");
86
printf (" hex \\ set hex or decimal for input and output\n");
87
printf (" decimal / (\"0x\" can be used for hex too)\n");
88
printf (" quit exit program (EOF works too)\n");
89
printf (" ; statements are separated with a ; or newline\n");
90
printf (" \\ continue expressions with \\ before newline\n");
91
printf (" # xxx comments are # though to newline\n");
92
printf ("Hex numbers must be entered in upper case, to distinguish them from the\n");
93
printf ("variables a to f (like in bc).\n");
101
/* The stack is a fixed size, which means there's a limit on the nesting
102
allowed in expressions. A more sophisticated program could let it grow
106
mpz_ptr sp = stack[0];
108
#define CHECK_OVERFLOW() \
109
if (sp >= stack[numberof(stack)]) \
112
"Value stack overflow, too much nesting in expression\n"); \
116
#define CHECK_EMPTY() \
117
if (sp != stack[0]) \
119
fprintf (stderr, "Oops, expected the value stack to be empty\n"); \
126
#define CHECK_VARIABLE(var) \
127
if ((var) < 0 || (var) >= numberof (variable)) \
129
fprintf (stderr, "Oops, bad variable somehow: %d\n", var); \
134
#define CHECK_UI(name,z) \
135
if (! mpz_fits_ulong_p (z)) \
137
fprintf (stderr, "%s too big\n", name); \
149
%token HELP HEX DECIMAL QUIT
150
%token ABS BIN FIB GCD KRON LCM LUCNUM NEXTPRIME POWM ROOT SQRT
152
%token <var> VARIABLE
154
/* operators, increasing precedence */
157
%nonassoc '<' '>' EQ NE LE GE
169
| statements statement;
173
| statements statement EOS
174
| error EOS { sp = stack[0]; yyerrok; };
179
mpz_out_str (stdout, obase, sp); putchar ('\n');
185
mpz_swap (variable[$1], sp);
189
| HELP { calc_help (); }
190
| HEX { ibase = 16; obase = -16; }
191
| DECIMAL { ibase = 0; obase = 10; }
192
| QUIT { exit (0); };
194
/* "e" leaves it's value on the top of the mpz stack. A rule like "e '+' e"
195
will have done a reduction for the first "e" first and the second "e"
196
second, so the code receives the values in that order on the stack. */
198
'(' e ')' /* value on stack */
199
| e '+' e { sp--; mpz_add (sp, sp, sp+1); }
200
| e '-' e { sp--; mpz_sub (sp, sp, sp+1); }
201
| e '*' e { sp--; mpz_mul (sp, sp, sp+1); }
202
| e '/' e { sp--; mpz_fdiv_q (sp, sp, sp+1); }
203
| e '%' e { sp--; mpz_fdiv_r (sp, sp, sp+1); }
204
| e '^' e { CHECK_UI ("Exponent", sp);
205
sp--; mpz_pow_ui (sp, sp, mpz_get_ui (sp+1)); }
206
| e LSHIFT e { CHECK_UI ("Shift count", sp);
207
sp--; mpz_mul_2exp (sp, sp, mpz_get_ui (sp+1)); }
208
| e RSHIFT e { CHECK_UI ("Shift count", sp);
209
sp--; mpz_fdiv_q_2exp (sp, sp, mpz_get_ui (sp+1)); }
210
| e '!' { CHECK_UI ("Factorial", sp);
211
mpz_fac_ui (sp, mpz_get_ui (sp)); }
212
| '-' e %prec UMINUS { mpz_neg (sp, sp); }
214
| e '<' e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) < 0); }
215
| e LE e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) <= 0); }
216
| e EQ e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) == 0); }
217
| e NE e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) != 0); }
218
| e GE e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) >= 0); }
219
| e '>' e { sp--; mpz_set_ui (sp, mpz_cmp (sp, sp+1) > 0); }
221
| e LAND e { sp--; mpz_set_ui (sp, mpz_sgn (sp) && mpz_sgn (sp+1)); }
222
| e LOR e { sp--; mpz_set_ui (sp, mpz_sgn (sp) || mpz_sgn (sp+1)); }
224
| ABS '(' e ')' { mpz_abs (sp, sp); }
225
| BIN '(' e ',' e ')' { sp--; CHECK_UI ("Binomial base", sp+1);
226
mpz_bin_ui (sp, sp, mpz_get_ui (sp+1)); }
227
| FIB '(' e ')' { CHECK_UI ("Fibonacci", sp);
228
mpz_fib_ui (sp, mpz_get_ui (sp)); }
229
| GCD '(' gcdlist ')' /* value on stack */
230
| KRON '(' e ',' e ')' { sp--; mpz_set_si (sp,
231
mpz_kronecker (sp, sp+1)); }
232
| LCM '(' lcmlist ')' /* value on stack */
233
| LUCNUM '(' e ')' { CHECK_UI ("Lucas number", sp);
234
mpz_lucnum_ui (sp, mpz_get_ui (sp)); }
235
| NEXTPRIME '(' e ')' { mpz_nextprime (sp, sp); }
236
| POWM '(' e ',' e ',' e ')' { sp -= 2; mpz_powm (sp, sp, sp+1, sp+2); }
237
| ROOT '(' e ',' e ')' { sp--; CHECK_UI ("Nth-root", sp+1);
238
mpz_root (sp, sp, mpz_get_ui (sp+1)); }
239
| SQRT '(' e ')' { mpz_sqrt (sp, sp); }
245
mpz_set (sp, variable[$1]);
250
if (mpz_set_str (sp, $1, ibase) != 0)
252
fprintf (stderr, "Invalid number: %s\n", $1);
258
e /* value on stack */
259
| gcdlist ',' e { sp--; mpz_gcd (sp, sp, sp+1); };
262
e /* value on stack */
263
| lcmlist ',' e { sp--; mpz_lcm (sp, sp, sp+1); };
269
fprintf (stderr, "%s\n", s);
272
int calc_option_readline = -1;
275
main (int argc, char *argv[])
279
for (i = 1; i < argc; i++)
281
if (strcmp (argv[i], "--readline") == 0)
282
calc_option_readline = 1;
283
else if (strcmp (argv[i], "--noreadline") == 0)
284
calc_option_readline = 0;
285
else if (strcmp (argv[i], "--help") == 0)
287
printf ("Usage: calc [--option]...\n");
288
printf (" --readline use readline\n");
289
printf (" --noreadline don't use readline\n");
290
printf (" --help this message\n");
291
printf ("Readline is only available when compiled in,\n");
292
printf ("and in that case it's the default on a tty.\n");
297
fprintf (stderr, "Unrecognised option: %s\n", argv[i]);
303
calc_init_readline ();
305
if (calc_option_readline == 1)
307
fprintf (stderr, "Readline support not available\n");
312
for (i = 0; i < numberof (variable); i++)
313
mpz_init (variable[i]);
315
for (i = 0; i < numberof (stack); i++)