1 /* mpz 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 #include <ctype.h> 21 #include <stdio.h> 22 #include <string.h> 23 #include "gmp.h" 24 #include "expr-impl.h" 25 26 27 /* No need to parse '-' since that's handled as an operator. 28 This function also by mpq_expr_a, so it's not static. */ 29 size_t 30 mpexpr_mpz_number (mpz_ptr res, const char *e, size_t elen, int base) 31 { 32 char *edup; 33 size_t i, ret; 34 int base_effective = (base == 0 ? 10 : base); 35 void *(*allocate_func) (size_t); 36 void (*free_func) (void *, size_t); 37 38 i = 0; 39 if (e[i] == '0') 40 { 41 i++; 42 if (e[i] == 'x' || e[i] == 'b') 43 i++; 44 } 45 46 for ( ; i < elen; i++) 47 if (! isasciidigit_in_base (e[i], base_effective)) 48 break; 49 50 mp_get_memory_functions (&allocate_func, NULL, &free_func); 51 edup = (*allocate_func) (i+1); 52 memcpy (edup, e, i); 53 edup[i] = '\0'; 54 55 if (mpz_set_str (res, edup, base) == 0) 56 ret = i; 57 else 58 ret = 0; 59 60 (*free_func) (edup, i+1); 61 return ret; 62 } 63 64 /* ignoring prec */ 65 static void 66 e_mpz_init (mpz_ptr z, unsigned long prec) 67 { 68 mpz_init (z); 69 } 70 71 int 72 mpz_expr_a (const struct mpexpr_operator_t *table, 73 mpz_ptr res, int base, 74 const char *e, size_t elen, 75 mpz_srcptr var[26]) 76 { 77 struct mpexpr_parse_t p; 78 79 p.table = table; 80 p.res = (mpX_ptr) res; 81 p.base = base; 82 p.e = e; 83 p.elen = elen; 84 p.var = (mpX_srcptr *) var; 85 86 p.mpX_clear = (mpexpr_fun_one_t) mpz_clear; 87 p.mpX_ulong_p = (mpexpr_fun_i_unary_t) mpz_fits_ulong_p; 88 p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpz_get_ui; 89 p.mpX_init = (mpexpr_fun_unary_ui_t) e_mpz_init; 90 p.mpX_number = (mpexpr_fun_number_t) mpexpr_mpz_number; 91 p.mpX_set = (mpexpr_fun_unary_t) mpz_set; 92 p.mpX_set_or_swap = (mpexpr_fun_unary_t) mpz_swap; 93 p.mpX_set_si = (mpexpr_fun_set_si_t) mpz_set_si; 94 p.mpX_swap = (mpexpr_fun_swap_t) mpz_swap; 95 96 return mpexpr_evaluate (&p); 97 } 98