Austin Schuh | dace2a6 | 2020-08-18 10:56:48 -0700 | [diff] [blame^] | 1 | /* mpz expression evaluation |
| 2 | |
| 3 | Copyright 2000-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 either: |
| 9 | |
| 10 | * the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 3 of the License, or (at your |
| 12 | option) any later version. |
| 13 | |
| 14 | or |
| 15 | |
| 16 | * the GNU General Public License as published by the Free Software |
| 17 | Foundation; either version 2 of the License, or (at your option) any |
| 18 | later version. |
| 19 | |
| 20 | or both in parallel, as here. |
| 21 | |
| 22 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 23 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 24 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 25 | for more details. |
| 26 | |
| 27 | You should have received copies of the GNU General Public License and the |
| 28 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 29 | see https://www.gnu.org/licenses/. */ |
| 30 | |
| 31 | #include <ctype.h> |
| 32 | #include <stdio.h> |
| 33 | #include <string.h> |
| 34 | #include "gmp.h" |
| 35 | #include "expr-impl.h" |
| 36 | |
| 37 | |
| 38 | /* No need to parse '-' since that's handled as an operator. |
| 39 | This function also by mpq_expr_a, so it's not static. */ |
| 40 | size_t |
| 41 | mpexpr_mpz_number (mpz_ptr res, const char *e, size_t elen, int base) |
| 42 | { |
| 43 | char *edup; |
| 44 | size_t i, ret; |
| 45 | int base_effective = (base == 0 ? 10 : base); |
| 46 | void *(*allocate_func) (size_t); |
| 47 | void (*free_func) (void *, size_t); |
| 48 | |
| 49 | i = 0; |
| 50 | if (e[i] == '0') |
| 51 | { |
| 52 | i++; |
| 53 | if (e[i] == 'x' || e[i] == 'b') |
| 54 | i++; |
| 55 | } |
| 56 | |
| 57 | for ( ; i < elen; i++) |
| 58 | if (! isasciidigit_in_base (e[i], base_effective)) |
| 59 | break; |
| 60 | |
| 61 | mp_get_memory_functions (&allocate_func, NULL, &free_func); |
| 62 | edup = (*allocate_func) (i+1); |
| 63 | memcpy (edup, e, i); |
| 64 | edup[i] = '\0'; |
| 65 | |
| 66 | if (mpz_set_str (res, edup, base) == 0) |
| 67 | ret = i; |
| 68 | else |
| 69 | ret = 0; |
| 70 | |
| 71 | (*free_func) (edup, i+1); |
| 72 | return ret; |
| 73 | } |
| 74 | |
| 75 | /* ignoring prec */ |
| 76 | static void |
| 77 | e_mpz_init (mpz_ptr z, unsigned long prec) |
| 78 | { |
| 79 | mpz_init (z); |
| 80 | } |
| 81 | |
| 82 | int |
| 83 | mpz_expr_a (const struct mpexpr_operator_t *table, |
| 84 | mpz_ptr res, int base, |
| 85 | const char *e, size_t elen, |
| 86 | mpz_srcptr var[26]) |
| 87 | { |
| 88 | struct mpexpr_parse_t p; |
| 89 | |
| 90 | p.table = table; |
| 91 | p.res = (mpX_ptr) res; |
| 92 | p.base = base; |
| 93 | p.e = e; |
| 94 | p.elen = elen; |
| 95 | p.var = (mpX_srcptr *) var; |
| 96 | |
| 97 | p.mpX_clear = (mpexpr_fun_one_t) mpz_clear; |
| 98 | p.mpX_ulong_p = (mpexpr_fun_i_unary_t) mpz_fits_ulong_p; |
| 99 | p.mpX_get_ui = (mpexpr_fun_get_ui_t) mpz_get_ui; |
| 100 | p.mpX_init = (mpexpr_fun_unary_ui_t) e_mpz_init; |
| 101 | p.mpX_number = (mpexpr_fun_number_t) mpexpr_mpz_number; |
| 102 | p.mpX_set = (mpexpr_fun_unary_t) mpz_set; |
| 103 | p.mpX_set_or_swap = (mpexpr_fun_unary_t) mpz_swap; |
| 104 | p.mpX_set_si = (mpexpr_fun_set_si_t) mpz_set_si; |
| 105 | p.mpX_swap = (mpexpr_fun_swap_t) mpz_swap; |
| 106 | |
| 107 | return mpexpr_evaluate (&p); |
| 108 | } |