Compare commits
2 Commits
9aa5c11369
...
115b8f8132
Author | SHA1 | Date |
---|---|---|
Tibor Bizjak | 115b8f8132 | |
Tibor Bizjak | 28480c4876 |
|
@ -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,16 +62,20 @@ 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)
|
||||
|
||||
char var_flags[NS_SIZE];
|
||||
double var_vals[NS_SIZE];
|
||||
|
||||
/* is_var: return 0 if c is not a variable name */
|
||||
#define is_var(c) isalpha(c)
|
||||
|
||||
/* set_var: pop val from stack, set variable c to val */
|
||||
#define set_var(c) \
|
||||
do { \
|
||||
int i = c - 'a'; \
|
||||
int i = tolower(c) - 'a'; \
|
||||
var_flags[i] = 1; \
|
||||
var_vals[i] = pop(); \
|
||||
} while (0)
|
||||
|
@ -75,17 +83,26 @@ double var_vals[NS_SIZE];
|
|||
/* get_var: push value of c to stack */
|
||||
#define get_var(c) \
|
||||
do { \
|
||||
int i = c - 'a'; \
|
||||
int i = tolower(c) - 'a'; \
|
||||
if (!var_flags[i]) \
|
||||
printerr(VAR_NOT_SET_ERR, i+'a'); \
|
||||
else \
|
||||
push(var_vals[i]); \
|
||||
} while (0)
|
||||
|
||||
/* print_vars: print all set variables and their values */
|
||||
void print_vars(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < NS_SIZE; i++)
|
||||
if (var_flags[i])
|
||||
printf("%c = %.8g\n", i+'a', var_vals[i]);
|
||||
}
|
||||
|
||||
/* --------- 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 +132,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) \
|
||||
|
@ -129,6 +143,7 @@ void help(void);
|
|||
X(swap, "swap top two elements of stack", COMMAND) \
|
||||
X(clear, "clear stack", COMMAND) \
|
||||
X(help, "print all available commands", COMMAND) \
|
||||
X(vars, "print defined variables and their values", CALL, print_vars, COMMAND)
|
||||
|
||||
#define OPERATORS \
|
||||
X(+, "add operands", COMM_OP) \
|
||||
|
@ -163,7 +178,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 +190,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()
|
||||
|
@ -203,8 +216,8 @@ int main()
|
|||
TOKENS
|
||||
#undef X
|
||||
#define X(C, DESC, F) \
|
||||
if (t_len == 2 && token[0] == C && isalpha(token[1])) \
|
||||
F(tolower(token[1])); \
|
||||
if (t_len == 2 && token[0] == C && is_var(token[1])) \
|
||||
F(token[1]); \
|
||||
else
|
||||
VAR_TOKENS
|
||||
#undef X
|
||||
|
@ -213,14 +226,15 @@ int main()
|
|||
else
|
||||
printerr(UNKNOWN_TOKEN_ERR, token);
|
||||
}
|
||||
if (!length())
|
||||
putchar('\n');
|
||||
|
||||
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 +259,7 @@ void help(void)
|
|||
#undef X
|
||||
}
|
||||
|
||||
|
||||
/* -------- PARSING FUNCTIONS */
|
||||
#define BLANK ' '
|
||||
|
||||
|
@ -252,7 +267,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 +286,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, ...) \
|
||||
|
|
Loading…
Reference in New Issue