diff --git a/k&r/04-funcs-and-prog-struct/04-calc.c b/k&r/04-funcs-and-prog-struct/04-calc.c index c290c25..839595d 100644 --- a/k&r/04-funcs-and-prog-struct/04-calc.c +++ b/k&r/04-funcs-and-prog-struct/04-calc.c @@ -37,15 +37,19 @@ double val[MAXVAL]; /* length: current length of stack */ #define length() sp /* push: push f onto value stack */ -#define push(x) ((sp < MAXVAL) ? val[sp++] = (x) : printerr(FULL_STACK_ERR, ((double) (x)))) +#define push(x) \ + ((sp < MAXVAL) ? val[sp++] = x : printerr(FULL_STACK_ERR, (double) (x))) /* pop: pop and return top value from stack */ -#define pop() (sp ? val[--sp] : (printerr(EMPTY_STACK_ERR), 0)) +double pop(void) +{ + return sp ? val[--sp] : (printerr(EMPTY_STACK_ERR), 0); +} /* peek: return top value from stack, leave stack unchanged */ #define peek() (sp ? val[sp-1] : (printerr(EMPTY_STACK_ERR), 0)) /* clear: clear stack */ #define clear() (sp = 0) /* dup: duplicate top element */ -#define dup() push(peek()) +#define dup() do { double tmp = peek(); push(tmp); } while (0) /* swap: swap top two elements */ #define swap() \ do { if (sp < 2) printerr(SHORT_STACK_ERR); else { \ @@ -203,7 +207,7 @@ int main() int t_len; printf(INITIAL_PROMPT_F); - while (t_len = gettoken(token, MAXTOKEN)) { + while ((t_len = gettoken(token, MAXTOKEN))) { if (t_len < 0) { /* newline token */ if (length()) print_pop();