evalexpr: parse: simplify operators table

This commit is contained in:
Bruno BELANYI 2020-10-30 22:54:44 +01:00
parent 793a6c6f6f
commit d82bf6e77b
2 changed files with 19 additions and 16 deletions

View file

@ -17,11 +17,7 @@ static const struct {
const enum { ASSOC_LEFT, ASSOC_RIGHT, ASSOC_NONE } assoc;
const enum { OP_INFIX, OP_PREFIX, OP_POSTFIX } fix;
} ops[] = {
# define BINOP(Op, Kind, Prio, Assoc, Fix) \
[Kind] = { #Op, Kind, Prio, Assoc, Fix },
# define PREOP(Op, Kind, Prio, Assoc, Fix) \
[Kind] = { #Op, Kind, Prio, Assoc, Fix },
# define POSTOP(Op, Kind, Prio, Assoc, Fix) \
# define OP(Op, Kind, Prio, Assoc, Fix) \
[Kind] = { #Op, Kind, Prio, Assoc, Fix },
#include "operators.inc"
};

View file

@ -10,25 +10,32 @@
* G, the operand, is parsed by a specific function to start the process.
*/
#ifndef OP
# define OP(Op, Kind, Prio, Assoc, Fix)
#endif
#ifndef BINOP
# define BINOP(_, __, ___, ____, _____)
# define BINOP(Op, Kind, Prio, Assoc) \
OP(Op, Kind, Prio, Assoc, OP_INFIX)
#endif
#ifndef PREOP
# define PREOP(_, __, ___, ____, _____)
# define PREOP(Op, Kind, Prio, Assoc) \
OP(Op, Kind, Prio, Assoc, OP_PREFIX)
#endif
#ifndef POSTOP
# define POSTOP(_, __, ___, ____, _____)
# define POSTOP(Op, Kind, Prio, Assoc) \
OP(Op, Kind, Prio, Assoc, OP_POSTFIX)
#endif
BINOP(+, BINOP_PLUS, 1, ASSOC_LEFT, OP_INFIX)
BINOP(-, BINOP_MINUS, 1, ASSOC_LEFT, OP_INFIX)
BINOP(*, BINOP_TIMES, 2, ASSOC_LEFT, OP_INFIX)
BINOP(/, BINOP_DIVIDES, 2, ASSOC_LEFT, OP_INFIX)
BINOP(^, BINOP_POW, 4, ASSOC_RIGHT, OP_INFIX)
PREOP(-, UNOP_NEGATE, 3, ASSOC_RIGHT, OP_PREFIX)
PREOP(+, UNOP_IDENTITY, 3, ASSOC_RIGHT, OP_PREFIX)
POSTOP(!, UNOP_FACT, 5, ASSOC_NONE, OP_POSTFIX)
BINOP(+, BINOP_PLUS, 1, ASSOC_LEFT)
BINOP(-, BINOP_MINUS, 1, ASSOC_LEFT)
BINOP(*, BINOP_TIMES, 2, ASSOC_LEFT)
BINOP(/, BINOP_DIVIDES, 2, ASSOC_LEFT)
BINOP(^, BINOP_POW, 4, ASSOC_RIGHT)
PREOP(-, UNOP_NEGATE, 3, ASSOC_RIGHT)
PREOP(+, UNOP_IDENTITY, 3, ASSOC_RIGHT)
POSTOP(!, UNOP_FACT, 5, ASSOC_NONE)
#undef BINOP
#undef PREOP
#undef POSTOP
#undef OP