Compare commits

...

2 Commits

Author SHA1 Message Date
Tibor Bizjak 1f31d9e372 k&r 04-05: added math.h bindings 2023-07-09 14:06:16 +02:00
Tibor Bizjak b655221b5b k&r 04-04: added stack manipulation commands 2023-07-08 17:46:02 +02:00
1 changed files with 124 additions and 22 deletions

View File

@ -1,34 +1,116 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define printerr(format, ...) printf("error: " format "\n", ##__VA_ARGS__)
int stack_len(void);
void push(double x);
double pop(void);
int stack_len(void);
double peek(void);
void clear(void);
/* --------- PROMPT FORMATS */
#define PROMPT_F ">> "
#define POP_PRINT_F "pop -> %.8g\n"
#define PEEK_PRINT_F "peek -> %.8g\n"
#define EMPTY_F "empty\n"
/* --------- ERROR MESSAGES */
#define ZERODIV_ERR "zero divisor"
#define DOMAIN_ERR "operand not in domain [%d, %d]"
#define NEG_ERR "negative operand %g"
#define FULL_STACK_ERR "stack full, can't push %g"
#define EMPTY_STACK_ERR "stack empty"
/* --------- CALCULATOR FUNCTION TYPES */
/* renaming macro */
#define CALL(TOKEN, F, APPLY, ...) APPLY(F, ##__VA_ARGS__)
/* 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, T, ...) \
do { T op = pop(); __VA_ARGS__ push(((T) pop()) OP op); } while (0)
/* nonzero second operand */
#define NONZ_SND_OP if (!op) printerr(ZERODIV_ERR); else
/* unary function */
#define UNARY_FUN(F) push(F(pop()))
/* unary function with tests */
#define UNARY_T_FUN(F, T, ...) do { double op = pop(); __VA_ARGS__ push(F(op)); } while (0)
/* operator in range */
#define IN_RANGE(A, B) if (op < A || op > B) printerr(DOMAIN_ERR, A, B); else
/* operator > 0 */
#define NON_NEG if (op < 0) printerr(NEG_ERR, op); else
/* commutative function */
#define COMM_FUN(F) push(F(pop(), pop()))
/* non-commutative function */
#define NONC_FUN(F, T, ...) \
do { T op = pop(); __VA_ARGS__ push(F(pop(), op)); } while (0)
/* --------- CALCULATOR FUNCTIONS */
/* commutative operator application */
#define COMM_OP(OP) push(pop() OP pop())
/* non-commutative operator application */
#define NONC_OP(OP, T, ...) \
do { T op2 = pop(); __VA_ARGS__ push(((T) pop()) OP op2); } while (0)
/* nonzero second operand operator application */
#define NONZ_OP(OP, T) \
NONC_OP(OP, T, if (!op2) printerr(ZERODIV_ERR); else)
#define FUNCTIONS \
#define COMMANDS \
X(print, COMMAND) \
X(dup, COMMAND) \
X(swap, COMMAND) \
X(clear, COMMAND)
#define OPERATORS \
X(+, COMM_OP) \
X(*, COMM_OP) \
X(-, NONC_OP, double) \
X(/, NONZ_OP, double) \
X(%, NONZ_OP, int)
X(/, NONC_OP, double, NONZ_SND_OP) \
X(%, NONC_OP, int, NONZ_SND_OP) \
X(>, NONC_OP, double) \
X(<, NONC_OP, double) \
X(<=, NONC_OP, double) \
X(>=, NONC_OP, double)
#define MATH_H_BINDINGS \
X(sin, UNARY_FUN) \
X(cos, UNARY_FUN) \
X(tan, UNARY_FUN) \
X(acos, UNARY_T_FUN, double, IN_RANGE(-1, 1)) \
X(asin, UNARY_T_FUN, double, IN_RANGE(-1, 1)) \
X(atan, UNARY_FUN) \
X(exp, UNARY_FUN) \
X(log, UNARY_T_FUN, double, NON_NEG) \
X(log10, UNARY_T_FUN, double, NON_NEG) \
X(log2, UNARY_T_FUN, double, NON_NEG) \
X(pow, NONC_FUN, double) \
X(sqrt, UNARY_T_FUN, double, NON_NEG) \
X(ceil, UNARY_FUN) \
X(floor, UNARY_FUN) \
X(round, UNARY_FUN) \
X(abs, CALL, fabs, UNARY_FUN) \
X(max, CALL, fmax, COMM_FUN) \
X(min, CALL, fmin, COMM_FUN) \
X(pi, CALL, M_PI, VALUE)
#define TOKENS \
COMMANDS \
OPERATORS \
MATH_H_BINDINGS
/* --------- COMMAND DEFINITIONS */
#define print() printf(PEEK_PRINT_F, peek())
#define dup() push(peek())
#define swap() \
do { double fst, snd; snd = pop(), fst = pop(); push(snd), push(fst); } while (0)
#define MAXTOKEN 100
@ -40,20 +122,28 @@ int main()
char token[MAXTOKEN + 1];
double op2;
int t_len;
printf(PROMPT_F);
while (t_len = gettoken(token, MAXTOKEN)) {
if (t_len < 0)
printf("\t%g\n", pop());
else
if (t_len < 0) { /* newline token */
if (stack_len())
printf(POP_PRINT_F, pop());
else
printf(EMPTY_F);
printf(PROMPT_F);
} else
#define X(F, APPLY, ...) \
if (!strcmp(#F, token)) \
APPLY(F, ##__VA_ARGS__); \
else
FUNCTIONS
TOKENS
#undef X
push(atof(token));
}
printf("%g\n", pop());
if (!stack_len())
putchar('\n');
while (stack_len())
printf(POP_PRINT_F, pop());
return 0;
}
@ -98,14 +188,14 @@ int stack_len()
return sp;
}
/* push: push f onto value stack */
void push(double f)
{
if (sp < MAXVAL)
val[sp++] = f;
else
printf("error: stack full, can't push %g\n", f);
printerr(FULL_STACK_ERR, f);
}
/* pop: pop and return top value from stack */
@ -113,8 +203,20 @@ double pop(void)
{
if (sp)
return val[--sp];
printf("error: stack empty\n");
printerr(EMPTY_STACK_ERR);
return 0;
}
/* peek: return top value from stack, leave stack unchanged */
double peek(void)
{
if (sp)
return val[sp-1];
printerr(EMPTY_STACK_ERR);
return 0.0;
}
void clear(void)
{
sp = 0;
}