exc/k&r/04-funcs-and-prog-struct/04-calc/main.c

213 lines
5.0 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
2023-07-09 18:33:07 +02:00
#include <ctype.h>
#include <assert.h>
#define MAX_STACK 100
2023-07-23 12:24:13 +02:00
#include "global-stack.h"
#include "tokens.h"
2023-07-09 19:23:11 +02:00
/* --------- ERROR MESSAGES */
#define printerr(format, ...) printf("error: " format "\n", ##__VA_ARGS__)
2023-07-09 14:59:27 +02:00
#define UNKNOWN_TOKEN_ERR "unknown token %s"
2023-07-09 18:33:07 +02:00
#define VAR_NOT_SET_ERR "variable %c has no value"
2023-07-09 19:23:11 +02:00
/* --------- PROMPT FORMATS */
#define PROMPT_F ">>> "
#define POP_PRINT_F "%.8g\n"
#define PEEK_PRINT_F "-> %.8g\n"
2023-07-09 16:12:03 +02:00
#define INITIAL_PROMPT_F \
"A simple polish notation calculator.\n" \
2023-07-09 16:12:03 +02:00
"Type 'help' for a list of available commands\n" \
PROMPT_F
2023-07-09 18:33:07 +02:00
/* -------- STACK PRINTING FUNCTIONS */
double lst_print;
/* print_peek: print top element, leave stack unchanged */
#define print_peek() printf(PEEK_PRINT_F, lst_print = peek())
/* print_pop: pop top element and print it */
#define print_pop() printf(POP_PRINT_F, lst_print = pop())
2023-07-09 19:23:11 +02:00
2023-07-09 18:33:07 +02:00
/* ------- GLOBAL VARIABLE NAMESPACE */
#define NS_SIZE ('z' - 'a' + 1)
char var_flags[NS_SIZE];
double var_vals[NS_SIZE];
2023-07-09 19:30:05 +02:00
/* is_var: return 0 if c is not a variable name */
#define is_var(c) isalpha(c)
2023-07-09 18:33:07 +02:00
/* set_var: pop val from stack, set variable c to val */
#define set_var(c) \
do { \
2023-07-09 19:30:05 +02:00
int i = tolower(c) - 'a'; \
2023-07-09 18:33:07 +02:00
var_flags[i] = 1; \
var_vals[i] = pop(); \
} while (0)
/* get_var: push value of c to stack */
#define get_var(c) \
do { \
2023-07-09 19:30:05 +02:00
int i = tolower(c) - 'a'; \
2023-07-09 18:33:07 +02:00
if (!var_flags[i]) \
printerr(VAR_NOT_SET_ERR, i+'a'); \
else \
push(var_vals[i]); \
} while (0)
2023-07-09 19:30:05 +02:00
/* 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]);
}
2023-07-09 18:33:07 +02:00
/* variable manipulation tokens */
#define VAR_TOKENS \
X('=', "pop value top from stack and set %s to value", set_var) \
X('$', "push value of %s to top of stack", get_var)
2023-07-09 16:12:03 +02:00
/* --------- MAIN PROGRAM */
#define MAXTOKEN 100
int gettoken(char *token, int max_t);
2023-07-09 14:59:27 +02:00
char * parsef(char *s, double *x);
2023-07-09 19:23:11 +02:00
void help(void);
2023-07-09 18:33:07 +02:00
/* reverse Polish calculator */
int main()
{
char token[MAXTOKEN + 1];
2023-07-09 14:59:27 +02:00
double x;
int t_len;
2023-07-09 16:12:03 +02:00
printf(INITIAL_PROMPT_F);
2023-07-09 20:08:50 +02:00
while ((t_len = gettoken(token, MAXTOKEN))) {
if (t_len < 0) { /* newline token */
2023-07-09 16:12:03 +02:00
if (length())
2023-07-09 18:33:07 +02:00
print_pop();
printf(PROMPT_F);
} else
2023-07-09 16:12:03 +02:00
#define X(F, DESC, APPLY, ...) \
2023-07-09 14:59:27 +02:00
if (t_len == sizeof(#F) / sizeof(char) - 1 && !strcmp(#F, token)) \
APPLY(F, ##__VA_ARGS__); \
else
2023-07-09 14:06:16 +02:00
TOKENS
2023-07-09 18:33:07 +02:00
#undef X
#define X(C, DESC, F) \
2023-07-09 19:30:05 +02:00
if (t_len == 2 && token[0] == C && is_var(token[1])) \
F(token[1]); \
2023-07-09 18:33:07 +02:00
else
VAR_TOKENS
#undef X
2023-07-09 14:59:27 +02:00
if (*parsef(token, &x) == '\0')
2023-07-09 18:33:07 +02:00
push(x);
2023-07-09 14:59:27 +02:00
else
printerr(UNKNOWN_TOKEN_ERR, token);
}
2023-07-09 19:23:11 +02:00
putchar('\n');
while (length())
2023-07-09 19:23:11 +02:00
print_pop();
return 0;
}
2023-07-09 19:23:11 +02:00
2023-07-09 18:33:07 +02:00
/* ---------- HELP COMMAND */
#define MAX_CMD_W 10 /* token padding */
#define VAR_STR "X" /* string to represent variable */
/* we verify that no token is to large to be pretty printed */
#define X(TOKEN, ...) \
static_assert(sizeof(#TOKEN) / sizeof(char) <= MAX_CMD_W, \
"size of " #TOKEN " is to large for pretty printing " \
"in help command");
TOKENS
#undef X
2023-07-09 16:12:03 +02:00
/* help: prints all available calculator functions */
void help(void)
{
2023-07-09 18:33:07 +02:00
#define X(C, DESC, ...) \
printf("%c%-*s" DESC "\n", C, MAX_CMD_W-1, VAR_STR, VAR_STR);
VAR_TOKENS
#undef X
#define X(F, DESC, ...) printf("%-*s" DESC "\n", MAX_CMD_W, #F);
2023-07-09 16:12:03 +02:00
TOKENS
#undef X
}
2023-07-09 19:23:11 +02:00
/* -------- PARSING FUNCTIONS */
#define BLANK ' '
/* gettoken: get next space delimited token from input
* 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 */
c = getchar();
if (c == '\n') { /* newline is a token */
c = BLANK;
return -1;
}
/* fetch token */
int i;
for (i = 0; i < t_max && c != EOF && !isspace(c); c = getchar(), i++)
token[i] = c;
token[i] = '\0';
return i;
}
2023-07-09 14:59:27 +02:00
#define DEC_SEP X('.') X(',')
#define EXP_SEP X('e') X('E')
2023-07-09 19:23:11 +02:00
/* macros for parsef */
2023-07-09 14:59:27 +02:00
#define SIGN(s) \
((*s == '-') ? (s++, -1) : ((*s == '+') ? (s++, 1) : 1))
#define ATOI(s, x, ...) \
for (; isdigit(*s); s++, ##__VA_ARGS__) \
(x) = 10 * (x) + (*s - '0')
/* parsef: convert string s to double, write it to *x
* return pointer of last read char in s */
char * parsef(char s[], double *x)
{
*x = 0;
/* integer part */
int sign = SIGN(s);
ATOI(s, *x);
int exp = 0;
/* fractional part */
#define X(sep) *s == sep ||
if (DEC_SEP 0) {
s++;
ATOI(s, *x, exp--);
*x *= pow(10, exp);
2023-07-09 14:59:27 +02:00
}
*x *= sign;
2023-07-09 14:59:27 +02:00
#undef X
/* exponent part */
#define X(sep) *s == sep ||
if (EXP_SEP 0) {
s++;
sign = SIGN(s);
exp = 0;
ATOI(s, exp);
*x *= pow(10, sign * exp);
}
2023-07-09 14:59:27 +02:00
#undef X
return s;
}