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