xref: /netbsd-src/external/lgpl3/gmp/dist/demos/expr/expr-impl.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /* Implementation specifics for 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 /* Same tests as gmp.h. */
22 #if  defined (__STDC__)                                 \
23   || defined (__cplusplus)                              \
24   || defined (_AIX)                                     \
25   || defined (__DECC)                                   \
26   || (defined (__mips) && defined (_SYSTYPE_SVR4))      \
27   || defined (_MSC_VER)                                 \
28   || defined (_WIN32)
29 #define HAVE_STDARG 1
30 #include <stdarg.h>
31 #else
32 #define HAVE_STDARG 0
33 #include <varargs.h>
34 #endif
35 
36 #include "expr.h"
37 
38 
39 #define isasciidigit(c)   (isascii (c) && isdigit (c))
40 #define isasciicsym(c)    (isascii (c) && (isalnum(c) || (c) == '_'))
41 
42 #define isasciidigit_in_base(c,base)                    \
43   (isascii (c)                                          \
44    && ((isdigit (c) && (c)-'0' < (base))                \
45        || (isupper (c) && (c)-'A'+10 < (base))          \
46        || (islower (c) && (c)-'a'+10 < (base))))
47 
48 
49 union mpX_t {
50   mpz_t   z;
51   mpq_t   q;
52   mpf_t   f;
53 };
54 
55 typedef union mpX_t *mpX_ptr;
56 typedef const union mpX_t *mpX_srcptr;
57 
58 typedef void (*mpexpr_fun_one_t) (mpX_ptr);
59 typedef unsigned long (*mpexpr_fun_ui_one_t) (mpX_ptr);
60 
61 typedef void (*mpexpr_fun_0ary_t) (mpX_ptr);
62 typedef int  (*mpexpr_fun_i_0ary_t) (void);
63 
64 typedef void (*mpexpr_fun_unary_t) (mpX_ptr, mpX_srcptr);
65 typedef void (*mpexpr_fun_unary_ui_t) (mpX_ptr, unsigned long);
66 typedef int  (*mpexpr_fun_i_unary_t) (mpX_srcptr);
67 typedef int  (*mpexpr_fun_i_unary_ui_t) (unsigned long);
68 
69 typedef void (*mpexpr_fun_binary_t) (mpX_ptr, mpX_srcptr, mpX_srcptr);
70 typedef void (*mpexpr_fun_binary_ui_t) (mpX_ptr, mpX_srcptr, unsigned long);
71 typedef int  (*mpexpr_fun_i_binary_t) (mpX_srcptr, mpX_srcptr);
72 typedef int  (*mpexpr_fun_i_binary_ui_t) (mpX_srcptr, unsigned long);
73 
74 typedef void (*mpexpr_fun_ternary_t) (mpX_ptr, mpX_srcptr, mpX_srcptr, mpX_srcptr);
75 typedef void (*mpexpr_fun_ternary_ui_t) (mpX_ptr, mpX_srcptr, mpX_srcptr, unsigned long);
76 typedef int (*mpexpr_fun_i_ternary_t) (mpX_srcptr, mpX_srcptr, mpX_srcptr);
77 typedef int (*mpexpr_fun_i_ternary_ui_t) (mpX_srcptr, mpX_srcptr, unsigned long);
78 
79 typedef size_t (*mpexpr_fun_number_t) (mpX_ptr, const char *str, size_t len, int base);
80 typedef void (*mpexpr_fun_swap_t) (mpX_ptr, mpX_ptr);
81 typedef unsigned long (*mpexpr_fun_get_ui_t) (mpX_srcptr);
82 typedef void (*mpexpr_fun_set_si_t) (mpX_srcptr, long);
83 
84 struct mpexpr_control_t {
85   const struct mpexpr_operator_t  *op;
86   int                             argcount;
87 };
88 
89 #define MPEXPR_VARIABLES  26
90 
91 struct mpexpr_parse_t {
92   const struct mpexpr_operator_t  *table;
93 
94   mpX_ptr                         res;
95   int                             base;
96   unsigned long                   prec;
97   const char                      *e;
98   size_t                          elen;
99   mpX_srcptr                      *var;
100   int                             error_code;
101 
102   int                             token;
103   const struct mpexpr_operator_t  *token_op;
104 
105   union mpX_t                     *data_stack;
106   int                             data_top;
107   int                             data_alloc;
108   int                             data_inited;
109 
110   struct mpexpr_control_t         *control_stack;
111   int                             control_top;
112   int                             control_alloc;
113 
114   mpexpr_fun_0ary_t               mpX_clear;
115   mpexpr_fun_i_unary_t            mpX_ulong_p;
116   mpexpr_fun_get_ui_t             mpX_get_ui;
117   mpexpr_fun_unary_ui_t           mpX_init;
118   mpexpr_fun_number_t             mpX_number;
119   mpexpr_fun_unary_t              mpX_set;
120   mpexpr_fun_unary_t              mpX_set_or_swap;
121   mpexpr_fun_set_si_t             mpX_set_si;
122   mpexpr_fun_swap_t               mpX_swap;
123 };
124 
125 
126 int mpexpr_evaluate (struct mpexpr_parse_t *p);
127 int mpexpr_va_to_var (void *var[], va_list ap);
128 size_t mpexpr_mpz_number (mpz_ptr res, const char *e, size_t elen, int base);
129