xref: /netbsd-src/external/lgpl3/gmp/dist/demos/expr/run-expr.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* Demo program to run expression evaluation.
2 
3 Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
4 
5 This file is part of the GNU MP Library.
6 
7 The GNU MP Library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The GNU MP Library is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15 License for more details.
16 
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19 
20 
21 /* Usage: ./run-expr [-z] [-q] [-f] [-p prec] [-b base] expression...
22 
23    Evaluate each argument as a simple expression.  By default this is in mpz
24    integers, but -q selects mpq or -f selects mpf.  For mpf the float
25    precision can be set with -p.  In all cases the input base can be set
26    with -b, or the default is "0" meaning decimal with "0x" allowed.
27 
28    This is a pretty trivial program, it's just an easy way to experiment
29    with the evaluation functions.  */
30 
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 #include "gmp.h"
36 #include "expr.h"
37 
38 
39 void
40 run_expr (int type, int base, unsigned long prec, char *str)
41 {
42   int  outbase = (base == 0 ? 10 : base);
43   int  ret;
44 
45   switch (type) {
46   case 'z':
47   default:
48     {
49       mpz_t  res, var_a, var_b;
50 
51       mpz_init (res);
52       mpz_init_set_ui (var_a, 55L);
53       mpz_init_set_ui (var_b, 99L);
54 
55       ret = mpz_expr (res, base, str, var_a, var_b, NULL);
56       printf ("\"%s\" base %d: ", str, base);
57       if (ret == MPEXPR_RESULT_OK)
58         {
59           printf ("result ");
60           mpz_out_str (stdout, outbase, res);
61           printf ("\n");
62         }
63       else
64         printf ("invalid (return code %d)\n", ret);
65 
66       mpz_clear (res);
67       mpz_clear (var_a);
68       mpz_clear (var_b);
69     }
70     break;
71 
72   case 'q':
73     {
74       mpq_t  res, var_a, var_b;
75 
76       mpq_init (res);
77       mpq_init (var_a);
78       mpq_init (var_b);
79 
80       mpq_set_ui (var_a, 55L, 1);
81       mpq_set_ui (var_b, 99L, 1);
82 
83       ret = mpq_expr (res, base, str, var_a, var_b, NULL);
84       printf ("\"%s\" base %d: ", str, base);
85       if (ret == MPEXPR_RESULT_OK)
86         {
87           printf ("result ");
88           mpq_out_str (stdout, outbase, res);
89           printf ("\n");
90         }
91       else
92         printf ("invalid (return code %d)\n", ret);
93 
94       mpq_clear (res);
95       mpq_clear (var_a);
96       mpq_clear (var_b);
97     }
98     break;
99 
100   case 'f':
101     {
102       mpf_t  res, var_a, var_b;
103 
104       mpf_init2 (res, prec);
105       mpf_init_set_ui (var_a, 55L);
106       mpf_init_set_ui (var_b, 99L);
107 
108       ret = mpf_expr (res, base, str, var_a, var_b, NULL);
109       printf ("\"%s\" base %d: ", str, base);
110       if (ret == MPEXPR_RESULT_OK)
111         {
112           printf ("result ");
113           mpf_out_str (stdout, outbase, (size_t) 0, res);
114           printf ("\n");
115         }
116       else
117         printf ("invalid (return code %d)\n", ret);
118 
119       mpf_clear (res);
120       mpf_clear (var_a);
121       mpf_clear (var_b);
122     }
123     break;
124   }
125 }
126 
127 int
128 main (int argc, char *argv[])
129 {
130   int            type = 'z';
131   int            base = 0;
132   unsigned long  prec = 64;
133   int            seen_expr = 0;
134   int            opt;
135   char           *arg;
136 
137   for (;;)
138     {
139       argv++;
140       arg = argv[0];
141       if (arg == NULL)
142         break;
143 
144       if (arg[0] == '-')
145         {
146           for (;;)
147             {
148               arg++;
149               opt = arg[0];
150 
151               switch (opt) {
152               case '\0':
153                 goto end_opt;
154 
155               case 'f':
156               case 'q':
157               case 'z':
158                 type = opt;
159                 break;
160 
161               case 'b':
162                 arg++;
163                 if (arg[0] == '\0')
164                   {
165                     argv++;
166                     arg = argv[0];
167                     if (arg == NULL)
168                       {
169                       need_arg:
170                         fprintf (stderr, "Need argument for -%c\n", opt);
171                         exit (1);
172                       }
173                   }
174                 base = atoi (arg);
175                 goto end_opt;
176 
177               case 'p':
178                 arg++;
179                 if (arg[0] == '\0')
180                   {
181                     argv++;
182                     arg = argv[0];
183                     if (arg == NULL)
184                       goto need_arg;
185                   }
186                 prec = atoi (arg);
187                 goto end_opt;
188 
189               case '-':
190                 arg++;
191                 if (arg[0] != '\0')
192                   {
193                     /* no "--foo" options */
194                     fprintf (stderr, "Unrecognised option --%s\n", arg);
195                     exit (1);
196                   }
197                 /* stop option interpretation at "--" */
198                 for (;;)
199                   {
200                     argv++;
201                     arg = argv[0];
202                     if (arg == NULL)
203                       goto done;
204                     run_expr (type, base, prec, arg);
205                     seen_expr = 1;
206                   }
207 
208               default:
209                 fprintf (stderr, "Unrecognised option -%c\n", opt);
210                 exit (1);
211               }
212             }
213         end_opt:
214           ;
215         }
216       else
217         {
218           run_expr (type, base, prec, arg);
219           seen_expr = 1;
220         }
221     }
222 
223  done:
224   if (! seen_expr)
225     {
226       printf ("Usage: %s [-z] [-q] [-f] [-p prec] [-b base] expression...\n", argv[0]);
227       exit (1);
228     }
229 
230   return 0;
231 }
232