exc/k&r/04-funcs-and-prog-struct/04-calc/tokens.h

101 lines
3.4 KiB
C

#ifndef TOKENS_H
#define TOKENS_H
#include <math.h>
#define printerr(format, ...) printf("error: " format "\n", ##__VA_ARGS__)
/* ERROR MESSAGES */
#define ZERODIV_ERR "zero divisor"
#define DOMAIN_ERR "operand not in domain [%d, %d]"
#define NEG_ERR "negative operand %g"
/* --------- CALCULATOR FUNCTION TYPES */
/* the following macros allow us to call native c constructs
* in a declarative style */
/* value */
#define VALUE(ID) push(ID)
/* command */
#define COMMAND(CMD) CMD()
/* commutative operator */
#define COMM_OP(OP) push(pop() OP pop())
/* non-commutative operator */
#define NONC_OP(OP, ...) \
do { double op = pop(); __VA_ARGS__ push(pop() OP op); } while (0)
/* unary function */
#define UNARY_FUN(F) push(F(pop()))
/* unary function with tests */
#define UNARY_T_FUN(F, ...) \
do { double op = pop(); __VA_ARGS__ push(F(op)); } while (0)
/* commutative function */
#define COMM_FUN(F) push(F(pop(), pop()))
/* non-commutative function */
#define NONC_FUN(F, ...) \
do { double op = pop(); __VA_ARGS__ push(F(pop(), op)); } while (0)
/* OPERAND TESTS */
/* nonzero operand */
#define NONZ_SND_OP if (!op) printerr(ZERODIV_ERR); else
/* operator in range */
#define IN_RANGE(A, B) if (op < A || op > B) printerr(DOMAIN_ERR, A, B); else
/* non-negative operand */
#define NON_NEG if (op < 0) printerr(NEG_ERR, op); else
/* --------- CALCULATOR FUNCTIONS */
/* renaming macro */
#define CALL(TOKEN, F, APPLY, ...) APPLY(F, ##__VA_ARGS__)
#define COMMANDS \
X(_, "last printed value", CALL, lst_print, VALUE) \
X(ENTER, "pop top of stack and print it", CALL, print_pop, COMMAND) \
X(print, "print top of stack", CALL, print_peek, COMMAND) \
X(dup, "duplicate top of stack", COMMAND) \
X(swap, "swap top two elements of stack", COMMAND) \
X(clear, "clear stack", COMMAND) \
X(help, "print all available commands", COMMAND) \
X(vars, "print defined variables and their values", CALL, print_vars, COMMAND)
#define MOD(x, y) ((int) (x) % (int) (y))
#define OPERATORS \
X(+, "add operands", COMM_OP) \
X(*, "multiply operands", COMM_OP) \
X(-, "substact operands", NONC_OP) \
X(/, "divide operands", NONC_OP, NONZ_SND_OP) \
X(%, "apply modulo to operands", CALL, MOD, NONC_FUN, NONZ_SND_OP) \
X(>, "greater than", NONC_OP) \
X(<, "less than", NONC_OP) \
X(<=, "less than or equal", NONC_OP) \
X(>=, "greater than or equal", NONC_OP) \
X(==, "test equality", COMM_OP)
#define MATH_H_BINDINGS \
X(sin, "sine", UNARY_FUN) \
X(cos, "cosine", UNARY_FUN) \
X(tan, "tangent", UNARY_FUN) \
X(acos, "arc cosine", UNARY_T_FUN, IN_RANGE(-1, 1)) \
X(asin, "arc sine", UNARY_T_FUN, IN_RANGE(-1, 1)) \
X(atan, "arc tangent", UNARY_FUN) \
X(exp, "exponentional function", UNARY_FUN) \
X(log, "natural logarithm", UNARY_T_FUN, NON_NEG) \
X(log10, "logarithm of base 10", UNARY_T_FUN, NON_NEG) \
X(log2, "logarithm of base 2", UNARY_T_FUN, NON_NEG) \
X(pow, "power function", NONC_FUN) \
X(sqrt, "square root", UNARY_T_FUN, NON_NEG) \
X(ceil, "round up value", UNARY_FUN) \
X(floor, "round down value", UNARY_FUN) \
X(round, "round to nearest", UNARY_FUN) \
X(abs, "absolute value", CALL, fabs, UNARY_FUN) \
X(max, "maximum value", CALL, fmax, COMM_FUN) \
X(min, "minimum value", CALL, fmin, COMM_FUN) \
X(pi, "value of pi", CALL, M_PI, VALUE)
#define TOKENS \
COMMANDS \
OPERATORS \
MATH_H_BINDINGS
#undef printerr
#endif // TOKENS_H