small changes

master
Tibor Bizjak 2023-07-09 19:23:11 +02:00
parent 9aa5c11369
commit 28480c4876
1 changed files with 14 additions and 14 deletions

View File

@ -5,6 +5,7 @@
#include <ctype.h>
#include <assert.h>
/* --------- ERROR MESSAGES */
#define printerr(format, ...) printf("error: " format "\n", ##__VA_ARGS__)
#define ZERODIV_ERR "zero divisor"
@ -16,6 +17,7 @@
#define UNKNOWN_TOKEN_ERR "unknown token %s"
#define VAR_NOT_SET_ERR "variable %c has no value"
/* --------- PROMPT FORMATS */
#define PROMPT_F ">> "
#define POP_PRINT_F "pop -> %.8g\n"
@ -25,6 +27,7 @@
"Type 'help' for a list of available commands\n" \
PROMPT_F
/* ------ GLOBAL VALUE STACK */
#define MAXVAL 100 /* maximum depth of val stack */
@ -51,6 +54,7 @@ double val[MAXVAL];
} \
} while (0)
/* -------- STACK PRINTING FUNCTIONS */
double lst_print;
/* print_peek: print top element, leave stack unchanged */
@ -58,6 +62,7 @@ double lst_print;
/* print_pop: pop top element and print it */
#define print_pop() printf(POP_PRINT_F, lst_print = pop())
/* ------- GLOBAL VARIABLE NAMESPACE */
#define NS_SIZE ('z' - 'a' + 1)
@ -84,8 +89,9 @@ double var_vals[NS_SIZE];
/* --------- CALCULATOR FUNCTION TYPES */
/* the following macros allow us to call native c constructs
* in a declarative style */
/* the following macros allow us to call native c constructs in a declarative style */
/* value */
#define VALUE(ID) push(ID)
/* command */
@ -115,12 +121,9 @@ double var_vals[NS_SIZE];
/* --------- CALCULATOR FUNCTIONS */
/* renaming macro */
#define CALL(TOKEN, F, APPLY, ...) APPLY(F, ##__VA_ARGS__)
void help(void);
#define COMMANDS \
X(_, "last printed value", CALL, lst_print, VALUE) \
X(ENTER, "pop top of stack and print it", CALL, print_pop, COMMAND) \
@ -163,7 +166,6 @@ void help(void);
X(min, "minimum value", CALL, fmin, COMM_FUN) \
X(pi, "value of pi", CALL, M_PI, VALUE)
#define TOKENS \
COMMANDS \
OPERATORS \
@ -176,11 +178,10 @@ void help(void);
/* --------- MAIN PROGRAM */
#define MAXTOKEN 100
int gettoken(char *token, int max_t);
char * parsef(char *s, double *x);
void help(void);
/* reverse Polish calculator */
int main()
@ -213,14 +214,15 @@ int main()
else
printerr(UNKNOWN_TOKEN_ERR, token);
}
if (!length())
putchar('\n');
while (length())
printf(POP_PRINT_F, pop());
print_pop();
return 0;
}
/* ---------- HELP COMMAND */
#define MAX_CMD_W 10 /* token padding */
#define VAR_STR "X" /* string to represent variable */
@ -245,6 +247,7 @@ void help(void)
#undef X
}
/* -------- PARSING FUNCTIONS */
#define BLANK ' '
@ -252,7 +255,6 @@ void help(void)
* return token length, -1 on newline and 0 on EOF */
int gettoken(char *token, int t_max)
{
static int c = BLANK;
while (isspace(c) && c != '\n') /* remove leading whitespace */
@ -272,12 +274,10 @@ int gettoken(char *token, int t_max)
return i;
}
/* ---------- CUSTOM ATOF */
#define DEC_SEP X('.') X(',')
#define EXP_SEP X('e') X('E')
/* macros for parsef*/
/* macros for parsef */
#define SIGN(s) \
((*s == '-') ? (s++, -1) : ((*s == '+') ? (s++, 1) : 1))
#define ATOI(s, x, ...) \