summaryrefslogtreecommitdiff
path: root/demos/expr/expr.c
blob: e87ba6087cb4a4dc98b4e5adcd38595b35cff5ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/* mpexpr_evaluate -- shared code for simple expression evaluation

Copyright 2000, 2001, 2002, 2004 Free Software Foundation, Inc.

This file is part of the GNU MP Library.

The GNU MP Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.

The GNU MP Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */

#include <ctype.h>
#include <stdio.h>
#include <string.h>

#include "gmp.h"
#include "expr-impl.h"


/* Change this to "#define TRACE(x) x" to get some traces.  The trace
   printfs junk up the code a bit, but it's very hard to tell what's going
   on without them.  Set MPX_TRACE to a suitable output function for the
   mpz/mpq/mpf being run (if you have the wrong trace function it'll
   probably segv).  */

#define TRACE(x) 
#define MPX_TRACE  mpz_trace


/* A few helper macros copied from gmp-impl.h */
#define ALLOCATE_FUNC_TYPE(n,type) \
  ((type *) (*allocate_func) ((n) * sizeof (type)))
#define ALLOCATE_FUNC_LIMBS(n)   ALLOCATE_FUNC_TYPE (n, mp_limb_t)
#define REALLOCATE_FUNC_TYPE(p, old_size, new_size, type) \
  ((type *) (*reallocate_func)                            \
   (p, (old_size) * sizeof (type), (new_size) * sizeof (type)))
#define REALLOCATE_FUNC_LIMBS(p, old_size, new_size) \
  REALLOCATE_FUNC_TYPE(p, old_size, new_size, mp_limb_t)
#define FREE_FUNC_TYPE(p,n,type) (*free_func) (p, (n) * sizeof (type))
#define FREE_FUNC_LIMBS(p,n)     FREE_FUNC_TYPE (p, n, mp_limb_t)
#define ASSERT(x)



/* All the error strings are just for diagnostic traces.  Only the error
   code is actually returned.  */
#define ERROR(str,code)                 \
  {                                     \
    TRACE (printf ("%s\n", str));       \
    p->error_code = (code);             \
    goto done;                          \
  }


#define REALLOC(ptr, alloc, incr, type)                         \
  do {                                                          \
    int  new_alloc = (alloc) + (incr);                          \
    ptr = REALLOCATE_FUNC_TYPE (ptr, alloc, new_alloc, type);   \
    (alloc) = new_alloc;                                        \
  } while (0)


/* data stack top element */
#define SP   (p->data_stack + p->data_top)

/* Make sure there's room for another data element above current top.
   reallocate_func is fetched for when this macro is used in lookahead(). */
#define DATA_SPACE()                                                    \
  do {                                                                  \
    if (p->data_top + 1 >= p->data_alloc)                               \
      {                                                                 \
        void *(*reallocate_func) (void *, size_t, size_t);              \
        mp_get_memory_functions (NULL, &reallocate_func, NULL);         \
        TRACE (printf ("grow stack from %d\n", p->data_alloc));         \
        REALLOC (p->data_stack, p->data_alloc, 20, union mpX_t);        \
      }                                                                 \
    ASSERT (p->data_top + 1 <= p->data_inited);                         \
    if (p->data_top + 1 == p->data_inited)                              \
      {                                                                 \
        TRACE (printf ("initialize %d\n", p->data_top + 1));            \
        (*p->mpX_init) (&p->data_stack[p->data_top + 1], p->prec);      \
        p->data_inited++;                                               \
      }                                                                 \
  } while (0)

#define DATA_PUSH()                             \
  do {                                          \
    p->data_top++;                              \
    ASSERT (p->data_top < p->data_alloc);       \
    ASSERT (p->data_top < p->data_inited);      \
  } while (0)

/* the last stack entry is never popped, so top>=0 will be true */
#define DATA_POP(n)             \
  do {                          \
    p->data_top -= (n);         \
    ASSERT (p->data_top >= 0);  \
  } while (0)


/* lookahead() parses the next token.  Return 1 if successful, with some
   extra data.  Return 0 if fail, with reason in p->error_code.

   "prefix" is MPEXPR_TYPE_PREFIX if an operator with that attribute is
   preferred, or 0 if an operator without is preferred. */

