Skip to content

Instantly share code, notes, and snippets.

@nnathan
Created October 30, 2019 06:39
Show Gist options
  • Save nnathan/88f8c1a39751c21d614629710dcc2e8f to your computer and use it in GitHub Desktop.
Save nnathan/88f8c1a39751c21d614629710dcc2e8f to your computer and use it in GitHub Desktop.
caze transition table from https://ideone.com/DdKmwq
#include <stdio.h>
#include <stdlib.h>
typedef void effector(int);
typedef struct transition transition;
struct transition {
int state;
effector *effect;
};
enum sigil { Letter, EndOfFile };
typedef enum sigil sigil;
sigil sigilify(int c) {
switch (c) {
case EOF: return EndOfFile;
default: return Letter;
}
}
void push (int c);
void pop (int c);
void swap (int c);
void halt (int c);
#define T(state, sigil) (((state) << 1) | sigil)
int main() {
transition state = { 0, push };
transition table[] = {
[T(0, Letter)]={ 1, push }, [T(0, EndOfFile)]={ 0, halt },
[T(1, Letter)]={ 0, swap }, [T(1, EndOfFile)]={ 0, pop },
};
for (;;) {
int c = getchar();
(state = table[T(state.state, sigilify(c))]).effect(c);
}
}
int b;
void push (int c) { b = c; }
void pop (int c) { putchar(b); }
void swap (int c) { putchar(c); pop(0); }
void halt (int c) { exit(EXIT_SUCCESS); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment