Home | History | Annotate | Line # | Download | only in expr
README revision 1.1
      1  1.1  mrg Copyright 2001, 2004 Free Software Foundation, Inc.
      2  1.1  mrg 
      3  1.1  mrg This file is part of the GNU MP Library.
      4  1.1  mrg 
      5  1.1  mrg The GNU MP Library is free software; you can redistribute it and/or modify
      6  1.1  mrg it under the terms of the GNU Lesser General Public License as published by
      7  1.1  mrg the Free Software Foundation; either version 3 of the License, or (at your
      8  1.1  mrg option) any later version.
      9  1.1  mrg 
     10  1.1  mrg The GNU MP Library is distributed in the hope that it will be useful, but
     11  1.1  mrg WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  1.1  mrg or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
     13  1.1  mrg License for more details.
     14  1.1  mrg 
     15  1.1  mrg You should have received a copy of the GNU Lesser General Public License
     16  1.1  mrg along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.
     17  1.1  mrg 
     18  1.1  mrg 
     19  1.1  mrg 
     20  1.1  mrg 
     21  1.1  mrg 
     22  1.1  mrg 
     23  1.1  mrg                     GMP EXPRESSION EVALUATION
     24  1.1  mrg                     -------------------------
     25  1.1  mrg 
     26  1.1  mrg 
     27  1.1  mrg 
     28  1.1  mrg THIS CODE IS PRELIMINARY AND MAY BE SUBJECT TO INCOMPATIBLE CHANGES IN
     29  1.1  mrg FUTURE VERSIONS OF GMP.
     30  1.1  mrg 
     31  1.1  mrg 
     32  1.1  mrg 
     33  1.1  mrg The files in this directory implement a simple scheme of string based
     34  1.1  mrg expression parsing and evaluation, supporting mpz, mpq and mpf.
     35  1.1  mrg 
     36  1.1  mrg This will be slower than direct GMP library calls, but may be convenient in
     37  1.1  mrg various circumstances, such as while prototyping, or for letting a user
     38  1.1  mrg enter values in symbolic form.  "2**5723-7" for example is a lot easier to
     39  1.1  mrg enter or maintain than the equivalent written out in decimal.
     40  1.1  mrg 
     41  1.1  mrg 
     42  1.1  mrg 
     43  1.1  mrg BUILDING
     44  1.1  mrg 
     45  1.1  mrg Nothing in this directory is a normal part of libgmp, and nothing is built
     46  1.1  mrg or installed, but various Makefile rules are available to compile
     47  1.1  mrg everything.
     48  1.1  mrg 
     49  1.1  mrg All the functions are available through a little library (there's no shared
     50  1.1  mrg library since upward binary compatibility is not guaranteed).
     51  1.1  mrg 
     52  1.1  mrg 	make libexpr.a
     53  1.1  mrg 
     54  1.1  mrg In a program, prototypes are available using
     55  1.1  mrg 
     56  1.1  mrg 	#include "expr.h"
     57  1.1  mrg 
     58  1.1  mrg run-expr.c is a sample program doing evaluations from the command line.
     59  1.1  mrg 
     60  1.1  mrg 	make run-expr
     61  1.1  mrg 	./run-expr '1+2*3'
     62  1.1  mrg 
     63  1.1  mrg t-expr.c is self-test program, it prints nothing if successful.
     64  1.1  mrg 
     65  1.1  mrg 	make t-expr
     66  1.1  mrg 	./t-expr
     67  1.1  mrg 
     68  1.1  mrg The expr*.c sources don't depend on gmp-impl.h and can be compiled with just
     69  1.1  mrg a standard installed GMP.  This isn't true of t-expr though, since it uses
     70  1.1  mrg some of the internal tests/libtests.la.
     71  1.1  mrg 
     72  1.1  mrg 
     73  1.1  mrg 
     74  1.1  mrg SIMPLE USAGE
     75  1.1  mrg 
     76  1.1  mrg int mpz_expr (mpz_t res, int base, const char *e, ...);
     77  1.1  mrg int mpq_expr (mpq_t res, int base, const char *e, ...);
     78  1.1  mrg int mpf_expr (mpf_t res, int base, const char *e, ...);
     79  1.1  mrg 
     80  1.1  mrg These functions evaluate simple arithmetic expressions.  For example,
     81  1.1  mrg 
     82  1.1  mrg 	mpz_expr (result, 0, "123+456", NULL);
     83  1.1  mrg 
     84  1.1  mrg Numbers are parsed by mpz_expr and mpq_expr the same as mpz_set_str with the
     85  1.1  mrg given base.  mpf_expr follows mpf_set_str, but supporting an "0x" prefix for
     86  1.1  mrg hex when base==0.
     87  1.1  mrg 
     88  1.1  mrg 	mpz_expr (result, 0, "0xAAAA * 0x5555", NULL);
     89  1.1  mrg 
     90  1.1  mrg White space, as indicated by <ctype.h> isspace(), is ignored except for the
     91  1.1  mrg purpose of separating tokens.
     92  1.1  mrg 
     93  1.1  mrg Variables can be included in expressions by putting them in the varargs list
     94  1.1  mrg after the string.  "a", "b", "c" etc in the expression string designate
     95  1.1  mrg those values.  For example,
     96  1.1  mrg 
     97  1.1  mrg         mpq_t  foo, bar;
     98  1.1  mrg         ...
     99  1.1  mrg 	mpq_expr (q, 10, "2/3 + 1/a + b/2", foo, bar, NULL);
    100  1.1  mrg 
    101  1.1  mrg Here "a" will be the value from foo and "b" from bar.  Up to 26 variables
    102  1.1  mrg can be included this way.  The NULL must be present to indicate the end of
    103  1.1  mrg the list.
    104  1.1  mrg 
    105  1.1  mrg Variables can also be written "$a", "$b" etc.  This is necessary when using
    106  1.1  mrg bases greater than 10 since plain "a", "b" etc will otherwise be interpreted
    107  1.1  mrg as numbers.  For example,
    108  1.1  mrg 
    109  1.1  mrg         mpf_t  quux;
    110  1.1  mrg         mpf_expr (f, 16, "F00F@-6 * $a", quux, NULL);
    111  1.1  mrg 
    112  1.1  mrg All the standard C operators are available, with the usual precedences, plus
    113  1.1  mrg "**" for exponentiation at the highest precedence (and right associative).
    114  1.1  mrg 
    115  1.1  mrg         Operators      Precedence
    116  1.1  mrg          **              220
    117  1.1  mrg          ~ ! - (unary)   210
    118  1.1  mrg          * / %           200
    119  1.1  mrg          + -             190
    120  1.1  mrg          << >>           180
    121  1.1  mrg          <= < >= >       170
    122  1.1  mrg          == !=           160
    123  1.1  mrg          &               150
    124  1.1  mrg          ^               140
    125  1.1  mrg          |               130
    126  1.1  mrg          &&              120
    127  1.1  mrg          ||              110
    128  1.1  mrg          ? :             100/101
    129  1.1  mrg 
    130  1.1  mrg Currently only mpz_expr has the bitwise ~ % & ^ and | operators.  The
    131  1.1  mrg precedence numbers are of interest in the advanced usage described below.
    132  1.1  mrg 
    133  1.1  mrg Various functions are available too.  For example,
    134  1.1  mrg 
    135  1.1  mrg         mpz_expr (res, 10, "gcd(123,456,789) * abs(a)", var, NULL);
    136  1.1  mrg 
    137  1.1  mrg The following is the full set of functions,
    138  1.1  mrg 
    139  1.1  mrg         mpz_expr
    140  1.1  mrg             abs bin clrbit cmp cmpabs congruent_p divisible_p even_p fib fac
    141  1.1  mrg             gcd hamdist invert jacobi kronecker lcm lucnum max min nextprime
    142  1.1  mrg             odd_p perfect_power_p perfect_square_p popcount powm
    143  1.1  mrg             probab_prime_p root scan0 scan1 setbit sgn sqrt
    144  1.1  mrg 
    145  1.1  mrg         mpq_expr
    146  1.1  mrg             abs, cmp, den, max, min, num, sgn
    147  1.1  mrg 
    148  1.1  mrg         mpf_expr
    149  1.1  mrg             abs, ceil, cmp, eq, floor, integer_p, max, min, reldiff, sgn,
    150  1.1  mrg             sqrt, trunc
    151  1.1  mrg 
    152  1.1  mrg All these are the same as the GMP library functions, except that min and max
    153  1.1  mrg don't exist in the library.  Note also that min, max, gcd and lcm take any
    154  1.1  mrg number of arguments, not just two.
    155  1.1  mrg 
    156  1.1  mrg mpf_expr does all calculations to the precision of the destination variable.
    157  1.1  mrg 
    158  1.1  mrg 
    159  1.1  mrg Expression parsing can succeed or fail.  The return value indicates this,
    160  1.1  mrg and will be one of the following
    161  1.1  mrg 
    162  1.1  mrg 	MPEXPR_RESULT_OK
    163  1.1  mrg 	MPEXPR_RESULT_BAD_VARIABLE
    164  1.1  mrg 	MPEXPR_RESULT_BAD_TABLE
    165  1.1  mrg 	MPEXPR_RESULT_PARSE_ERROR
    166  1.1  mrg 	MPEXPR_RESULT_NOT_UI
    167  1.1  mrg 
    168  1.1  mrg BAD_VARIABLE is when a variable is referenced that hasn't been provided.
    169  1.1  mrg For example if "c" is used when only two parameters have been passed.
    170  1.1  mrg BAD_TABLE is applicable to the advanced usage described below.
    171  1.1  mrg 
    172  1.1  mrg PARSE_ERROR is a general syntax error, returned for any mal-formed input
    173  1.1  mrg string.
    174  1.1  mrg 
    175  1.1  mrg NOT_UI is returned when an attempt is made to use an operand that's bigger
    176  1.1  mrg than an "unsigned long" with a function that's restricted to that range.
    177  1.1  mrg For example "fib" is mpz_fib_ui and only accepts an "unsigned long".
    178  1.1  mrg 
    179  1.1  mrg 
    180  1.1  mrg 
    181  1.1  mrg 
    182  1.1  mrg ADVANCED USAGE
    183  1.1  mrg 
    184  1.1  mrg int mpz_expr_a (const struct mpexpr_operator_t *table,
    185  1.1  mrg                 mpz_ptr res, int base, const char *e, size_t elen,
    186  1.1  mrg                 mpz_srcptr var[26])
    187  1.1  mrg int mpq_expr_a (const struct mpexpr_operator_t *table,
    188  1.1  mrg                 mpq_ptr res, int base, const char *e, size_t elen,
    189  1.1  mrg                 mpq_srcptr var[26])
    190  1.1  mrg int mpf_expr_a (const struct mpexpr_operator_t *table,
    191  1.1  mrg                 mpf_ptr res, int base, unsigned long prec,
    192  1.1  mrg                 const char *e, size_t elen,
    193  1.1  mrg                 mpf_srcptr var[26])
    194  1.1  mrg 
    195  1.1  mrg These functions are an advanced interface to expression parsing.
    196  1.1  mrg 
    197  1.1  mrg The string is taken as pointer and length.  This makes it possible to parse
    198  1.1  mrg an expression in the middle of somewhere without copying and null
    199  1.1  mrg terminating it.
    200  1.1  mrg 
    201  1.1  mrg Variables are an array of 26 pointers to the appropriate operands, or NULL
    202  1.1  mrg for variables that are not available.  Any combination of variables can be
    203  1.1  mrg given, for example just "x" and "y" (var[23] and var[24]) could be set.
    204  1.1  mrg 
    205  1.1  mrg Operators and functions are specified with a table.  This makes it possible
    206  1.1  mrg to provide additional operators or functions, or to completely change the
    207  1.1  mrg syntax.  The standard tables used by the simple functions above are
    208  1.1  mrg available as
    209  1.1  mrg 
    210  1.1  mrg 	const struct mpexpr_operator_t * const mpz_expr_standard_table;
    211  1.1  mrg 	const struct mpexpr_operator_t * const mpq_expr_standard_table;
    212  1.1  mrg 	const struct mpexpr_operator_t * const mpf_expr_standard_table;
    213  1.1  mrg 
    214  1.1  mrg struct mpexpr_operator_t is the following
    215  1.1  mrg 
    216  1.1  mrg 	struct mpexpr_operator_t {
    217  1.1  mrg 	  const char    *name;
    218  1.1  mrg 	  mpexpr_fun_t  fun;
    219  1.1  mrg 	  int           type;
    220  1.1  mrg 	  int           precedence;
    221  1.1  mrg 	};
    222  1.1  mrg 
    223  1.1  mrg         typedef void (*mpexpr_fun_t) (void);
    224  1.1  mrg 
    225  1.1  mrg As an example, the standard mpz_expr table entry for multiplication is as
    226  1.1  mrg follows.  See the source code for the full set of standard entries.
    227  1.1  mrg 
    228  1.1  mrg 	{ "*", (mpexpr_fun_t) mpz_mul, MPEXPR_TYPE_BINARY, 200 },
    229  1.1  mrg 
    230  1.1  mrg "name" is the string to parse, "fun" is the function to call for it, "type"
    231  1.1  mrg indicates what parameters the function takes (among other things), and
    232  1.1  mrg "precedence" sets its operator precedence.
    233  1.1  mrg 
    234  1.1  mrg A NULL for "name" indicates the end of the table, so for example an mpf
    235  1.1  mrg table with nothing but addition could be
    236  1.1  mrg 
    237  1.1  mrg         struct mpexpr_operator_t  table[] = {
    238  1.1  mrg           { "+", (mpexpr_fun_t) mpf_add, MPEXPR_TYPE_BINARY, 190 },
    239  1.1  mrg           { NULL }
    240  1.1  mrg         };
    241  1.1  mrg 
    242  1.1  mrg A special type MPEXPR_TYPE_NEW_TABLE makes it possible to chain from one
    243  1.1  mrg table to another.  For example the following would add a "mod" operator to
    244  1.1  mrg the standard mpz table,
    245  1.1  mrg 
    246  1.1  mrg         struct mpexpr_operator_t  table[] = {
    247  1.1  mrg         { "mod", (mpexpr_fun_t) mpz_fdiv_r, MPEXPR_TYPE_BINARY, 125 },
    248  1.1  mrg         { (const char *) mpz_expr_standard_table, NULL, MPEXPR_TYPE_NEW_TABLE }
    249  1.1  mrg         };
    250  1.1  mrg 
    251  1.1  mrg Notice the low precedence on "mod", so that for instance "45+26 mod 7"
    252  1.1  mrg parses as "(45+26)mod7".
    253  1.1  mrg 
    254  1.1  mrg 
    255  1.1  mrg Functions are designated by a precedence of 0.  They always occur as
    256  1.1  mrg "foo(expr)" and so have no need for a precedence level.  mpq_abs in the
    257  1.1  mrg standard mpq table is
    258  1.1  mrg 
    259  1.1  mrg 	{ "abs", (mpexpr_fun_t) mpq_abs, MPEXPR_TYPE_UNARY },
    260  1.1  mrg 
    261  1.1  mrg Functions expecting no arguments as in "foo()" can be given with
    262  1.1  mrg MPEXPR_TYPE_0ARY, or actual constants to be parsed as just "foo" are
    263  1.1  mrg MPEXPR_TYPE_CONSTANT.  For example if a "void mpf_const_pi(mpf_t f)"
    264  1.1  mrg function existed (which it doesn't) it could be,
    265  1.1  mrg 
    266  1.1  mrg 	{ "pi", (mpexpr_fun_t) mpf_const_pi, MPEXPR_TYPE_CONSTANT },
    267  1.1  mrg 
    268  1.1  mrg 
    269  1.1  mrg Parsing of operator names is done by seeking the table entry with the
    270  1.1  mrg longest matching name.  So for instance operators "<" and "<=" exist, and
    271  1.1  mrg when presented with "x <= y" the parser matches "<=" because it's longer.
    272  1.1  mrg 
    273  1.1  mrg Parsing of function names, on the other hand, is done by requiring a whole
    274  1.1  mrg alphanumeric word to match.  For example presented with "fib2zz(5)" the
    275  1.1  mrg parser will attempt to find a function called "fib2zz".  A function "fib"
    276  1.1  mrg wouldn't be used because it doesn't match the whole word.
    277  1.1  mrg 
    278  1.1  mrg The flag MPEXPR_TYPE_WHOLEWORD can be ORed into an operator type to override
    279  1.1  mrg the default parsing style.  Similarly MPEXPR_TYPE_OPERATOR into a function.
    280  1.1  mrg 
    281  1.1  mrg 
    282  1.1  mrg Binary operators are left associative by default, meaning they're evaluated
    283  1.1  mrg from left to right, so for example "1+2+3" is treated as "(1+2)+3".
    284  1.1  mrg MPEXPR_TYPE_RIGHTASSOC can be ORed into the operator type to work from right
    285  1.1  mrg to left as in "1+(2+3)".  This is generally what's wanted for
    286  1.1  mrg exponentiation, and for example the standard mpz table has
    287  1.1  mrg 
    288  1.1  mrg         { "**", (mpexpr_fun_t) mpz_pow_ui,
    289  1.1  mrg           MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC, 220 }
    290  1.1  mrg 
    291  1.1  mrg Unary operators are postfix by default.  For example a factorial to be used
    292  1.1  mrg as "123!" might be
    293  1.1  mrg 
    294  1.1  mrg 	{ "!", (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI, 215 }
    295  1.1  mrg 
    296  1.1  mrg MPEXPR_TYPE_PREFIX can be ORed into the type to get a prefix operator.  For
    297  1.1  mrg instance negation (unary minus) in the standard mpf table is
    298  1.1  mrg 
    299  1.1  mrg 	{ "-", (mpexpr_fun_t) mpf_neg,
    300  1.1  mrg           MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX, 210 },
    301  1.1  mrg 
    302  1.1  mrg 
    303  1.1  mrg The same operator can exist as a prefix unary and a binary, or as a prefix
    304  1.1  mrg and postfix unary, simply by putting two entries in the table.  While
    305  1.1  mrg parsing the context determines which style is sought.  But note that the
    306  1.1  mrg same operator can't be both a postfix unary and a binary, since the parser
    307  1.1  mrg doesn't try to look ahead to decide which ought to be used.
    308  1.1  mrg 
    309  1.1  mrg When there's two entries for an operator, both prefix or both postfix (or
    310  1.1  mrg binary), then the first in the table will be used.  This makes it possible
    311  1.1  mrg to override an entry in a standard table, for example to change the function
    312  1.1  mrg it calls, or perhaps its precedence level.  The following would change mpz
    313  1.1  mrg division from tdiv to cdiv,
    314  1.1  mrg 
    315  1.1  mrg         struct mpexpr_operator_t  table[] = {
    316  1.1  mrg           { "/", (mpexpr_fun_t) mpz_cdiv_q, MPEXPR_TYPE_BINARY, 200 },
    317  1.1  mrg           { "%", (mpexpr_fun_t) mpz_cdiv_r, MPEXPR_TYPE_BINARY, 200 },
    318  1.1  mrg           { (char *) mpz_expr_standard_table, NULL, MPEXPR_TYPE_NEW_TABLE }
    319  1.1  mrg         };
    320  1.1  mrg 
    321  1.1  mrg 
    322  1.1  mrg The type field indicates what parameters the given function expects.  The
    323  1.1  mrg following styles of functions are supported.  mpz_t is shown, but of course
    324  1.1  mrg this is mpq_t for mpq_expr_a, mpf_t for mpf_expr_a, etc.
    325  1.1  mrg 
    326  1.1  mrg     MPEXPR_TYPE_CONSTANT     void func (mpz_t result);
    327  1.1  mrg 
    328  1.1  mrg     MPEXPR_TYPE_0ARY         void func (mpz_t result);
    329  1.1  mrg     MPEXPR_TYPE_I_0ARY       int func (void);
    330  1.1  mrg 
    331  1.1  mrg     MPEXPR_TYPE_UNARY        void func (mpz_t result, mpz_t op);
    332  1.1  mrg     MPEXPR_TYPE_UNARY_UI     void func (mpz_t result, unsigned long op);
    333  1.1  mrg     MPEXPR_TYPE_I_UNARY      int func (mpz_t op);
    334  1.1  mrg     MPEXPR_TYPE_I_UNARY_UI   int func (unsigned long op);
    335  1.1  mrg 
    336  1.1  mrg     MPEXPR_TYPE_BINARY       void func (mpz_t result, mpz_t op1, mpz_t op2);
    337  1.1  mrg     MPEXPR_TYPE_BINARY_UI    void func (mpz_t result,
    338  1.1  mrg                                         mpz_t op1, unsigned long op2);
    339  1.1  mrg     MPEXPR_TYPE_I_BINARY     int func (mpz_t op1, mpz_t op2);
    340  1.1  mrg     MPEXPR_TYPE_I_BINARY_UI  int func (mpz_t op1, unsigned long op2);
    341  1.1  mrg 
    342  1.1  mrg     MPEXPR_TYPE_TERNARY      void func (mpz_t result,
    343  1.1  mrg                                         mpz_t op1, mpz_t op2, mpz_t op3);
    344  1.1  mrg     MPEXPR_TYPE_TERNARY_UI   void func (mpz_t result, mpz_t op1, mpz_t op2,
    345  1.1  mrg                                         unsigned long op3);
    346  1.1  mrg     MPEXPR_TYPE_I_TERNARY    int func (mpz_t op1, mpz_t op2, mpz_t op3);
    347  1.1  mrg     MPEXPR_TYPE_I_TERNARY_UI int func (mpz_t op1, mpz_t op2,
    348  1.1  mrg                                        unsigned long op3);
    349  1.1  mrg 
    350  1.1  mrg Notice the pattern of "UI" for the last parameter as an unsigned long, or
    351  1.1  mrg "I" for the result as an "int" return value.
    352  1.1  mrg 
    353  1.1  mrg It's important that the declared type for an operator or function matches
    354  1.1  mrg the function pointer given.  Any mismatch will have unpredictable results.
    355  1.1  mrg 
    356  1.1  mrg For binary functions, a further type attribute is MPEXPR_TYPE_PAIRWISE which
    357  1.1  mrg indicates that any number of arguments should be accepted, and evaluated by
    358  1.1  mrg applying the given binary function to them pairwise.  This is used by gcd,
    359  1.1  mrg lcm, min and max.  For example the standard mpz gcd is
    360  1.1  mrg 
    361  1.1  mrg 	{ "gcd", (mpexpr_fun_t) mpz_gcd,
    362  1.1  mrg 	  MPEXPR_TYPE_BINARY | MPEXPR_TYPE_PAIRWISE },
    363  1.1  mrg 
    364  1.1  mrg Some special types exist for comparison operators (or functions).
    365  1.1  mrg MPEXPR_TYPE_CMP_LT through MPEXPR_TYPE_CMP_GE expect an MPEXPR_TYPE_I_BINARY
    366  1.1  mrg function, returning positive, negative or zero like mpz_cmp and similar.
    367  1.1  mrg For example the standard mpf "!=" operator is
    368  1.1  mrg 
    369  1.1  mrg 	{ "!=", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_CMP_NE, 160 },
    370  1.1  mrg 
    371  1.1  mrg But there's no obligation to use these types, for instance the standard mpq
    372  1.1  mrg table just uses a plain MPEXPR_TYPE_I_BINARY and mpq_equal for "==".
    373  1.1  mrg 
    374  1.1  mrg Further special types MPEXPR_TYPE_MIN and MPEXPR_TYPE_MAX exist to implement
    375  1.1  mrg the min and max functions, and they take a function like mpf_cmp similarly.
    376  1.1  mrg The standard mpf max function is
    377  1.1  mrg 
    378  1.1  mrg 	{ "max",  (mpexpr_fun_t) mpf_cmp,
    379  1.1  mrg           MPEXPR_TYPE_MAX | MPEXPR_TYPE_PAIRWISE },
    380  1.1  mrg 
    381  1.1  mrg These can be used as operators too, for instance the following would be the
    382  1.1  mrg >? operator which is a feature of GNU C++,
    383  1.1  mrg 
    384  1.1  mrg 	{ ">?", (mpexpr_fun_t) mpf_cmp, MPEXPR_TYPE_MAX, 175 },
    385  1.1  mrg 
    386  1.1  mrg Other special types are used to define "(" ")" parentheses, "," function
    387  1.1  mrg argument separator, "!" through "||" logical booleans, ternary "?"  ":", and
    388  1.1  mrg the "$" which introduces variables.  See the sources for how they should be
    389  1.1  mrg used.
    390  1.1  mrg 
    391  1.1  mrg 
    392  1.1  mrg User definable operator tables will have various uses.  For example,
    393  1.1  mrg 
    394  1.1  mrg   - a subset of the C operators, to be rid of infrequently used things
    395  1.1  mrg   - a more mathematical syntax like "." for multiply, "^" for powering,
    396  1.1  mrg     and "!" for factorial
    397  1.1  mrg   - a boolean evaluator with "^" for AND, "v" for OR
    398  1.1  mrg   - variables introduced with "%" instead of "$"
    399  1.1  mrg   - brackets as "[" and "]" instead of "(" and ")"
    400  1.1  mrg 
    401  1.1  mrg The only fixed parts of the parsing are the treatment of numbers, whitespace
    402  1.1  mrg and the two styles of operator/function name recognition.
    403  1.1  mrg 
    404  1.1  mrg As a final example, the following would be a complete mpz table implementing
    405  1.1  mrg some operators with a more mathematical syntax.  Notice there's no need to
    406  1.1  mrg preserve the standard precedence values, anything can be used so long as
    407  1.1  mrg they're in the desired relation to each other.  There's also no need to have
    408  1.1  mrg entries in precedence order, but it's convenient to do so to show what comes
    409  1.1  mrg where.
    410  1.1  mrg 
    411  1.1  mrg         static const struct mpexpr_operator_t  table[] = {
    412  1.1  mrg 	  { "^",   (mpexpr_fun_t) mpz_pow_ui,
    413  1.1  mrg             MPEXPR_TYPE_BINARY_UI | MPEXPR_TYPE_RIGHTASSOC,           9 },
    414  1.1  mrg 
    415  1.1  mrg           { "!",   (mpexpr_fun_t) mpz_fac_ui, MPEXPR_TYPE_UNARY_UI,   8 },
    416  1.1  mrg           { "-",   (mpexpr_fun_t) mpz_neg,
    417  1.1  mrg             MPEXPR_TYPE_UNARY | MPEXPR_TYPE_PREFIX,                   7 },
    418  1.1  mrg 
    419  1.1  mrg           { "*",   (mpexpr_fun_t) mpz_mul,    MPEXPR_TYPE_BINARY,     6 },
    420  1.1  mrg           { "/",   (mpexpr_fun_t) mpz_fdiv_q, MPEXPR_TYPE_BINARY,     6 },
    421  1.1  mrg 
    422  1.1  mrg           { "+",   (mpexpr_fun_t) mpz_add,    MPEXPR_TYPE_BINARY,     5 },
    423  1.1  mrg           { "-",   (mpexpr_fun_t) mpz_sub,    MPEXPR_TYPE_BINARY,     5 },
    424  1.1  mrg 
    425  1.1  mrg           { "mod", (mpexpr_fun_t) mpz_mod,    MPEXPR_TYPE_BINARY,     6 },
    426  1.1  mrg 
    427  1.1  mrg           { ")",   NULL,                      MPEXPR_TYPE_CLOSEPAREN, 4 },
    428  1.1  mrg           { "(",   NULL,                      MPEXPR_TYPE_OPENPAREN,  3 },
    429  1.1  mrg           { ",",   NULL,                      MPEXPR_TYPE_ARGSEP,     2 },
    430  1.1  mrg 
    431  1.1  mrg           { "$",   NULL,                      MPEXPR_TYPE_VARIABLE,   1 },
    432  1.1  mrg           { NULL }
    433  1.1  mrg         };
    434  1.1  mrg 
    435  1.1  mrg 
    436  1.1  mrg 
    437  1.1  mrg 
    438  1.1  mrg INTERNALS
    439  1.1  mrg 
    440  1.1  mrg Operator precedence is implemented using a control and data stack, there's
    441  1.1  mrg no C recursion.  When an expression like 1+2*3 is read the "+" is held on
    442  1.1  mrg the control stack and 1 on the data stack until "*" has been parsed and
    443  1.1  mrg applied to 2 and 3.  This happens any time a higher precedence operator
    444  1.1  mrg follows a lower one, or when a right-associative operator like "**" is
    445  1.1  mrg repeated.
    446  1.1  mrg 
    447  1.1  mrg Parentheses are handled by making "(" a special prefix unary with a low
    448  1.1  mrg precedence so a whole following expression is read.  The special operator
    449  1.1  mrg ")" knows to discard the pending "(".  Function arguments are handled
    450  1.1  mrg similarly, with the function pretending to be a low precedence prefix unary
    451  1.1  mrg operator, and with "," allowed within functions.  The same special ")"
    452  1.1  mrg operator recognises a pending function and will invoke it appropriately.
    453  1.1  mrg 
    454  1.1  mrg The ternary "? :" operator is also handled using precedences.  ":" is one
    455  1.1  mrg level higher than "?", so when a valid a?b:c is parsed the ":" finds a "?"
    456  1.1  mrg on the control stack.  It's a parse error for ":" to find anything else.
    457  1.1  mrg 
    458  1.1  mrg 
    459  1.1  mrg 
    460  1.1  mrg FUTURE
    461  1.1  mrg 
    462  1.1  mrg The ternary "?:" operator evaluates the "false" side of its pair, which is
    463  1.1  mrg wasteful, though it ought to be harmless.  It'd be better if it could
    464  1.1  mrg evaluate only the "true" side.  Similarly for the logical booleans "&&" and
    465  1.1  mrg "||" if they know their result already.
    466  1.1  mrg 
    467  1.1  mrg Functions like MPEXPR_TYPE_BINARY could return a status indicating operand
    468  1.1  mrg out of range or whatever, to get an error back through mpz_expr etc.  That
    469  1.1  mrg would want to be just an option, since plain mpz_add etc have no such
    470  1.1  mrg return.
    471  1.1  mrg 
    472  1.1  mrg Could have assignments like "a = b*c" modifying the input variables.
    473  1.1  mrg Assignment could be an operator attribute, making it expect an lvalue.
    474  1.1  mrg There would want to be a standard table without assignments available
    475  1.1  mrg though, so user input could be safely parsed.
    476  1.1  mrg 
    477  1.1  mrg The closing parenthesis table entry could specify the type of open paren it
    478  1.1  mrg expects, so that "(" and ")" could match and "[" and "]" match but not a
    479  1.1  mrg mixture of the two.  Currently "[" and "]" can be added, but there's no
    480  1.1  mrg error on writing a mixed expression like "2*(3+4]".  Maybe also there could
    481  1.1  mrg be a way to say that functions can only be written with one or the other
    482  1.1  mrg style of parens.
    483  1.1  mrg 
    484  1.1  mrg 
    485  1.1  mrg 
    486  1.1  mrg ----------------
    487  1.1  mrg Local variables:
    488  1.1  mrg mode: text
    489  1.1  mrg fill-column: 76
    490  1.1  mrg End:
    491