#define TOKEN_EOF         -1   /* no extra data */
#define TOKEN_VALUE       -2   /* pushed onto data stack */
#define TOKEN_OPERATOR    -3   /* stored in p->token_op */
#define TOKEN_FUNCTION    -4   /* stored in p->token_op */

#define TOKEN_NAME(n)                           \
  ((n) == TOKEN_EOF ? "TOKEN_EOF"               \
   : (n) == TOKEN_VALUE ? "TOKEN_VALUE"         \
   : (n) == TOKEN_OPERATOR ? "TOKEN_OPERATOR"   \
   : (n) == TOKEN_VALUE ? "TOKEN_FUNCTION"      \
   : "UNKNOWN TOKEN")

/* Functions default to being parsed as whole words, operators to match just
   at the start of the string.  The type flags override this. */
#define WHOLEWORD(op)                           \
  (op->precedence == 0                          \
   ? (! (op->type & MPEXPR_TYPE_OPERATOR))      \
   :   (op->type & MPEXPR_TYPE_WHOLEWORD))

#define isasciispace(c)   (isascii (c) && isspace (c))

static int
lookahead (struct mpexpr_parse_t *p, int prefix)
{
  __gmp_const struct mpexpr_operator_t  *op, *op_found;
  size_t  oplen, oplen_found, wlen;
  int     i;

  /* skip white space */
  while (p->elen > 0 && isasciispace (*p->e))
    p->e++, p->elen--;

  if (p->elen == 0)
    {
      TRACE (printf ("lookahead EOF\n"));
      p->token = TOKEN_EOF;
      return 1;
    }

  DATA_SPACE ();

  /* Get extent of whole word. */
  for (wlen = 0; wlen < p->elen; wlen++)
    if (! isasciicsym (p->e[wlen]))
      break;

  TRACE (printf ("lookahead at: \"%.*s\" length %u, word %u\n",
                 (int) p->elen, p->e, p->elen, wlen));

  op_found = NULL;
  oplen_found = 0;
  for (op = p->table; op->name != NULL; op++)
    {
      if (op->type == MPEXPR_TYPE_NEW_TABLE)
        {
          printf ("new\n");
          op = (struct mpexpr_operator_t *) op->name - 1;
          continue;
        }

      oplen = strlen (op->name);
      if (! ((WHOLEWORD (op) ? wlen == oplen : p->elen >= oplen)
             && memcmp (p->e, op->name, oplen) == 0))
        continue;

      /* Shorter matches don't replace longer previous ones. */
      if (op_found && oplen < oplen_found)
        continue;

      /* On a match of equal length to a previous one, the old match isn't
         replaced if it has the preferred prefix, and if it doesn't then
         it's not replaced if the new one also doesn't.  */
      if (op_found && oplen == oplen_found
          && ((op_found->type & MPEXPR_TYPE_PREFIX) == prefix
              || (op->type & MPEXPR_TYPE_PREFIX) != prefix))
        continue;

      /* This is now either the first match seen, or a longer than previous
         match, or an equal to previous one but with a preferred prefix. */
      op_found = op;
      oplen_found = oplen;
    }

  if (op_found)
    {
      p->e += oplen_found, p->elen -= oplen_found;

      if (op_found->type == MPEXPR_TYPE_VARIABLE)
        {
          if (p->elen == 0)
            ERROR ("end of string expecting a variable",
                   MPEXPR_RESULT_PARSE_ERROR);
          i = p->e[0] - 'a';
          if (i < 0 || i >= MPEXPR_VARIABLES)
            ERROR ("bad variable name", MPEXPR_RESULT_BAD_VARIABLE);
          goto variable;
        }

      if (op_found->precedence == 0)
        {
          TRACE (printf ("lookahead function: %s\n", op_found->name));
          p->token = TOKEN_FUNCTION;
          p->token_op = op_found;
          return 1;
        }
      else
        {
          TRACE (printf ("lookahead operator: %s\n", op_found->name));
          p->token = TOKEN_OPERATOR;
          p->token_op = op_found;
          return 1;
        }
    }

  oplen = (*p->mpX_number) (SP+1, p->e, p->elen, p->base);
  if (oplen != 0)
    {
      p->e += oplen, p->elen -= oplen;
      p->token = TOKEN_VALUE;
      DATA_PUSH ();
      TRACE (MPX_TRACE ("lookahead number", SP));
      return 1;
    }

  /* Maybe an unprefixed one character variable */
  i = p->e[0] - 'a';
  if (wlen == 1 && i >= 0 && i < MPEXPR_VARIABLES)
    {
    variable:
      p->e++, p->elen--;
      if (p->var[i] == NULL)
        ERROR ("NULL variable", MPEXPR_RESULT_BAD_VARIABLE);
      TRACE (printf ("lookahead variable: var[%d] = ", i);
             MPX_TRACE ("", p->var[i]));
      p->token = TOKEN_VALUE;
      DATA_PUSH ();
      (*p->mpX_set) (SP, p->var[i]);
      return 1;
    }

  ERROR ("no token matched", MPEXPR_RESULT_PARSE_ERROR);

 done:
  return 0;
}


