removed unecessary type argument in calc function macros
parent
13cc6ee019
commit
66433dd34d
|
@ -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
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ double val[MAXVAL];
|
||||||
#define length() sp
|
#define length() sp
|
||||||
/* push: push f onto value stack */
|
/* push: push f onto value stack */
|
||||||
#define push(x) \
|
#define push(x) \
|
||||||
((sp < MAXVAL) ? val[sp++] = x : printerr(FULL_STACK_ERR, (double) (x)))
|
((sp < MAXVAL) ? val[sp++] = x : printerr(FULL_STACK_ERR, (double) (x)))
|
||||||
/* pop: pop and return top value from stack */
|
/* pop: pop and return top value from stack */
|
||||||
double pop(void)
|
double pop(void)
|
||||||
{
|
{
|
||||||
|
@ -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) \
|
||||||
|
|
Loading…
Reference in New Issue