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