/* control stack current top element */
#define CP   (p->control_stack + p->control_top)

/* make sure there's room for another control element above current top */
#define CONTROL_SPACE()                                                    \
  do {                                                                     \
    if (p->control_top + 1 >= p->control_alloc)                            \
      {                                                                    \
        TRACE (printf ("grow control stack from %d\n", p->control_alloc)); \
        REALLOC (p->control_stack, p->control_alloc, 20,                   \
                 struct mpexpr_control_t);                                 \
      }                                                                    \
  } while (0)

/* Push an operator on the control stack, claiming currently to have the
   given number of args ready.  Local variable "op" is used in case opptr is
   a reference through CP.  */
#define CONTROL_PUSH(opptr,args)                        \
  do {                                                  \
    __gmp_const struct mpexpr_operator_t *op = opptr;   \
    struct mpexpr_control_t *cp;                        \
    CONTROL_SPACE ();                                   \
    p->control_top++;                                   \
    ASSERT (p->control_top < p->control_alloc);         \
    cp = CP;                                            \
    cp->op = op;                                        \
    cp->argcount = (args);                              \
    TRACE_CONTROL("control stack push:");               \
  } while (0)

/* The special operator_done is never popped, so top>=0 will hold. */
#define CONTROL_POP()                           \
  do {                                          \
    p->control_top--;                           \
    ASSERT (p->control_top >= 0);               \
    TRACE_CONTROL ("control stack pop:");       \
  } while (0)

#define TRACE_CONTROL(str)                              \
  TRACE ({                                              \
    int  i;                                             \
    printf ("%s depth %d:", str, p->control_top);       \
    for (i = 0; i <= p->control_top; i++)               \
      printf (" \"%s\"(%d)",                            \
              p->control_stack[i].op->name,             \
              p->control_stack[i].argcount);            \
    printf ("\n");                                      \
  });


#define LOOKAHEAD(prefix)               \
  do {                                  \
    if (! lookahead (p, prefix))        \
      goto done;                        \
  } while (0)

#define CHECK_UI(n)                                                     \
  do {                                                                  \
    if (! (*p->mpX_ulong_p) (n))                                        \
      ERROR ("operand doesn't fit ulong", MPEXPR_RESULT_NOT_UI);        \
  } while (0)

#define CHECK_ARGCOUNT(str,n)                                              \
  do {                                                                     \
    if (CP->argcount != (n))                                               \
      {                                                                    \
        TRACE (printf ("wrong number of arguments for %s, got %d want %d", \
                       str, CP->argcount, n));                             \
        ERROR ("", MPEXPR_RESULT_PARSE_ERROR);                             \
      }                                                                    \
  } while (0)


/* There's two basic states here.  In both p->token is the next token.

   "another_expr" is when a whole expression should be parsed.  This means a
   literal or variable value possibly followed by an operator, or a function
   or prefix operator followed by a further whole expression.

   "another_operator" is when an expression has been parsed and its value is
   on the top of the data stack (SP) and an optional further postfix or
   infix operator should be parsed.

   In "another_operator" precedences determine whether to push the operator
   onto the control stack, or instead go to "apply_control" to reduce the
   operator currently on top of the control stack.

   When an operator has both a prefix and postfix/infix form, a LOOKAHEAD()
   for "another_expr" will seek the prefix form, a LOOKAHEAD() for
   "another_operator" will seek the postfix/infix form.  The grammar is
   simple enough that the next state is known before reading the next token.

   Argument count checking guards against functions consuming the wrong
   number of operands from the data stack.  The same checks are applied to
   operators, but will always pass since a UNARY or BINARY will only ever
   parse with the correct operands.  */

