@CesantaHQ
// Producer function
static int consume(void) {
return getchar();
}
// Consumer function
static void tokenize(void) {
int c;
while ((c = consume()) != EOF) {
if (isdigit(c)) {
do {
add_to_token(c);
c = consume();
} while (isdigit(c));
got_token(TOKEN_NUMBER);
}
if (!isspace(c)) {
add_to_token(c);
got_token(TOKEN_OPERATION);
}
}
}
// Before
static int consume(void) {
return getchar();
}
// After
static int consume(void) {
size_t i, n;
char buf[10];
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) {
for (i = 0; i < n; i++) {
MAGIC_RETURN buf[i]; // <--- Simple return won't work
}
}
MAGIC_RETURN EOF; // <--- Simple return won't work
}
// To save state, all local variables are declared static
static int consume(void) {
static size_t i, n;
static char buf[10];
static int state;
switch (state) {
case 0:
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) {
for (i = 0; i < n; i++) {
state = 1;
return buf[i];
case 1:;
}
}
}
return EOF;
}
// Coroutine macros
#define CR_BEGIN static int state = 0; switch (state) { case 0:
#define YIELD(x) do { state = __LINE__; return x; case __LINE__:; } while (0)
#define CR_END }
static int consume(void) {
static size_t i, n;
static char buf[10];
CR_BEGIN;
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) {
for (i = 0; i < n; i++) {
YIELD(buf[i]);
}
}
YIELD(EOF);
CR_END;
}
contact me at
sergey.lyubka@cesanta.com