blob: 9b6458fbd08323080a1e205ba8fa712a332621ff [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* Implementation specifics for expression evaluation.
2
3Copyright 2000-2002, 2004 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it 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
14or
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
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library. If not,
29see 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
46union mpX_t {
47 mpz_t z;
48 mpq_t q;
49 mpf_t f;
50};
51
52typedef union mpX_t *mpX_ptr;
53typedef const union mpX_t *mpX_srcptr;
54
55typedef void (*mpexpr_fun_one_t) (mpX_ptr);
56typedef unsigned long (*mpexpr_fun_ui_one_t) (mpX_ptr);
57
58typedef void (*mpexpr_fun_0ary_t) (mpX_ptr);
59typedef int (*mpexpr_fun_i_0ary_t) (void);
60
61typedef void (*mpexpr_fun_unary_t) (mpX_ptr, mpX_srcptr);
62typedef void (*mpexpr_fun_unary_ui_t) (mpX_ptr, unsigned long);
63typedef int (*mpexpr_fun_i_unary_t) (mpX_srcptr);
64typedef int (*mpexpr_fun_i_unary_ui_t) (unsigned long);
65
66typedef void (*mpexpr_fun_binary_t) (mpX_ptr, mpX_srcptr, mpX_srcptr);
67typedef void (*mpexpr_fun_binary_ui_t) (mpX_ptr, mpX_srcptr, unsigned long);
68typedef int (*mpexpr_fun_i_binary_t) (mpX_srcptr, mpX_srcptr);
69typedef int (*mpexpr_fun_i_binary_ui_t) (mpX_srcptr, unsigned long);
70
71typedef void (*mpexpr_fun_ternary_t) (mpX_ptr, mpX_srcptr, mpX_srcptr, mpX_srcptr);
72typedef void (*mpexpr_fun_ternary_ui_t) (mpX_ptr, mpX_srcptr, mpX_srcptr, unsigned long);
73typedef int (*mpexpr_fun_i_ternary_t) (mpX_srcptr, mpX_srcptr, mpX_srcptr);
74typedef int (*mpexpr_fun_i_ternary_ui_t) (mpX_srcptr, mpX_srcptr, unsigned long);
75
76typedef size_t (*mpexpr_fun_number_t) (mpX_ptr, const char *str, size_t len, int base);
77typedef void (*mpexpr_fun_swap_t) (mpX_ptr, mpX_ptr);
78typedef unsigned long (*mpexpr_fun_get_ui_t) (mpX_srcptr);
79typedef void (*mpexpr_fun_set_si_t) (mpX_srcptr, long);
80
81struct mpexpr_control_t {
82 const struct mpexpr_operator_t *op;
83 int argcount;
84};
85
86#define MPEXPR_VARIABLES 26
87
88struct 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
123int mpexpr_evaluate (struct mpexpr_parse_t *p);
124int mpexpr_va_to_var (void *var[], va_list ap);
125size_t mpexpr_mpz_number (mpz_ptr res, const char *e, size_t elen, int base);