int
mpexpr_evaluate (struct mpexpr_parse_t *p)
{
  void *(*allocate_func) (size_t);
  void *(*reallocate_func) (void *, size_t, size_t);
  void (*free_func) (void *, size_t);

  mp_get_memory_functions (&allocate_func, &reallocate_func, &free_func);

  TRACE (printf ("mpexpr_evaluate() base %d \"%.*s\"\n",
                 p->base, (int) p->elen, p->e));

  /* "done" is a special sentinel at the bottom of the control stack,
     precedence -1 is lower than any normal operator.  */
  {
    static __gmp_const struct mpexpr_operator_t  operator_done
      = { "DONE", NULL, MPEXPR_TYPE_DONE, -1 };

    p->control_alloc = 20;
    p->control_stack = ALLOCATE_FUNC_TYPE (p->control_alloc,
                                           struct mpexpr_control_t);
    p->control_top = 0;
    CP->op = &operator_done;
    CP->argcount = 1;
  }

  p->data_inited = 0;
  p->data_alloc = 20;
  p->data_stack = ALLOCATE_FUNC_TYPE (p->data_alloc, union mpX_t);
  p->data_top = -1;

  p->error_code = MPEXPR_RESULT_OK;


 another_expr_lookahead:
  LOOKAHEAD (MPEXPR_TYPE_PREFIX);
  TRACE (printf ("another expr\n"));

  /*another_expr:*/
  switch (p->token) {
  case TOKEN_VALUE:
    goto another_operator_lookahead;

  case TOKEN_OPERATOR:
    TRACE (printf ("operator %s\n", p->token_op->name));
    if (! (p->token_op->type & MPEXPR_TYPE_PREFIX))
      ERROR ("expected a prefix operator", MPEXPR_RESULT_PARSE_ERROR);

    CONTROL_PUSH (p->token_op, 1);
    goto another_expr_lookahead;

  case TOKEN_FUNCTION:
    CONTROL_PUSH (p->token_op, 1);

    if (p->token_op->type & MPEXPR_TYPE_CONSTANT)
      goto apply_control_lookahead;

    LOOKAHEAD (MPEXPR_TYPE_PREFIX);
    if (! (p->token == TOKEN_OPERATOR
           && p->token_op->type == MPEXPR_TYPE_OPENPAREN))
      ERROR ("expected open paren for function", MPEXPR_RESULT_PARSE_ERROR);

    TRACE (printf ("open paren for function \"%s\"\n", CP->op->name));

    if ((CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) == MPEXPR_TYPE_NARY(0))
      {
        LOOKAHEAD (0);
        if (! (p->token == TOKEN_OPERATOR
               && p->token_op->type == MPEXPR_TYPE_CLOSEPAREN))
          ERROR ("expected close paren for 0ary function",
                 MPEXPR_RESULT_PARSE_ERROR);
        goto apply_control_lookahead;
      }

    goto another_expr_lookahead;
  }
  ERROR ("unrecognised start of expression", MPEXPR_RESULT_PARSE_ERROR);


 another_operator_lookahead:
  LOOKAHEAD (0);
 another_operator:
  TRACE (printf ("another operator maybe: %s\n", TOKEN_NAME(p->token)));

  switch (p->token) {
  case TOKEN_EOF:
    goto apply_control;

  case TOKEN_OPERATOR:
    /* The next operator is compared to the one on top of the control stack.
       If the next is lower precedence, or the same precedence and not
       right-associative, then reduce using the control stack and look at
       the next operator again later.  */

#define PRECEDENCE_TEST_REDUCE(tprec,cprec,ttype,ctype)                 \
    ((tprec) < (cprec)                                                  \
     || ((tprec) == (cprec) && ! ((ttype) & MPEXPR_TYPE_RIGHTASSOC)))

    if (PRECEDENCE_TEST_REDUCE (p->token_op->precedence, CP->op->precedence,
                                p->token_op->type,       CP->op->type))
      {
        TRACE (printf ("defer operator: %s (prec %d vs %d, type 0x%X)\n",
                       p->token_op->name,
                       p->token_op->precedence, CP->op->precedence,
                       p->token_op->type));
        goto apply_control;
      }

    /* An argsep is a binary operator, but is never pushed on the control
       stack, it just accumulates an extra argument for a function. */
    if (p->token_op->type == MPEXPR_TYPE_ARGSEP)
      {
        if (CP->op->precedence != 0)
          ERROR ("ARGSEP not in a function call", MPEXPR_RESULT_PARSE_ERROR);

        TRACE (printf ("argsep for function \"%s\"(%d)\n",
                       CP->op->name, CP->argcount));

#define IS_PAIRWISE(type)                                               \
        (((type) & (MPEXPR_TYPE_MASK_ARGCOUNT | MPEXPR_TYPE_PAIRWISE))  \
         == (MPEXPR_TYPE_BINARY | MPEXPR_TYPE_PAIRWISE))

        if (IS_PAIRWISE (CP->op->type) && CP->argcount >= 2)
          {
            TRACE (printf ("    will reduce pairwise now\n"));
            CP->argcount--;
            CONTROL_PUSH (CP->op, 2);
            goto apply_control;
          }

        CP->argcount++;
        goto another_expr_lookahead;
      }

    switch (p->token_op->type & MPEXPR_TYPE_MASK_ARGCOUNT) {
    case MPEXPR_TYPE_NARY(1):
      /* Postfix unary operators can always be applied immediately.  The
         easiest way to do this is just push it on the control stack and go
         to the normal control stack reduction code. */

      TRACE (printf ("postfix unary operator: %s\n", p->token_op->name));
      if (p->token_op->type & MPEXPR_TYPE_PREFIX)
        ERROR ("prefix unary operator used postfix",
               MPEXPR_RESULT_PARSE_ERROR);
      CONTROL_PUSH (p->token_op, 1);
      goto apply_control_lookahead;

    case MPEXPR_TYPE_NARY(2):
      CONTROL_PUSH (p->token_op, 2);
      goto another_expr_lookahead;

    case MPEXPR_TYPE_NARY(3):
      CONTROL_PUSH (p->token_op, 1);
      goto another_expr_lookahead;
    }

    TRACE (printf ("unrecognised operator \"%s\" type: 0x%X",
                   CP->op->name, CP->op->type));
    ERROR ("", MPEXPR_RESULT_PARSE_ERROR);
    break;

  default:
    TRACE (printf ("expecting an operator, got token %d", p->token));
    ERROR ("", MPEXPR_RESULT_PARSE_ERROR);
  }


 apply_control_lookahead:
  LOOKAHEAD (0);
 apply_control:
  /* Apply the top element CP of the control stack.  Data values are SP,
     SP-1, etc.  Result is left as stack top SP after popping consumed
     values.

     The use of sp as a duplicate of SP will help compilers that can't
     otherwise recognise the various uses of SP as common subexpressions.  */

  TRACE (printf ("apply control: nested %d, \"%s\" 0x%X, %d args\n",
                 p->control_top, CP->op->name, CP->op->type, CP->argcount));

  TRACE (printf ("apply 0x%X-ary\n",
                 CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT));
  switch (CP->op->type & MPEXPR_TYPE_MASK_ARGCOUNT) {
  case MPEXPR_TYPE_NARY(0):
    {
      mpX_ptr  sp;
      DATA_SPACE ();
      DATA_PUSH ();
      sp = SP;
      switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {
      case 0:
        (* (mpexpr_fun_0ary_t) CP->op->fun) (sp);
        break;
      case MPEXPR_TYPE_RESULT_INT:
        (*p->mpX_set_si) (sp, (long) (* (mpexpr_fun_i_0ary_t) CP->op->fun) ());
        break;
      default:
        ERROR ("unrecognised 0ary argument calling style",
               MPEXPR_RESULT_BAD_TABLE);
      }
    }
    break;

  case MPEXPR_TYPE_NARY(1):
    {
      mpX_ptr  sp = SP;
      CHECK_ARGCOUNT ("unary", 1);
      TRACE (MPX_TRACE ("before", sp));

      switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) {
      case 0:
        /* not a special */
        break;

      case MPEXPR_TYPE_DONE & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special done\n"));
        goto done;

      case MPEXPR_TYPE_LOGICAL_NOT & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special logical not\n"));
        (*p->mpX_set_si)
          (sp, (long) ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp) == 0));
        goto apply_control_done;

      case MPEXPR_TYPE_CLOSEPAREN & MPEXPR_TYPE_MASK_SPECIAL:
        CONTROL_POP ();
        if (CP->op->type == MPEXPR_TYPE_OPENPAREN)
          {
            TRACE (printf ("close paren matching open paren\n"));
            CONTROL_POP ();
            goto another_operator;
          }
        if (CP->op->precedence == 0)
          {
            TRACE (printf ("close paren for function\n"));
            goto apply_control;
          }
        ERROR ("unexpected close paren", MPEXPR_RESULT_PARSE_ERROR);

      default:
        TRACE (printf ("unrecognised special unary operator 0x%X",
                       CP->op->type & MPEXPR_TYPE_MASK_SPECIAL));
        ERROR ("", MPEXPR_RESULT_BAD_TABLE);
      }

      switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {
      case 0:
        (* (mpexpr_fun_unary_t) CP->op->fun) (sp, sp);
        break;
      case MPEXPR_TYPE_LAST_UI:
        CHECK_UI (sp);
        (* (mpexpr_fun_unary_ui_t) CP->op->fun)
          (sp, (*p->mpX_get_ui) (sp));
        break;
      case MPEXPR_TYPE_RESULT_INT:
        (*p->mpX_set_si)
          (sp, (long) (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp));
        break;
      case MPEXPR_TYPE_RESULT_INT | MPEXPR_TYPE_LAST_UI:
        CHECK_UI (sp);
        (*p->mpX_set_si)
          (sp,
           (long) (* (mpexpr_fun_i_unary_ui_t) CP->op->fun)
           ((*p->mpX_get_ui) (sp)));
        break;
      default:
        ERROR ("unrecognised unary argument calling style",
               MPEXPR_RESULT_BAD_TABLE);
      }
    }
    break;

  case MPEXPR_TYPE_NARY(2):
    {
      mpX_ptr  sp;

      /* pairwise functions are allowed to have just one argument */
      if ((CP->op->type & MPEXPR_TYPE_PAIRWISE)
          && CP->op->precedence == 0
          && CP->argcount == 1)
        goto apply_control_done;

      CHECK_ARGCOUNT ("binary", 2);
      DATA_POP (1);
      sp = SP;
      TRACE (MPX_TRACE ("lhs", sp);
             MPX_TRACE ("rhs", sp+1));

      if (CP->op->type & MPEXPR_TYPE_MASK_CMP)
        {
          int  type = CP->op->type;
          int  cmp = (* (mpexpr_fun_i_binary_t) CP->op->fun)
            (sp, sp+1);
          (*p->mpX_set_si)
            (sp,
             (long)
             ((  (cmp  < 0) & ((type & MPEXPR_TYPE_MASK_CMP_LT) != 0))
              | ((cmp == 0) & ((type & MPEXPR_TYPE_MASK_CMP_EQ) != 0))
              | ((cmp  > 0) & ((type & MPEXPR_TYPE_MASK_CMP_GT) != 0))));
          goto apply_control_done;
        }

      switch (CP->op->type & MPEXPR_TYPE_MASK_SPECIAL) {
      case 0:
        /* not a special */
        break;

      case MPEXPR_TYPE_QUESTION & MPEXPR_TYPE_MASK_SPECIAL:
        ERROR ("'?' without ':'", MPEXPR_RESULT_PARSE_ERROR);

      case MPEXPR_TYPE_COLON & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special colon\n"));
        CONTROL_POP ();
        if (CP->op->type != MPEXPR_TYPE_QUESTION)
          ERROR ("':' without '?'", MPEXPR_RESULT_PARSE_ERROR);

        CP->argcount--;
        DATA_POP (1);
        sp--;
        TRACE (MPX_TRACE ("query", sp);
               MPX_TRACE ("true",  sp+1);
               MPX_TRACE ("false", sp+2));
        (*p->mpX_set)
          (sp, (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)
           ? sp+1 : sp+2);
        goto apply_control_done;

      case MPEXPR_TYPE_LOGICAL_AND & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special logical and\n"));
        (*p->mpX_set_si)
          (sp,
           (long)
           ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)
            && (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1)));
        goto apply_control_done;

      case MPEXPR_TYPE_LOGICAL_OR & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special logical and\n"));
        (*p->mpX_set_si)
          (sp,
           (long)
           ((* (mpexpr_fun_i_unary_t) CP->op->fun) (sp)
            || (* (mpexpr_fun_i_unary_t) CP->op->fun) (sp+1)));
        goto apply_control_done;

      case MPEXPR_TYPE_MAX & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special max\n"));
        if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) < 0)
          (*p->mpX_swap) (sp, sp+1);
        goto apply_control_done;
      case MPEXPR_TYPE_MIN & MPEXPR_TYPE_MASK_SPECIAL:
        TRACE (printf ("special min\n"));
        if ((* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1) > 0)
          (*p->mpX_swap) (sp, sp+1);
        goto apply_control_done;

      default:
        ERROR ("unrecognised special binary operator",
               MPEXPR_RESULT_BAD_TABLE);
      }

      switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {
      case 0:
        (* (mpexpr_fun_binary_t) CP->op->fun) (sp, sp, sp+1);
        break;
      case MPEXPR_TYPE_LAST_UI:
        CHECK_UI (sp+1);
        (* (mpexpr_fun_binary_ui_t) CP->op->fun)
          (sp, sp, (*p->mpX_get_ui) (sp+1));
        break;
      case MPEXPR_TYPE_RESULT_INT:
        (*p->mpX_set_si)
          (sp,
           (long) (* (mpexpr_fun_i_binary_t) CP->op->fun) (sp, sp+1));
        break;
      case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT:
        CHECK_UI (sp+1);
        (*p->mpX_set_si)
          (sp,
           (long) (* (mpexpr_fun_i_binary_ui_t) CP->op->fun)
           (sp, (*p->mpX_get_ui) (sp+1)));
        break;
      default:
        ERROR ("unrecognised binary argument calling style",
               MPEXPR_RESULT_BAD_TABLE);
      }
    }
    break;

  case MPEXPR_TYPE_NARY(3):
    {
      mpX_ptr  sp;

      CHECK_ARGCOUNT ("ternary", 3);
      DATA_POP (2);
      sp = SP;
      TRACE (MPX_TRACE ("arg1", sp);
             MPX_TRACE ("arg2", sp+1);
             MPX_TRACE ("arg3", sp+1));

      switch (CP->op->type & MPEXPR_TYPE_MASK_ARGSTYLE) {
      case 0:
        (* (mpexpr_fun_ternary_t) CP->op->fun) (sp, sp, sp+1, sp+2);
        break;
      case MPEXPR_TYPE_LAST_UI:
        CHECK_UI (sp+2);
        (* (mpexpr_fun_ternary_ui_t) CP->op->fun)
          (sp, sp, sp+1, (*p->mpX_get_ui) (sp+2));
        break;
      case MPEXPR_TYPE_RESULT_INT:
        (*p->mpX_set_si)
          (sp,
           (long) (* (mpexpr_fun_i_ternary_t) CP->op->fun)
           (sp, sp+1, sp+2));
        break;
      case MPEXPR_TYPE_LAST_UI | MPEXPR_TYPE_RESULT_INT:
        CHECK_UI (sp+2);
        (*p->mpX_set_si)
          (sp,
           (long) (* (mpexpr_fun_i_ternary_ui_t) CP->op->fun)
           (sp, sp+1, (*p->mpX_get_ui) (sp+2)));
        break;
      default:
        ERROR ("unrecognised binary argument calling style",
               MPEXPR_RESULT_BAD_TABLE);
      }
    }
    break;

  default:
    TRACE (printf ("unrecognised operator type: 0x%X\n", CP->op->type));
    ERROR ("", MPEXPR_RESULT_PARSE_ERROR);
  }

 apply_control_done:
  TRACE (MPX_TRACE ("result", SP));
  CONTROL_POP ();
  goto another_operator;

 done:
  if (p->error_code == MPEXPR_RESULT_OK)
    {
      if (p->data_top != 0)
        {
          TRACE (printf ("data stack want top at 0, got %d\n", p->data_top));
          p->error_code = MPEXPR_RESULT_PARSE_ERROR;
        }
      else
        (*p->mpX_set_or_swap) (p->res, SP);
    }

  {
    int  i;
    for (i = 0; i < p->data_inited; i++)
      {
        TRACE (printf ("clear %d\n", i));
        (*p->mpX_clear) (p->data_stack+i);
      }
  }

  FREE_FUNC_TYPE (p->data_stack, p->data_alloc, union mpX_t);
  FREE_FUNC_TYPE (p->control_stack, p->control_alloc, struct mpexpr_control_t);

  return p->error_code;
}