removed unecessary type argument in calc function macros

master
Tibor Bizjak 2023-07-09 22:21:49 +02:00
parent 13cc6ee019
commit 66433dd34d
1 changed files with 23 additions and 20 deletions

View File

@ -23,7 +23,7 @@
#define POP_PRINT_F "pop -> %.8g\n" #define POP_PRINT_F "pop -> %.8g\n"
#define PEEK_PRINT_F "peek -> %.8g\n" #define PEEK_PRINT_F "peek -> %.8g\n"
#define INITIAL_PROMPT_F \ #define INITIAL_PROMPT_F \
"A simple calculator using polish notation.\n" \ "A simple polish notation calculator.\n" \
"Type 'help' for a list of available commands\n" \ "Type 'help' for a list of available commands\n" \
PROMPT_F PROMPT_F
@ -114,8 +114,8 @@ void print_vars(void)
/* commutative operator */ /* commutative operator */
#define COMM_OP(OP) push(pop() OP pop()) #define COMM_OP(OP) push(pop() OP pop())
/* non-commutative operator */ /* non-commutative operator */
#define NONC_OP(OP, T, ...) \ #define NONC_OP(OP, ...) \
do { T op = pop(); __VA_ARGS__ push(((T) pop()) OP op); } while (0) do { double op = pop(); __VA_ARGS__ push(pop() OP op); } while (0)
/* unary function */ /* unary function */
#define UNARY_FUN(F) push(F(pop())) #define UNARY_FUN(F) push(F(pop()))
/* unary function with tests */ /* unary function with tests */
@ -124,9 +124,10 @@ void print_vars(void)
/* commutative function */ /* commutative function */
#define COMM_FUN(F) push(F(pop(), pop())) #define COMM_FUN(F) push(F(pop(), pop()))
/* non-commutative function */ /* non-commutative function */
#define NONC_FUN(F, T, ...) \ #define NONC_FUN(F, ...) \
do { T op = pop(); __VA_ARGS__ push(F(pop(), op)); } while (0) do { double op = pop(); __VA_ARGS__ push(F(pop(), op)); } while (0)
/* OPERAND TESTS */
/* nonzero operand */ /* nonzero operand */
#define NONZ_SND_OP if (!op) printerr(ZERODIV_ERR); else #define NONZ_SND_OP if (!op) printerr(ZERODIV_ERR); else
/* operator in range */ /* operator in range */
@ -149,31 +150,33 @@ void print_vars(void)
X(help, "print all available commands", COMMAND) \ X(help, "print all available commands", COMMAND) \
X(vars, "print defined variables and their values", CALL, print_vars, COMMAND) X(vars, "print defined variables and their values", CALL, print_vars, COMMAND)
#define MOD(x, y) ((int) (x) % (int) (y))
#define OPERATORS \ #define OPERATORS \
X(+, "add operands", COMM_OP) \ X(+, "add operands", COMM_OP) \
X(*, "multiply operands", COMM_OP) \ X(*, "multiply operands", COMM_OP) \
X(-, "substact operands", NONC_OP, double) \ X(-, "substact operands", NONC_OP) \
X(/, "divide operands", NONC_OP, double, NONZ_SND_OP) \ X(/, "divide operands", NONC_OP, NONZ_SND_OP) \
X(%, "modulo operands", NONC_OP, int, NONZ_SND_OP) \ X(%, "apply modulo to operands", CALL, MOD, NONC_FUN, NONZ_SND_OP) \
X(>, "greater than", NONC_OP, double) \ X(>, "greater than", NONC_OP) \
X(<, "less than", NONC_OP, double) \ X(<, "less than", NONC_OP) \
X(<=, "less than or equal", NONC_OP, double) \ X(<=, "less than or equal", NONC_OP) \
X(>=, "greater than or equal", NONC_OP, double) \ X(>=, "greater than or equal", NONC_OP) \
X(==, "test equality", COMM_OP) X(==, "test equality", COMM_OP)
#define MATH_H_BINDINGS \ #define MATH_H_BINDINGS \
X(sin, "sine", UNARY_FUN) \ X(sin, "sine", UNARY_FUN) \
X(cos, "cosine", UNARY_FUN) \ X(cos, "cosine", UNARY_FUN) \
X(tan, "tangent", UNARY_FUN) \ X(tan, "tangent", UNARY_FUN) \
X(acos, "arc cosine", UNARY_T_FUN, double, IN_RANGE(-1, 1)) \ X(acos, "arc cosine", UNARY_T_FUN, IN_RANGE(-1, 1)) \
X(asin, "arc sine", UNARY_T_FUN, double, IN_RANGE(-1, 1)) \ X(asin, "arc sine", UNARY_T_FUN, IN_RANGE(-1, 1)) \
X(atan, "arc tangent", UNARY_FUN) \ X(atan, "arc tangent", UNARY_FUN) \
X(exp, "exponentional function", UNARY_FUN) \ X(exp, "exponentional function", UNARY_FUN) \
X(log, "natural logarithm", UNARY_T_FUN, double, NON_NEG) \ X(log, "natural logarithm", UNARY_T_FUN, NON_NEG) \
X(log10, "logarithm of base 10", UNARY_T_FUN, double, NON_NEG) \ X(log10, "logarithm of base 10", UNARY_T_FUN, NON_NEG) \
X(log2, "logarithm of base 2", UNARY_T_FUN, double, NON_NEG) \ X(log2, "logarithm of base 2", UNARY_T_FUN, NON_NEG) \
X(pow, "power function", NONC_FUN, double) \ X(pow, "power function", NONC_FUN) \
X(sqrt, "square root", UNARY_T_FUN, double, NON_NEG) \ X(sqrt, "square root", UNARY_T_FUN, NON_NEG) \
X(ceil, "round up value", UNARY_FUN) \ X(ceil, "round up value", UNARY_FUN) \
X(floor, "round down value", UNARY_FUN) \ X(floor, "round down value", UNARY_FUN) \
X(round, "round to nearest", UNARY_FUN) \ X(round, "round to nearest", UNARY_FUN) \