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, 2001, 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 the GNU Lesser General Public License as published by8the Free Software Foundation; either version 2.1 of the License, or (at your9option) any later version.1011The GNU MP Library is distributed in the hope that it will be useful, but12WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY13or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public14License for more details.1516You should have received a copy of the GNU Lesser General Public License17along with the GNU MP Library; see the file COPYING.LIB. If not, write to18the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,19MA 02110-1301, USA. */2021#include <ctype.h>22#include <stdio.h>23#include <string.h>2425#include "gmp.h"26#include "expr-impl.h"272829/* Change this to "#define TRACE(x) x" to get some traces. The trace30printfs junk up the code a bit, but it's very hard to tell what's going31on without them. Set MPX_TRACE to a suitable output function for the32mpz/mpq/mpf being run (if you have the wrong trace function it'll33probably segv). */3435#define TRACE(x)36#define MPX_TRACE mpz_trace373839/* A few helper macros copied from gmp-impl.h */40#define ALLOCATE_FUNC_TYPE(n,type) \41((type *) (*allocate_func) ((n) * sizeof (type)))42#define ALLOCATE_FUNC_LIMBS(n) ALLOCATE_FUNC_TYPE (n, mp_limb_t)43#define REALLOCATE_FUNC_TYPE(p, old_size, new_size, type) \44((type *) (*reallocate_func) \45(p, (old_size) * sizeof (type), (new_size) * sizeof (type)))46#define REALLOCATE_FUNC_LIMBS(p, old_size, new_size) \47REALLOCATE_FUNC_TYPE(p, old_size, new_size, mp_limb_t)48#define FREE_FUNC_TYPE(p,n,type) (*free_func) (p, (n) * sizeof (type))49#define FREE_FUNC_LIMBS(p,n) FREE_FUNC_TYPE (p, n, mp_limb_t)50#define ASSERT(x)51525354/* All the error strings are just for diagnostic traces. Only the error55code is actually returned. */56#define ERROR(str,code) \57{ \58TRACE (printf ("%s\n", str)); \59p->error_code = (code); \60goto done; \61}626364#define REALLOC(ptr, alloc, incr, type) \65do { \66int new_alloc = (alloc) + (incr); \67ptr = REALLOCATE_FUNC_TYPE (ptr, alloc, new_alloc, type); \68(alloc) = new_alloc; \69} while (0)707172/* data stack top element */73#define SP (p->data_stack + p->data_top)7475/* Make sure there's room for another data element above current top.76reallocate_func is fetched for when this macro is used in lookahead(). */77#define DATA_SPACE() \78do { \79if (p->data_top + 1 >= p->data_alloc) \80{ \81void *(*reallocate_func) (void *, size_t, size_t); \82mp_get_memory_functions (NULL, &reallocate_func, NULL); \83TRACE (printf ("grow stack from %d\n", p->data_alloc)); \84REALLOC (p->data_stack, p->data_alloc, 20, union mpX_t); \85} \86ASSERT (p->data_top + 1 <= p->data_inited); \87if (p->data_top + 1 == p->data_inited) \88{ \89TRACE (printf ("initialize %d\n", p->data_top + 1)); \90(*p->mpX_init) (&p->data_stack[p->data_top + 1], p->prec); \91p->data_inited++; \92} \93} while (0)9495#define DATA_PUSH() \96do { \97p->data_top++; \98ASSERT (p->data_top < p->data_alloc); \99ASSERT (p->data_top < p->data_inited); \100} while (0)101102/* the last stack entry is never popped, so top>=0 will be true */103#define DATA_POP(n) \104do { \105p->data_top -= (n); \106ASSERT (p->data_top >= 0); \107} while (0)108109110/* lookahead() parses the next token. Return 1 if successful, with some111extra data. Return 0 if fail, with reason in p->error_code.112113"prefix" is MPEXPR_TYPE_PREFIX if an operator with that attribute is114preferred, or 0 if an operator without is preferred. */115116#define TOKEN_EOF -1 /* no extra data */117#define TOKEN_VALUE -2 /* pushed onto data stack */118#define TOKEN_OPERATOR -3 /* stored in p->token_op */119#define TOKEN_FUNCTION -4 /* stored in p->token_op */120121#define TOKEN_NAME(n) \122((n) == TOKEN_EOF ? "TOKEN_EOF" \123: (n) == TOKEN_VALUE ? "TOKEN_VALUE" \124: (n) == TOKEN_OPERATOR ? "TOKEN_OPERATOR" \125: (n) == TOKEN_VALUE ? "TOKEN_FUNCTION" \126: "UNKNOWN TOKEN")127128/* Functions default to being parsed as whole words, operators to match just129at the start of the string. The type flags override this. */130#define WHOLEWORD(op) \131(op->precedence == 0 \132? (! (op->type & MPEXPR_TYPE_OPERATOR)) \133: (op->type & MPEXPR_TYPE_WHOLEWORD))134135#define isasciispace(c) (isascii (c) && isspace (c))136137static int138lookahead (struct mpexpr_parse_t *p, int prefix)139{140__gmp_const struct mpexpr_operator_t *op, *op_found;141size_t oplen, oplen_found, wlen;142int i;143144/* skip white space */145while (p->elen > 0 && isasciispace (*p->e))146p->e++, p->elen--;147148if (p->elen == 0)149{150TRACE (printf ("lookahead EOF\n"));151p->token = TOKEN_EOF;152return 1;153}154155DATA_SPACE ();156157/* Get extent of whole word. */158for (wlen = 0; wlen < p->elen; wlen++)159if (! isasciicsym (p->e[wlen]))160break;161162TRACE (printf ("lookahead at: \"%.*s\" length %u, word %u\n",163(int) p->elen, p->e, p->elen, wlen));164165op_found = NULL;166oplen_found = 0;167for (op = p->table; op->name != NULL; op++)168{169if (op->type == MPEXPR_TYPE_NEW_TABLE)170{171printf ("new\n");172op = (struct mpexpr_operator_t *) op->name - 1;173continue;174}175176oplen = strlen (op->name);177if (! ((WHOLEWORD (op) ? wlen == oplen : p->elen >= oplen)178&& memcmp (p->e, op->name, oplen) == 0))179continue;180181/* Shorter matches don't replace longer previous ones. */182if (op_found && oplen < oplen_found)183continue;184185/* On a match of equal length to a previous one, the old match isn't186replaced if it has the preferred prefix, and if it doesn't then187it's not replaced if the new one also doesn't. */188if (op_found && oplen == oplen_found189&& ((op_found->type & MPEXPR_TYPE_PREFIX) == prefix190|| (op->type & MPEXPR_TYPE_PREFIX) != prefix))191continue;192193/* This is now either the first match seen, or a longer than previous194match, or an equal to previous one but with a preferred prefix. */195op_found = op;196oplen_found = oplen;197}198199if (op_found)200{201p->e += oplen_found, p->elen -= oplen_found;202203if (op_found->type == MPEXPR_TYPE_VARIABLE)204{205if (p->elen == 0)206ERROR ("end of string expecting a variable",207MPEXPR_RESULT_PARSE_ERROR);208i = p->e[0] - 'a';209if (i < 0 || i >= MPEXPR_VARIABLES)210ERROR ("bad variable name", MPEXPR_RESULT_BAD_VARIABLE);211goto variable;212}213214if (op_found->precedence == 0)215{216TRACE (printf ("lookahead function: %s\n", op_found->name));217p->token = TOKEN_FUNCTION;218p->token_op = op_found;219return 1;220}221else222{223TRACE (printf ("lookahead operator: %s\n", op_found->name));224p->token = TOKEN_OPERATOR;225p->token_op = op_found;226return 1;227}228}229230oplen = (*p->mpX_number) (SP+1, p->e, p->elen, p->base);231if (oplen != 0)232{233p->e += oplen, p->elen -= oplen;234p->token = TOKEN_VALUE;235DATA_PUSH ();236TRACE (MPX_TRACE ("lookahead number", SP));237return 1;238}239240/* Maybe an unprefixed one character variable */241i = p->e[0] - 'a';242if (wlen == 1 && i >= 0 && i < MPEXPR_VARIABLES)243{244variable:245p->e++, p->elen--;246if (p->var[i] == NULL)247ERROR ("NULL variable", MPEXPR_RESULT_BAD_VARIABLE);248TRACE (printf ("lookahead variable: var[%d] = ", i);249MPX_TRACE ("", p->var[i]));250p->token = TOKEN_VALUE;251DATA_PUSH ();252(*p->mpX_set) (SP, p->var[i]);253return 1;254}255256ERROR ("no token matched", MPEXPR_RESULT_PARSE_ERROR);257258done:259return 0;260}261262263/* control stack current top element */264#define CP (p->control_stack + p->control_top)265266/* make sure there's room for another control element above current top */267#define CONTROL_SPACE() \268do { \269if (p->control_top + 1 >= p->control_alloc) \270{ \271TRACE (printf ("grow control stack from %d\n", p->control_alloc)); \272REALLOC (p->control_stack, p->control_alloc, 20, \273struct mpexpr_control_t); \274} \275} while (0)276277/* Push an operator on the control stack, claiming currently to have the278given number of args ready. Local variable "op" is used in case opptr is279a reference through CP. */280#define CONTROL_PUSH(opptr,args) \281do { \282__gmp_const struct mpexpr_operator_t *op = opptr; \283struct mpexpr_control_t *cp; \284CONTROL_SPACE (); \285p->control_top++; \286ASSERT (p->control_top < p->control_alloc); \287cp = CP; \288cp->op = op; \289cp->argcount = (args); \290TRACE_CONTROL("control stack push:"); \291} while (0)292293/* The special operator_done is never popped, so top>=0 will hold. */294#define CONTROL_POP() \295do { \296p->control_top--; \297ASSERT (p->control_top >= 0); \298TRACE_CONTROL ("control stack pop:"); \299} while (0)300301#define TRACE_CONTROL(str) \302TRACE ({ \303int i; \304printf ("%s depth %d:", str, p->control_top); \305for (i = 0; i <= p->control_top; i++) \306printf (" \"%s\"(%d)", \307p->control_stack[i].op->name, \308p->control_stack[i].argcount); \309printf ("\n"); \310});311312313#define LOOKAHEAD(prefix) \314do { \315if (! lookahead (p, prefix)) \316goto done; \317} while (0)318319#define CHECK_UI(n) \320do { \321if (! (*p->mpX_ulong_p) (n)) \322ERROR ("operand doesn't fit ulong", MPEXPR_RESULT_NOT_UI); \323} while (0)324325#define CHECK_ARGCOUNT(str,n) \326do { \327if (CP->argcount != (n)) \328{ \329TRACE (printf ("wrong number of arguments for %s, got %d want %d", \330str, CP->argcount, n)); \331ERROR ("", MPEXPR_RESULT_PARSE_ERROR); \332} \333} while (0)334335336/* There's two basic states here. In both p->token is the next token.337338"another_expr" is when a whole expression should be parsed. This means a339literal or variable value possibly followed by an operator, or a function340or prefix operator followed by a further whole expression.341342"another_operator" is when an expression has been parsed and its value is343on the top of the data stack (SP) and an optional further postfix or344infix operator should be parsed.345346In "another_operator" precedences determine whether to push the operator347onto the control stack, or instead go to "apply_control" to reduce the348operator currently on top of the control stack.349350When an operator has both a prefix and postfix/infix form, a LOOKAHEAD()351for "another_expr" will seek the prefix form, a LOOKAHEAD() for352"another_operator" will seek the postfix/infix form. The grammar is353simple enough that the next state is known before reading the next token.354355Argument count checking guards against functions consuming the wrong356number of operands from the data stack. The same checks are applied to357operators, but will always pass since a UNARY or BINARY will only ever358parse with the correct operands. */359360int361mpexpr_evaluate (struct mpexpr_parse_t *p)362{363void *(*allocate_func) (size_t);364void *(*reallocate_func) (void *, size_t, size_t);365void (*free_func) (void *, size_t);366367mp_get_memory_functions (&allocate_func, &reallocate_func, &free_func);368369TRACE (printf ("mpexpr_evaluate() base %d \"%.*s\"\n",370p->base, (int) p->elen, p->e));371372/* "done" is a special sentinel at the bottom of the control stack,373precedence -1 is lower than any normal operator. */374{375static __gmp_const struct mpexpr_operator_t operator_done376= { "DONE", NULL, MPEXPR_TYPE_DONE, -1 };377378p->control_alloc = 20;379p->control_stack = ALLOCATE_FUNC_TYPE (p->control_alloc,380struct mpexpr_control_t);381p->control_top = 0;382CP->op = &operator_done;383CP->argcount = 1;384}385386p->data_inited = 0;387p->data_alloc = 20;388p->data_stack = ALLOCATE_FUNC_TYPE (p->data_alloc, union mpX_t);389p->data_top = -1;390391p->error_code = MPEXPR_RESULT_OK;392393394another_expr_lookahead:395LOOKAHEAD (MPEXPR_TYPE_PREFIX);396TRACE (printf ("another expr\n"));397398/*another_expr:*/399switch (p->token) {400case TOKEN_VALUE:401goto another_operator_lookahead;402403case TOKEN_OPERATOR:404TRACE (printf ("operator %s\n", p->token_op->name));405if (! (p->token_op->type & MPEXPR_TYPE_PREFIX))406ERROR ("expected a prefix operator", MPEXPR_RESULT_PARSE_ERROR);407408CONTROL_PUSH (p->token_op, 1);409goto another_expr_lookahead;410411case TOKEN_FUNCTION:412CONTROL_PUSH (p->token_op, 1);413414if (p->token_op->type & MPEXPR_TYPE_CONSTANT)415goto apply_control_lookahead;416417LOOKAHEAD (MPEXPR_TYPE_PREFIX);418if (! (p->token == TOKEN_OPERATOR419&& p->token_op->type == MPEXPR_TYPE_OPENPAREN))420ERROR ("expected open paren for function", MPEXPR_RESULT_PARSE_ERROR);421422TRACE (printf ("open paren for function \"%s\"\n", CP->op->name));423424if ((CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) == MPEXPR_TYPE_NARY(0))425{426LOOKAHEAD (0);427if (! (p->token == TOKEN_OPERATOR428&& p->token_op->type == MPEXPR_TYPE_CLOSEPAREN))429ERROR ("expected close paren for 0ary function",430MPEXPR_RESULT_PARSE_ERROR);431goto apply_control_lookahead;432}433434goto another_expr_lookahead;435}436ERROR ("unrecognised start of expression", MPEXPR_RESULT_PARSE_ERROR);437438439another_operator_lookahead:440LOOKAHEAD (0);441another_operator:442TRACE (printf ("another operator maybe: %s\n", TOKEN_NAME(p->token)));443444switch (p->token) {445case TOKEN_EOF:446goto apply_control;447448case TOKEN_OPERATOR:449/* The next operator is compared to the one on top of the control stack.450If the next is lower precedence, or the same precedence and not451right-associative, then reduce using the control stack and look at452the next operator again later. */453454#define PRECEDENCE_TEST_REDUCE(tprec,cprec,ttype,ctype) \455((tprec) < (cprec) \456|| ((tprec) == (cprec) && ! ((ttype) & MPEXPR_TYPE_RIGHTASSOC)))457458if (PRECEDENCE_TEST_REDUCE (p->token_op->precedence, CP->op->precedence,459p->token_op->type, CP->op->type))460{461TRACE (printf ("defer operator: %s (prec %d vs %d, type 0x%X)\n",462p->token_op->name,463p->token_op->precedence, CP->op->precedence,464p->token_op->type));465goto apply_control;466}467468/* An argsep is a binary operator, but is never pushed on the control469stack, it just accumulates an extra argument for a function. */470if (p->token_op->type == MPEXPR_TYPE_ARGSEP)471{472if (CP->op->precedence != 0)473ERROR ("ARGSEP not in a function call", MPEXPR_RESULT_PARSE_ERROR);474475TRACE (printf ("argsep for function \"%s\"(%d)\n",476CP->op->name, CP->argcount));477478#define IS_PAIRWISE(type) \479(((type) & (MPEXPR_TYPE_MASK_ARGCOUNT | MPEXPR_TYPE_PAIRWISE)) \480== (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_PAIRWISE))481482if (IS_PAIRWISE (CP->op->type) && CP->argcount >= 2)483{484TRACE (printf (" will reduce pairwise now\n"));485CP->argcount--;486CONTROL_PUSH (CP->op, 2);487goto apply_control;488}489490CP->argcount++;491goto another_expr_lookahead;492}493494switch (p->token_op->type & MPEXPR_TYPE_MASK_ARGCOUNT) {495case MPEXPR_TYPE_NARY(1):496/* Postfix unary operators can always be applied immediately. The497easiest way to do this is just push it on the control stack and go498to the normal control stack reduction code. */499500TRACE (printf ("postfix unary operator: %s\n", p->token_op->name));501if (p->token_op->type & MPEXPR_TYPE_PREFIX)502ERROR ("prefix unary operator used postfix",503MPEXPR_RESULT_PARSE_ERROR);504CONTROL_PUSH (p->token_op, 1);505goto apply_control_lookahead;506507case MPEXPR_TYPE_NARY(2):508CONTROL_PUSH (p->token_op, 2);509goto another_expr_lookahead;510511case MPEXPR_TYPE_NARY(3):512CONTROL_PUSH (p->token_op, 1);513goto another_expr_lookahead;514}515516TRACE (printf ("unrecognised operator \"%s\" type: 0x%X",517CP->op->name, CP->op->type));518ERROR ("", MPEXPR_RESULT_PARSE_ERROR);519break;520521default:522TRACE (printf ("expecting an operator, got token %d", p->token));523ERROR ("", MPEXPR_RESULT_PARSE_ERROR);524}525526527apply_control_lookahead:528LOOKAHEAD (0);529apply_control:530/* Apply the top element CP of the control stack. Data values are SP,531SP-1, etc. Result is left as stack top SP after popping consumed532values.533534The use of sp as a duplicate of SP will help compilers that can't535otherwise recognise the various uses of SP as common subexpressions. */536537TRACE (printf ("apply control: nested %d, \"%s\" 0x%X, %d args\n",538p->control_top, CP->op->name, CP->op->type, CP->argcount));539540TRACE (printf ("apply 0x%X-ary\n",541CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT));542switch (CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) {543case MPEXPR_TYPE_NARY(0):544{545mpX_ptr sp;546DATA_SPACE ();547DATA_PUSH ();548sp = SP;549switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {550case 0:551(* (mpexpr_fun_0ary_t) CP->op->fun) (sp);552break;553case MPEXPR_TYPE_RESULT_INT:554(*p->mpX_set_si) (sp, (long) (* (mpexpr_fun_i_0ary_t) CP->op->fun) ());555break;556default:557ERROR ("unrecognised 0ary argument calling style",558MPEXPR_RESULT_BAD_TABLE);559}560}561break;562563case MPEXPR_TYPE_NARY(1):564{565mpX_ptr sp = SP;566CHECK_ARGCOUNT ("unary", 1);567TRACE (MPX_TRACE ("before", sp));568569switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) {570case 0:571/* not a special */572break;573574case MPEXPR_TYPE_DONE & MPEXPR_TYPE_MASK_SPECIAL:575TRACE (printf ("special done\n"));576goto done;577578case MPEXPR_TYPE_LOGICAL_NOT & MPEXPR_TYPE_MASK_SPECIAL:579TRACE (printf ("special logical not\n"));580(*p->mpX_set_si)581(sp, (long) ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) == 0));582goto apply_control_done;583584case MPEXPR_TYPE_CLOSEPAREN & MPEXPR_TYPE_MASK_SPECIAL:585CONTROL_POP ();586if (CP->op->type == MPEXPR_TYPE_OPENPAREN)587{588TRACE (printf ("close paren matching open paren\n"));589CONTROL_POP ();590goto another_operator;591}592if (CP->op->precedence == 0)593{594TRACE (printf ("close paren for function\n"));595goto apply_control;596}597ERROR ("unexpected close paren", MPEXPR_RESULT_PARSE_ERROR);598599default:600TRACE (printf ("unrecognised special unary operator 0x%X",601CP->op->type & MPEXPR_TYPE_MASK_SPECIAL));602ERROR ("", MPEXPR_RESULT_BAD_TABLE);603}604605switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {606case 0:607(* (mpexpr_fun_unary_t) CP->op->fun) (sp, sp);608break;609case MPEXPR_TYPE_LAST_UI:610CHECK_UI (sp);611(* (mpexpr_fun_unary_ui_t) CP->op->fun)612(sp, (*p->mpX_get_ui) (sp));613break;614case MPEXPR_TYPE_RESULT_INT:615(*p->mpX_set_si)616(sp, (long) (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp));617break;618case MPEXPR_TYPE_RESULT_INT | MPEXPR_TYPE_LAST_UI:619CHECK_UI (sp);620(*p->mpX_set_si)621(sp,622(long) (* (mpexpr_fun_i_unary_ui_t) CP->op->fun)623((*p->mpX_get_ui) (sp)));624break;625default:626ERROR ("unrecognised unary argument calling style",627MPEXPR_RESULT_BAD_TABLE);628}629}630break;631632case MPEXPR_TYPE_NARY(2):633{634mpX_ptr sp;635636/* pairwise functions are allowed to have just one argument */637if ((CP->op->type & MPEXPR_TYPE_PAIRWISE)638&& CP->op->precedence == 0639&& CP->argcount == 1)640goto apply_control_done;641642CHECK_ARGCOUNT ("binary", 2);643DATA_POP (1);644sp = SP;645TRACE (MPX_TRACE ("lhs", sp);646MPX_TRACE ("rhs", sp+1));647648if (CP->op->type & MPEXPR_TYPE_MASK_CMP)649{650int type = CP->op->type;651int cmp = (* (mpexpr_fun_i_binary_t) CP->op->fun)652(sp, sp+1);653(*p->mpX_set_si)654(sp,655(long)656(( (cmp < 0) & ((type & MPEXPR_TYPE_MASK_CMP_LT) != 0))657| ((cmp == 0) & ((type & MPEXPR_TYPE_MASK_CMP_EQ) != 0))658| ((cmp > 0) & ((type & MPEXPR_TYPE_MASK_CMP_GT) != 0))));659goto apply_control_done;660}661662switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) {663case 0:664/* not a special */665break;666667case MPEXPR_TYPE_QUESTION & MPEXPR_TYPE_MASK_SPECIAL:668ERROR ("'?' without ':'", MPEXPR_RESULT_PARSE_ERROR);669670case MPEXPR_TYPE_COLON & MPEXPR_TYPE_MASK_SPECIAL:671TRACE (printf ("special colon\n"));672CONTROL_POP ();673if (CP->op->type != MPEXPR_TYPE_QUESTION)674ERROR ("':' without '?'", MPEXPR_RESULT_PARSE_ERROR);675676CP->argcount--;677DATA_POP (1);678sp--;679TRACE (MPX_TRACE ("query", sp);680MPX_TRACE ("true", sp+1);681MPX_TRACE ("false", sp+2));682(*p->mpX_set)683(sp, (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)684? sp+1 : sp+2);685goto apply_control_done;686687case MPEXPR_TYPE_LOGICAL_AND & MPEXPR_TYPE_MASK_SPECIAL:688TRACE (printf ("special logical and\n"));689(*p->mpX_set_si)690(sp,691(long)692((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)693&& (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1)));694goto apply_control_done;695696case MPEXPR_TYPE_LOGICAL_OR & 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_MAX & MPEXPR_TYPE_MASK_SPECIAL:706TRACE (printf ("special max\n"));707if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) < 0)708(*p->mpX_swap) (sp, sp+1);709goto apply_control_done;710case MPEXPR_TYPE_MIN & MPEXPR_TYPE_MASK_SPECIAL:711TRACE (printf ("special min\n"));712if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) > 0)713(*p->mpX_swap) (sp, sp+1);714goto apply_control_done;715716default:717ERROR ("unrecognised special binary operator",718MPEXPR_RESULT_BAD_TABLE);719}720721switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {722case 0:723(* (mpexpr_fun_binary_t) CP->op->fun) (sp, sp, sp+1);724break;725case MPEXPR_TYPE_LAST_UI:726CHECK_UI (sp+1);727(* (mpexpr_fun_binary_ui_t) CP->op->fun)728(sp, sp, (*p->mpX_get_ui) (sp+1));729break;730case MPEXPR_TYPE_RESULT_INT:731(*p->mpX_set_si)732(sp,733(long) (* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1));734break;735case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT:736CHECK_UI (sp+1);737(*p->mpX_set_si)738(sp,739(long) (* (mpexpr_fun_i_binary_ui_t) CP->op->fun)740(sp, (*p->mpX_get_ui) (sp+1)));741break;742default:743ERROR ("unrecognised binary argument calling style",744MPEXPR_RESULT_BAD_TABLE);745}746}747break;748749case MPEXPR_TYPE_NARY(3):750{751mpX_ptr sp;752753CHECK_ARGCOUNT ("ternary", 3);754DATA_POP (2);755sp = SP;756TRACE (MPX_TRACE ("arg1", sp);757MPX_TRACE ("arg2", sp+1);758MPX_TRACE ("arg3", sp+1));759760switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {761case 0:762(* (mpexpr_fun_ternary_t) CP->op->fun) (sp, sp, sp+1, sp+2);763break;764case MPEXPR_TYPE_LAST_UI:765CHECK_UI (sp+2);766(* (mpexpr_fun_ternary_ui_t) CP->op->fun)767(sp, sp, sp+1, (*p->mpX_get_ui) (sp+2));768break;769case MPEXPR_TYPE_RESULT_INT:770(*p->mpX_set_si)771(sp,772(long) (* (mpexpr_fun_i_ternary_t) CP->op->fun)773(sp, sp+1, sp+2));774break;775case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT:776CHECK_UI (sp+2);777(*p->mpX_set_si)778(sp,779(long) (* (mpexpr_fun_i_ternary_ui_t) CP->op->fun)780(sp, sp+1, (*p->mpX_get_ui) (sp+2)));781break;782default:783ERROR ("unrecognised binary argument calling style",784MPEXPR_RESULT_BAD_TABLE);785}786}787break;788789default:790TRACE (printf ("unrecognised operator type: 0x%X\n", CP->op->type));791ERROR ("", MPEXPR_RESULT_PARSE_ERROR);792}793794apply_control_done:795TRACE (MPX_TRACE ("result", SP));796CONTROL_POP ();797goto another_operator;798799done:800if (p->error_code == MPEXPR_RESULT_OK)801{802if (p->data_top != 0)803{804TRACE (printf ("data stack want top at 0, got %d\n", p->data_top));805p->error_code = MPEXPR_RESULT_PARSE_ERROR;806}807else808(*p->mpX_set_or_swap) (p->res, SP);809}810811{812int i;813for (i = 0; i < p->data_inited; i++)814{815TRACE (printf ("clear %d\n", i));816(*p->mpX_clear) (p->data_stack+i);817}818}819820FREE_FUNC_TYPE (p->data_stack, p->data_alloc, union mpX_t);821FREE_FUNC_TYPE (p->control_stack, p->control_alloc, struct mpexpr_control_t);822823return p->error_code;824}825826827