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 __gmp_const union mpX_t *mpX_srcptr; 57 58 typedef void (*mpexpr_fun_one_t) __GMP_PROTO ((mpX_ptr)); 59 typedef unsigned long (*mpexpr_fun_ui_one_t) __GMP_PROTO ((mpX_ptr)); 60 61 typedef void (*mpexpr_fun_0ary_t) __GMP_PROTO ((mpX_ptr)); 62 typedef int (*mpexpr_fun_i_0ary_t) __GMP_PROTO ((void)); 63 64 typedef void (*mpexpr_fun_unary_t) __GMP_PROTO ((mpX_ptr, mpX_srcptr)); 65 typedef void (*mpexpr_fun_unary_ui_t) __GMP_PROTO ((mpX_ptr, unsigned long)); 66 typedef int (*mpexpr_fun_i_unary_t) __GMP_PROTO ((mpX_srcptr)); 67 typedef int (*mpexpr_fun_i_unary_ui_t) __GMP_PROTO ((unsigned long)); 68 69 typedef void (*mpexpr_fun_binary_t) __GMP_PROTO ((mpX_ptr, mpX_srcptr, mpX_srcptr)); 70 typedef void (*mpexpr_fun_binary_ui_t) __GMP_PROTO ((mpX_ptr, mpX_srcptr, unsigned long)); 71 typedef int (*mpexpr_fun_i_binary_t) __GMP_PROTO ((mpX_srcptr, mpX_srcptr)); 72 typedef int (*mpexpr_fun_i_binary_ui_t) __GMP_PROTO ((mpX_srcptr, unsigned long)); 73 74 typedef void (*mpexpr_fun_ternary_t) 75 __GMP_PROTO ((mpX_ptr, mpX_srcptr, mpX_srcptr, mpX_srcptr)); 76 typedef void (*mpexpr_fun_ternary_ui_t) 77 __GMP_PROTO ((mpX_ptr, mpX_srcptr, mpX_srcptr, unsigned long)); 78 typedef int (*mpexpr_fun_i_ternary_t) 79 __GMP_PROTO ((mpX_srcptr, mpX_srcptr, mpX_srcptr)); 80 typedef int (*mpexpr_fun_i_ternary_ui_t) 81 __GMP_PROTO ((mpX_srcptr, mpX_srcptr, unsigned long)); 82 83 typedef size_t (*mpexpr_fun_number_t) 84 __GMP_PROTO ((mpX_ptr, __gmp_const char *str, size_t len, int base)); 85 typedef void (*mpexpr_fun_swap_t) __GMP_PROTO ((mpX_ptr, mpX_ptr)); 86 typedef unsigned long (*mpexpr_fun_get_ui_t) __GMP_PROTO ((mpX_srcptr)); 87 typedef void (*mpexpr_fun_set_si_t) __GMP_PROTO ((mpX_srcptr, long)); 88 89 struct mpexpr_control_t { 90 __gmp_const struct mpexpr_operator_t *op; 91 int argcount; 92 }; 93 94 #define MPEXPR_VARIABLES 26 95 96 struct mpexpr_parse_t { 97 __gmp_const struct mpexpr_operator_t *table; 98 99 mpX_ptr res; 100 int base; 101 unsigned long prec; 102 __gmp_const char *e; 103 size_t elen; 104 mpX_srcptr *var; 105 int error_code; 106 107 int token; 108 __gmp_const struct mpexpr_operator_t *token_op; 109 110 union mpX_t *data_stack; 111 int data_top; 112 int data_alloc; 113 int data_inited; 114 115 struct mpexpr_control_t *control_stack; 116 int control_top; 117 int control_alloc; 118 119 120 mpexpr_fun_0ary_t mpX_clear; 121 mpexpr_fun_i_unary_t mpX_ulong_p; 122 mpexpr_fun_get_ui_t mpX_get_ui; 123 mpexpr_fun_unary_ui_t mpX_init; 124 mpexpr_fun_number_t mpX_number; 125 mpexpr_fun_unary_t mpX_set; 126 mpexpr_fun_unary_t mpX_set_or_swap; 127 mpexpr_fun_set_si_t mpX_set_si; 128 mpexpr_fun_swap_t mpX_swap; 129 }; 130 131 132 int mpexpr_evaluate __GMP_PROTO ((struct mpexpr_parse_t *p)); 133 int mpexpr_va_to_var __GMP_PROTO ((void *var[], va_list ap)); 134 size_t mpexpr_mpz_number __GMP_PROTO ((mpz_ptr res, 135 __gmp_const char *e, size_t elen, int base)); 136