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