1/* mpf expression evaluation
2
3Copyright 2000, 2001, 2002 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 the GNU Lesser General Public License as published by
9the Free Software Foundation; either version 3 of the License, or (at your
10option) any later version.
11
12The GNU MP Library is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
15License for more details.
16
17You should have received a copy of the GNU Lesser General Public License
18along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
19
20#include <stdio.h>
21#include <string.h>
22#include "gmp.h"
23#include "expr-impl.h"
24
25
26/* Change this to "#define TRACE(x) x" to get some traces. */
27#define TRACE(x)
28
29
30static int
31e_mpf_sgn (mpf_srcptr x)
32{
33  return mpf_sgn (x);
34}
35
36
37static __gmp_const struct mpexpr_operator_t  _mpf_expr_standard_table[] = {
38
39  { "**",  (mpexpr_fun_t) mpf_pow_ui,
40    MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC,                   220 },
41
42  { "!",   (mpexpr_fun_t) e_mpf_sgn,
43    MPEXPR_TYPE_LOGICAL_NOT | MPEXPR_TYPE_PREFIX,                     210 },
44  { "-",   (mpexpr_fun_t) mpf_neg,
45    MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                           210 },
46
47  { "*",   (mpexpr_fun_t) mpf_mul,           MPEXPR_TYPE_BINARY,      200 },
48  { "/",   (mpexpr_fun_t) mpf_div,           MPEXPR_TYPE_BINARY,      200 },
49
50  { "+",   (mpexpr_fun_t) mpf_add,           MPEXPR_TYPE_BINARY,      190 },
51  { "-",   (mpexpr_fun_t) mpf_sub,           MPEXPR_TYPE_BINARY,      190 },
52
53  { "<<",  (mpexpr_fun_t) mpf_mul_2exp,      MPEXPR_TYPE_BINARY_UI,   180 },
54  { ">>",  (mpexpr_fun_t) mpf_div_2exp,      MPEXPR_TYPE_BINARY_UI,   180 },
55
56  { "<=",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_LE,      170 },
57  { "<",   (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_LT,      170 },
58  { ">=",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_GE,      170 },
59  { ">",   (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_GT,      170 },
60
61  { "==",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_EQ,      160 },
62  { "!=",  (mpexpr_fun_t) mpf_cmp,           MPEXPR_TYPE_CMP_NE,      160 },
63
64  { "&&",  (mpexpr_fun_t) e_mpf_sgn,         MPEXPR_TYPE_LOGICAL_AND, 120 },
65  { "||",  (mpexpr_fun_t) e_mpf_sgn,         MPEXPR_TYPE_LOGICAL_OR,  110 },
66
67  { ":",   NULL,                             MPEXPR_TYPE_COLON,       101 },
68  { "?",   (mpexpr_fun_t) e_mpf_sgn,         MPEXPR_TYPE_QUESTION,    100 },
69
70  { ")",   NULL,                             MPEXPR_TYPE_CLOSEPAREN,    4 },
71  { "(",   NULL,                             MPEXPR_TYPE_OPENPAREN,     3 },
72  { ",",   NULL,                             MPEXPR_TYPE_ARGSEP,        2 },
73  { "$",   NULL,                             MPEXPR_TYPE_VARIABLE,      1 },
74
75  { "abs",      (mpexpr_fun_t) mpf_abs,          MPEXPR_TYPE_UNARY        },
76  { "ceil",     (mpexpr_fun_t) mpf_ceil,         MPEXPR_TYPE_UNARY        },
77  { "cmp",      (mpexpr_fun_t) mpf_cmp,          MPEXPR_TYPE_I_BINARY     },
78  { "eq",       (mpexpr_fun_t) mpf_eq,           MPEXPR_TYPE_I_TERNARY_UI },
79  { "floor",    (mpexpr_fun_t) mpf_floor,        MPEXPR_TYPE_UNARY        },
80  { "integer_p",(mpexpr_fun_t) mpf_integer_p,    MPEXPR_TYPE_I_UNARY      },
81  { "max",   (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },
82  { "min",   (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MIN | MPEXPR_TYPE_PAIRWISE },
83  { "reldiff",  (mpexpr_fun_t) mpf_reldiff,      MPEXPR_TYPE_BINARY       },
84  { "sgn",      (mpexpr_fun_t) e_mpf_sgn,        MPEXPR_TYPE_I_UNARY      },
85  { "sqrt",     (mpexpr_fun_t) mpf_sqrt,         MPEXPR_TYPE_UNARY        },
86  { "trunc",    (mpexpr_fun_t) mpf_trunc,        MPEXPR_TYPE_UNARY        },
87
88  { NULL }
89};
90
91__gmp_const struct mpexpr_operator_t * __gmp_const mpf_expr_standard_table
92= _mpf_expr_standard_table;
93
94
95int
96#if HAVE_STDARG
97mpf_expr (mpf_ptr res, int base, __gmp_const char *e, ...)
98#else
99mpf_expr (va_alist)
100     va_dcl
101#endif
102{
103  mpf_srcptr  var[MPEXPR_VARIABLES];
104  va_list     ap;
105  int         ret;
106#if HAVE_STDARG
107  va_start (ap, e);
108#else
109  mpf_ptr           res;
110  int               base;
111  __gmp_const char  *e;
112  va_start (ap);
113  res  = va_arg (ap, mpf_ptr);
114  base = va_arg (ap, int);
115  e    = va_arg (ap, __gmp_const char *);
116#endif
117
118  TRACE (printf ("mpf_expr(): base %d, %s\n", base, e));
119  ret = mpexpr_va_to_var ((void **) var, ap);
120  va_end (ap);
121
122  if (ret != MPEXPR_RESULT_OK)
123    return ret;
124
125  return mpf_expr_a (mpf_expr_standard_table, res, base,
126		     mpf_get_prec (res), e, strlen(e), var);
127}
128