GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it
/* mpexpr_evaluate -- shared code for simple expression evaluation12Copyright 2000-2002, 2004 Free Software Foundation, Inc.34This file is part of the GNU MP Library.56The GNU MP Library is free software; you can redistribute it and/or modify7it under the terms of either:89* the GNU Lesser General Public License as published by the Free10Software Foundation; either version 3 of the License, or (at your11option) any later version.1213or1415* the GNU General Public License as published by the Free Software16Foundation; either version 2 of the License, or (at your option) any17later version.1819or both in parallel, as here.2021The GNU MP Library is distributed in the hope that it will be useful, but22WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY23or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License24for more details.2526You should have received copies of the GNU General Public License and the27GNU Lesser General Public License along with the GNU MP Library. If not,28see https://www.gnu.org/licenses/. */2930#include <ctype.h>31#include <stdio.h>32#include <string.h>3334#include "gmp.h"35#include "expr-impl.h"363738/* Change this to "#define TRACE(x) x" to get some traces. The trace39printfs junk up the code a bit, but it's very hard to tell what's going40on without them. Set MPX_TRACE to a suitable output function for the41mpz/mpq/mpf being run (if you have the wrong trace function it'll42probably segv). */4344#define TRACE(x)45#define MPX_TRACE mpz_trace464748/* A few helper macros copied from gmp-impl.h */49#define ALLOCATE_FUNC_TYPE(n,type) \50((type *) (*allocate_func) ((n) * sizeof (type)))51#define ALLOCATE_FUNC_LIMBS(n) ALLOCATE_FUNC_TYPE (n, mp_limb_t)52#define REALLOCATE_FUNC_TYPE(p, old_size, new_size, type) \53((type *) (*reallocate_func) \54(p, (old_size) * sizeof (type), (new_size) * sizeof (type)))55#define REALLOCATE_FUNC_LIMBS(p, old_size, new_size) \56REALLOCATE_FUNC_TYPE(p, old_size, new_size, mp_limb_t)57#define FREE_FUNC_TYPE(p,n,type) (*free_func) (p, (n) * sizeof (type))58#define FREE_FUNC_LIMBS(p,n) FREE_FUNC_TYPE (p, n, mp_limb_t)59#define ASSERT(x)60616263/* All the error strings are just for diagnostic traces. Only the error64code is actually returned. */65#define ERROR(str,code) \66{ \67TRACE (printf ("%s\n", str)); \68p->error_code = (code); \69goto done; \70}717273#define REALLOC(ptr, alloc, incr, type) \74do { \75int new_alloc = (alloc) + (incr); \76ptr = REALLOCATE_FUNC_TYPE (ptr, alloc, new_alloc, type); \77(alloc) = new_alloc; \78} while (0)798081/* data stack top element */82#define SP (p->data_stack + p->data_top)8384/* Make sure there's room for another data element above current top.85reallocate_func is fetched for when this macro is used in lookahead(). */86#define DATA_SPACE() \87do { \88if (p->data_top + 1 >= p->data_alloc) \89{ \90void *(*reallocate_func) (void *, size_t, size_t); \91mp_get_memory_functions (NULL, &reallocate_func, NULL); \92TRACE (printf ("grow stack from %d\n", p->data_alloc)); \93REALLOC (p->data_stack, p->data_alloc, 20, union mpX_t); \94} \95ASSERT (p->data_top + 1 <= p->data_inited); \96if (p->data_top + 1 == p->data_inited) \97{ \98TRACE (printf ("initialize %d\n", p->data_top + 1)); \99(*p->mpX_init) (&p->data_stack[p->data_top + 1], p->prec); \100p->data_inited++; \101} \102} while (0)103104#define DATA_PUSH() \105do { \106p->data_top++; \107ASSERT (p->data_top < p->data_alloc); \108ASSERT (p->data_top < p->data_inited); \109} while (0)110111/* the last stack entry is never popped, so top>=0 will be true */112#define DATA_POP(n) \113do { \114p->data_top -= (n); \115ASSERT (p->data_top >= 0); \116} while (0)117118119/* lookahead() parses the next token. Return 1 if successful, with some120extra data. Return 0 if fail, with reason in p->error_code.121122"prefix" is MPEXPR_TYPE_PREFIX if an operator with that attribute is123preferred, or 0 if an operator without is preferred. */124125#define TOKEN_EOF -1 /* no extra data */126#define TOKEN_VALUE -2 /* pushed onto data stack */127#define TOKEN_OPERATOR -3 /* stored in p->token_op */128#define TOKEN_FUNCTION -4 /* stored in p->token_op */129130#define TOKEN_NAME(n) \131((n) == TOKEN_EOF ? "TOKEN_EOF" \132: (n) == TOKEN_VALUE ? "TOKEN_VALUE" \133: (n) == TOKEN_OPERATOR ? "TOKEN_OPERATOR" \134: (n) == TOKEN_VALUE ? "TOKEN_FUNCTION" \135: "UNKNOWN TOKEN")136137/* Functions default to being parsed as whole words, operators to match just138at the start of the string. The type flags override this. */139#define WHOLEWORD(op) \140(op->precedence == 0 \141? (! (op->type & MPEXPR_TYPE_OPERATOR)) \142: (op->type & MPEXPR_TYPE_WHOLEWORD))143144#define isasciispace(c) (isascii (c) && isspace (c))145146static int147lookahead (struct mpexpr_parse_t *p, int prefix)148{149const struct mpexpr_operator_t *op, *op_found;150size_t oplen, oplen_found, wlen;151int i;152153/* skip white space */154while (p->elen > 0 && isasciispace (*p->e))155p->e++, p->elen--;156157if (p->elen == 0)158{159TRACE (printf ("lookahead EOF\n"));160p->token = TOKEN_EOF;161return 1;162}163164DATA_SPACE ();165166/* Get extent of whole word. */167for (wlen = 0; wlen < p->elen; wlen++)168if (! isasciicsym (p->e[wlen]))169break;170171TRACE (printf ("lookahead at: \"%.*s\" length %u, word %u\n",172(int) p->elen, p->e, p->elen, wlen));173174op_found = NULL;175oplen_found = 0;176for (op = p->table; op->name != NULL; op++)177{178if (op->type == MPEXPR_TYPE_NEW_TABLE)179{180printf ("new\n");181op = (struct mpexpr_operator_t *) op->name - 1;182continue;183}184185oplen = strlen (op->name);186if (! ((WHOLEWORD (op) ? wlen == oplen : p->elen >= oplen)187&& memcmp (p->e, op->name, oplen) == 0))188continue;189190/* Shorter matches don't replace longer previous ones. */191if (op_found && oplen < oplen_found)192continue;193194/* On a match of equal length to a previous one, the old match isn't195replaced if it has the preferred prefix, and if it doesn't then196it's not replaced if the new one also doesn't. */197if (op_found && oplen == oplen_found198&& ((op_found->type & MPEXPR_TYPE_PREFIX) == prefix199|| (op->type & MPEXPR_TYPE_PREFIX) != prefix))200continue;201202/* This is now either the first match seen, or a longer than previous203match, or an equal to previous one but with a preferred prefix. */204op_found = op;205oplen_found = oplen;206}207208if (op_found)209{210p->e += oplen_found, p->elen -= oplen_found;211212if (op_found->type == MPEXPR_TYPE_VARIABLE)213{214if (p->elen == 0)215ERROR ("end of string expecting a variable",216MPEXPR_RESULT_PARSE_ERROR);217i = p->e[0] - 'a';218if (i < 0 || i >= MPEXPR_VARIABLES)219ERROR ("bad variable name", MPEXPR_RESULT_BAD_VARIABLE);220goto variable;221}222223if (op_found->precedence == 0)224{225TRACE (printf ("lookahead function: %s\n", op_found->name));226p->token = TOKEN_FUNCTION;227p->token_op = op_found;228return 1;229}230else231{232TRACE (printf ("lookahead operator: %s\n", op_found->name));233p->token = TOKEN_OPERATOR;234p->token_op = op_found;235return 1;236}237}238239oplen = (*p->mpX_number) (SP+1, p->e, p->elen, p->base);240if (oplen != 0)241{242p->e += oplen, p->elen -= oplen;243p->token = TOKEN_VALUE;244DATA_PUSH ();245TRACE (MPX_TRACE ("lookahead number", SP));246return 1;247}248249/* Maybe an unprefixed one character variable */250i = p->e[0] - 'a';251if (wlen == 1 && i >= 0 && i < MPEXPR_VARIABLES)252{253variable:254p->e++, p->elen--;255if (p->var[i] == NULL)256ERROR ("NULL variable", MPEXPR_RESULT_BAD_VARIABLE);257TRACE (printf ("lookahead variable: var[%d] = ", i);258MPX_TRACE ("", p->var[i]));259p->token = TOKEN_VALUE;260DATA_PUSH ();261(*p->mpX_set) (SP, p->var[i]);262return 1;263}264265ERROR ("no token matched", MPEXPR_RESULT_PARSE_ERROR);266267done:268return 0;269}270271272/* control stack current top element */273#define CP (p->control_stack + p->control_top)274275/* make sure there's room for another control element above current top */276#define CONTROL_SPACE() \277do { \278if (p->control_top + 1 >= p->control_alloc) \279{ \280TRACE (printf ("grow control stack from %d\n", p->control_alloc)); \281REALLOC (p->control_stack, p->control_alloc, 20, \282struct mpexpr_control_t); \283} \284} while (0)285286/* Push an operator on the control stack, claiming currently to have the287given number of args ready. Local variable "op" is used in case opptr is288a reference through CP. */289#define CONTROL_PUSH(opptr,args) \290do { \291const struct mpexpr_operator_t *op = opptr; \292struct mpexpr_control_t *cp; \293CONTROL_SPACE (); \294p->control_top++; \295ASSERT (p->control_top < p->control_alloc); \296cp = CP; \297cp->op = op; \298cp->argcount = (args); \299TRACE_CONTROL("control stack push:"); \300} while (0)301302/* The special operator_done is never popped, so top>=0 will hold. */303#define CONTROL_POP() \304do { \305p->control_top--; \306ASSERT (p->control_top >= 0); \307TRACE_CONTROL ("control stack pop:"); \308} while (0)309310#define TRACE_CONTROL(str) \311TRACE ({ \312int i; \313printf ("%s depth %d:", str, p->control_top); \314for (i = 0; i <= p->control_top; i++) \315printf (" \"%s\"(%d)", \316p->control_stack[i].op->name, \317p->control_stack[i].argcount); \318printf ("\n"); \319});320321322#define LOOKAHEAD(prefix) \323do { \324if (! lookahead (p, prefix)) \325goto done; \326} while (0)327328#define CHECK_UI(n) \329do { \330if (! (*p->mpX_ulong_p) (n)) \331ERROR ("operand doesn't fit ulong", MPEXPR_RESULT_NOT_UI); \332} while (0)333334#define CHECK_ARGCOUNT(str,n) \335do { \336if (CP->argcount != (n)) \337{ \338TRACE (printf ("wrong number of arguments for %s, got %d want %d", \339str, CP->argcount, n)); \340ERROR ("", MPEXPR_RESULT_PARSE_ERROR); \341} \342} while (0)343344345/* There's two basic states here. In both p->token is the next token.346347"another_expr" is when a whole expression should be parsed. This means a348literal or variable value possibly followed by an operator, or a function349or prefix operator followed by a further whole expression.350351"another_operator" is when an expression has been parsed and its value is352on the top of the data stack (SP) and an optional further postfix or353infix operator should be parsed.354355In "another_operator" precedences determine whether to push the operator356onto the control stack, or instead go to "apply_control" to reduce the357operator currently on top of the control stack.358359When an operator has both a prefix and postfix/infix form, a LOOKAHEAD()360for "another_expr" will seek the prefix form, a LOOKAHEAD() for361"another_operator" will seek the postfix/infix form. The grammar is362simple enough that the next state is known before reading the next token.363364Argument count checking guards against functions consuming the wrong365number of operands from the data stack. The same checks are applied to366operators, but will always pass since a UNARY or BINARY will only ever367parse with the correct operands. */368369int370mpexpr_evaluate (struct mpexpr_parse_t *p)371{372void *(*allocate_func) (size_t);373void *(*reallocate_func) (void *, size_t, size_t);374void (*free_func) (void *, size_t);375376mp_get_memory_functions (&allocate_func, &reallocate_func, &free_func);377378TRACE (printf ("mpexpr_evaluate() base %d \"%.*s\"\n",379p->base, (int) p->elen, p->e));380381/* "done" is a special sentinel at the bottom of the control stack,382precedence -1 is lower than any normal operator. */383{384static const struct mpexpr_operator_t operator_done385= { "DONE", NULL, MPEXPR_TYPE_DONE, -1 };386387p->control_alloc = 20;388p->control_stack = ALLOCATE_FUNC_TYPE (p->control_alloc,389struct mpexpr_control_t);390p->control_top = 0;391CP->op = &operator_done;392CP->argcount = 1;393}394395p->data_inited = 0;396p->data_alloc = 20;397p->data_stack = ALLOCATE_FUNC_TYPE (p->data_alloc, union mpX_t);398p->data_top = -1;399400p->error_code = MPEXPR_RESULT_OK;401402403another_expr_lookahead:404LOOKAHEAD (MPEXPR_TYPE_PREFIX);405TRACE (printf ("another expr\n"));406407/*another_expr:*/408switch (p->token) {409case TOKEN_VALUE:410goto another_operator_lookahead;411412case TOKEN_OPERATOR:413TRACE (printf ("operator %s\n", p->token_op->name));414if (! (p->token_op->type & MPEXPR_TYPE_PREFIX))415ERROR ("expected a prefix operator", MPEXPR_RESULT_PARSE_ERROR);416417CONTROL_PUSH (p->token_op, 1);418goto another_expr_lookahead;419420case TOKEN_FUNCTION:421CONTROL_PUSH (p->token_op, 1);422423if (p->token_op->type & MPEXPR_TYPE_CONSTANT)424goto apply_control_lookahead;425426LOOKAHEAD (MPEXPR_TYPE_PREFIX);427if (! (p->token == TOKEN_OPERATOR428&& p->token_op->type == MPEXPR_TYPE_OPENPAREN))429ERROR ("expected open paren for function", MPEXPR_RESULT_PARSE_ERROR);430431TRACE (printf ("open paren for function \"%s\"\n", CP->op->name));432433if ((CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) == MPEXPR_TYPE_NARY(0))434{435LOOKAHEAD (0);436if (! (p->token == TOKEN_OPERATOR437&& p->token_op->type == MPEXPR_TYPE_CLOSEPAREN))438ERROR ("expected close paren for 0ary function",439MPEXPR_RESULT_PARSE_ERROR);440goto apply_control_lookahead;441}442443goto another_expr_lookahead;444}445ERROR ("unrecognised start of expression", MPEXPR_RESULT_PARSE_ERROR);446447448another_operator_lookahead:449LOOKAHEAD (0);450another_operator:451TRACE (printf ("another operator maybe: %s\n", TOKEN_NAME(p->token)));452453switch (p->token) {454case TOKEN_EOF:455goto apply_control;456457case TOKEN_OPERATOR:458/* The next operator is compared to the one on top of the control stack.459If the next is lower precedence, or the same precedence and not460right-associative, then reduce using the control stack and look at461the next operator again later. */462463#define PRECEDENCE_TEST_REDUCE(tprec,cprec,ttype,ctype) \464((tprec) < (cprec) \465|| ((tprec) == (cprec) && ! ((ttype) & MPEXPR_TYPE_RIGHTASSOC)))466467if (PRECEDENCE_TEST_REDUCE (p->token_op->precedence, CP->op->precedence,468p->token_op->type, CP->op->type))469{470TRACE (printf ("defer operator: %s (prec %d vs %d, type 0x%X)\n",471p->token_op->name,472p->token_op->precedence, CP->op->precedence,473p->token_op->type));474goto apply_control;475}476477/* An argsep is a binary operator, but is never pushed on the control478stack, it just accumulates an extra argument for a function. */479if (p->token_op->type == MPEXPR_TYPE_ARGSEP)480{481if (CP->op->precedence != 0)482ERROR ("ARGSEP not in a function call", MPEXPR_RESULT_PARSE_ERROR);483484TRACE (printf ("argsep for function \"%s\"(%d)\n",485CP->op->name, CP->argcount));486487#define IS_PAIRWISE(type) \488(((type) & (MPEXPR_TYPE_MASK_ARGCOUNT | MPEXPR_TYPE_PAIRWISE)) \489== (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_PAIRWISE))490491if (IS_PAIRWISE (CP->op->type) && CP->argcount >= 2)492{493TRACE (printf (" will reduce pairwise now\n"));494CP->argcount--;495CONTROL_PUSH (CP->op, 2);496goto apply_control;497}498499CP->argcount++;500goto another_expr_lookahead;501}502503switch (p->token_op->type & MPEXPR_TYPE_MASK_ARGCOUNT) {504case MPEXPR_TYPE_NARY(1):505/* Postfix unary operators can always be applied immediately. The506easiest way to do this is just push it on the control stack and go507to the normal control stack reduction code. */508509TRACE (printf ("postfix unary operator: %s\n", p->token_op->name));510if (p->token_op->type & MPEXPR_TYPE_PREFIX)511ERROR ("prefix unary operator used postfix",512MPEXPR_RESULT_PARSE_ERROR);513CONTROL_PUSH (p->token_op, 1);514goto apply_control_lookahead;515516case MPEXPR_TYPE_NARY(2):517CONTROL_PUSH (p->token_op, 2);518goto another_expr_lookahead;519520case MPEXPR_TYPE_NARY(3):521CONTROL_PUSH (p->token_op, 1);522goto another_expr_lookahead;523}524525TRACE (printf ("unrecognised operator \"%s\" type: 0x%X",526CP->op->name, CP->op->type));527ERROR ("", MPEXPR_RESULT_PARSE_ERROR);528break;529530default:531TRACE (printf ("expecting an operator, got token %d", p->token));532ERROR ("", MPEXPR_RESULT_PARSE_ERROR);533}534535536apply_control_lookahead:537LOOKAHEAD (0);538apply_control:539/* Apply the top element CP of the control stack. Data values are SP,540SP-1, etc. Result is left as stack top SP after popping consumed541values.542543The use of sp as a duplicate of SP will help compilers that can't544otherwise recognise the various uses of SP as common subexpressions. */545546TRACE (printf ("apply control: nested %d, \"%s\" 0x%X, %d args\n",547p->control_top, CP->op->name, CP->op->type, CP->argcount));548549TRACE (printf ("apply 0x%X-ary\n",550CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT));551switch (CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) {552case MPEXPR_TYPE_NARY(0):553{554mpX_ptr sp;555DATA_SPACE ();556DATA_PUSH ();557sp = SP;558switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {559case 0:560(* (mpexpr_fun_0ary_t) CP->op->fun) (sp);561break;562case MPEXPR_TYPE_RESULT_INT:563(*p->mpX_set_si) (sp, (long) (* (mpexpr_fun_i_0ary_t) CP->op->fun) ());564break;565default:566ERROR ("unrecognised 0ary argument calling style",567MPEXPR_RESULT_BAD_TABLE);568}569}570break;571572case MPEXPR_TYPE_NARY(1):573{574mpX_ptr sp = SP;575CHECK_ARGCOUNT ("unary", 1);576TRACE (MPX_TRACE ("before", sp));577578switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) {579case 0:580/* not a special */581break;582583case MPEXPR_TYPE_DONE & MPEXPR_TYPE_MASK_SPECIAL:584TRACE (printf ("special done\n"));585goto done;586587case MPEXPR_TYPE_LOGICAL_NOT & MPEXPR_TYPE_MASK_SPECIAL:588TRACE (printf ("special logical not\n"));589(*p->mpX_set_si)590(sp, (long) ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) == 0));591goto apply_control_done;592593case MPEXPR_TYPE_CLOSEPAREN & MPEXPR_TYPE_MASK_SPECIAL:594CONTROL_POP ();595if (CP->op->type == MPEXPR_TYPE_OPENPAREN)596{597TRACE (printf ("close paren matching open paren\n"));598CONTROL_POP ();599goto another_operator;600}601if (CP->op->precedence == 0)602{603TRACE (printf ("close paren for function\n"));604goto apply_control;605}606ERROR ("unexpected close paren", MPEXPR_RESULT_PARSE_ERROR);607608default:609TRACE (printf ("unrecognised special unary operator 0x%X",610CP->op->type & MPEXPR_TYPE_MASK_SPECIAL));611ERROR ("", MPEXPR_RESULT_BAD_TABLE);612}613614switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {615case 0:616(* (mpexpr_fun_unary_t) CP->op->fun) (sp, sp);617break;618case MPEXPR_TYPE_LAST_UI:619CHECK_UI (sp);620(* (mpexpr_fun_unary_ui_t) CP->op->fun)621(sp, (*p->mpX_get_ui) (sp));622break;623case MPEXPR_TYPE_RESULT_INT:624(*p->mpX_set_si)625(sp, (long) (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp));626break;627case MPEXPR_TYPE_RESULT_INT | MPEXPR_TYPE_LAST_UI:628CHECK_UI (sp);629(*p->mpX_set_si)630(sp,631(long) (* (mpexpr_fun_i_unary_ui_t) CP->op->fun)632((*p->mpX_get_ui) (sp)));633break;634default:635ERROR ("unrecognised unary argument calling style",636MPEXPR_RESULT_BAD_TABLE);637}638}639break;640641case MPEXPR_TYPE_NARY(2):642{643mpX_ptr sp;644645/* pairwise functions are allowed to have just one argument */646if ((CP->op->type & MPEXPR_TYPE_PAIRWISE)647&& CP->op->precedence == 0648&& CP->argcount == 1)649goto apply_control_done;650651CHECK_ARGCOUNT ("binary", 2);652DATA_POP (1);653sp = SP;654TRACE (MPX_TRACE ("lhs", sp);655MPX_TRACE ("rhs", sp+1));656657if (CP->op->type & MPEXPR_TYPE_MASK_CMP)658{659int type = CP->op->type;660int cmp = (* (mpexpr_fun_i_binary_t) CP->op->fun)661(sp, sp+1);662(*p->mpX_set_si)663(sp,664(long)665(( (cmp < 0) & ((type & MPEXPR_TYPE_MASK_CMP_LT) != 0))666| ((cmp == 0) & ((type & MPEXPR_TYPE_MASK_CMP_EQ) != 0))667| ((cmp > 0) & ((type & MPEXPR_TYPE_MASK_CMP_GT) != 0))));668goto apply_control_done;669}670671switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) {672case 0:673/* not a special */674break;675676case MPEXPR_TYPE_QUESTION & MPEXPR_TYPE_MASK_SPECIAL:677ERROR ("'?' without ':'", MPEXPR_RESULT_PARSE_ERROR);678679case MPEXPR_TYPE_COLON & MPEXPR_TYPE_MASK_SPECIAL:680TRACE (printf ("special colon\n"));681CONTROL_POP ();682if (CP->op->type != MPEXPR_TYPE_QUESTION)683ERROR ("':' without '?'", MPEXPR_RESULT_PARSE_ERROR);684685CP->argcount--;686DATA_POP (1);687sp--;688TRACE (MPX_TRACE ("query", sp);689MPX_TRACE ("true", sp+1);690MPX_TRACE ("false", sp+2));691(*p->mpX_set)692(sp, (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)693? sp+1 : sp+2);694goto apply_control_done;695696case MPEXPR_TYPE_LOGICAL_AND & MPEXPR_TYPE_MASK_SPECIAL:697TRACE (printf ("special logical and\n"));698(*p->mpX_set_si)699(sp,700(long)701((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)702&& (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1)));703goto apply_control_done;704705case MPEXPR_TYPE_LOGICAL_OR & MPEXPR_TYPE_MASK_SPECIAL:706TRACE (printf ("special logical and\n"));707(*p->mpX_set_si)708(sp,709(long)710((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)711|| (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1)));712goto apply_control_done;713714case MPEXPR_TYPE_MAX & MPEXPR_TYPE_MASK_SPECIAL:715TRACE (printf ("special max\n"));716if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) < 0)717(*p->mpX_swap) (sp, sp+1);718goto apply_control_done;719case MPEXPR_TYPE_MIN & MPEXPR_TYPE_MASK_SPECIAL:720TRACE (printf ("special min\n"));721if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) > 0)722(*p->mpX_swap) (sp, sp+1);723goto apply_control_done;724725default:726ERROR ("unrecognised special binary operator",727MPEXPR_RESULT_BAD_TABLE);728}729730switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {731case 0:732(* (mpexpr_fun_binary_t) CP->op->fun) (sp, sp, sp+1);733break;734case MPEXPR_TYPE_LAST_UI:735CHECK_UI (sp+1);736(* (mpexpr_fun_binary_ui_t) CP->op->fun)737(sp, sp, (*p->mpX_get_ui) (sp+1));738break;739case MPEXPR_TYPE_RESULT_INT:740(*p->mpX_set_si)741(sp,742(long) (* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1));743break;744case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT:745CHECK_UI (sp+1);746(*p->mpX_set_si)747(sp,748(long) (* (mpexpr_fun_i_binary_ui_t) CP->op->fun)749(sp, (*p->mpX_get_ui) (sp+1)));750break;751default:752ERROR ("unrecognised binary argument calling style",753MPEXPR_RESULT_BAD_TABLE);754}755}756break;757758case MPEXPR_TYPE_NARY(3):759{760mpX_ptr sp;761762CHECK_ARGCOUNT ("ternary", 3);763DATA_POP (2);764sp = SP;765TRACE (MPX_TRACE ("arg1", sp);766MPX_TRACE ("arg2", sp+1);767MPX_TRACE ("arg3", sp+1));768769switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {770case 0:771(* (mpexpr_fun_ternary_t) CP->op->fun) (sp, sp, sp+1, sp+2);772break;773case MPEXPR_TYPE_LAST_UI:774CHECK_UI (sp+2);775(* (mpexpr_fun_ternary_ui_t) CP->op->fun)776(sp, sp, sp+1, (*p->mpX_get_ui) (sp+2));777break;778case MPEXPR_TYPE_RESULT_INT:779(*p->mpX_set_si)780(sp,781(long) (* (mpexpr_fun_i_ternary_t) CP->op->fun)782(sp, sp+1, sp+2));783break;784case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT:785CHECK_UI (sp+2);786(*p->mpX_set_si)787(sp,788(long) (* (mpexpr_fun_i_ternary_ui_t) CP->op->fun)789(sp, sp+1, (*p->mpX_get_ui) (sp+2)));790break;791default:792ERROR ("unrecognised binary argument calling style",793MPEXPR_RESULT_BAD_TABLE);794}795}796break;797798default:799TRACE (printf ("unrecognised operator type: 0x%X\n", CP->op->type));800ERROR ("", MPEXPR_RESULT_PARSE_ERROR);801}802803apply_control_done:804TRACE (MPX_TRACE ("result", SP));805CONTROL_POP ();806goto another_operator;807808done:809if (p->error_code == MPEXPR_RESULT_OK)810{811if (p->data_top != 0)812{813TRACE (printf ("data stack want top at 0, got %d\n", p->data_top));814p->error_code = MPEXPR_RESULT_PARSE_ERROR;815}816else817(*p->mpX_set_or_swap) (p->res, SP);818}819820{821int i;822for (i = 0; i < p->data_inited; i++)823{824TRACE (printf ("clear %d\n", i));825(*p->mpX_clear) (p->data_stack+i);826}827}828829FREE_FUNC_TYPE (p->data_stack, p->data_alloc, union mpX_t);830FREE_FUNC_TYPE (p->control_stack, p->control_alloc, struct mpexpr_control_t);831832return p->error_code;833}834835836