-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtinyfsm.c
53 lines (40 loc) · 1.16 KB
/
tinyfsm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Tiny FSM. rlyeh, public domain | wtrmrkrlyeh
#pragma once
#define with(st) for(int i=1;i--;st[1]=st[0]) switch(((st[0])<<16)|(st[1]))
#define when(a) break; case (((a)<<16)|(a))
#define transition(a,b) break; case (((b)<<16)|(a))
typedef int fsm[2];
/*
#include <stdio.h>
enum {
IDLE,
WALKING,
RUNNING,
};
void update( fsm state ) {
with(state) {
when(IDLE): puts("idle");
transition(IDLE,WALKING): puts("idle --> walking");
transition(IDLE,RUNNING): puts("idle --> running");
when(WALKING): puts("walking");
transition(WALKING,IDLE): puts("walking --> idle");
transition(WALKING,RUNNING): puts("walking --> running");
when(RUNNING): puts("running");
transition(RUNNING,IDLE): puts("running --> idle");
transition(RUNNING,WALKING): puts("running --> walking");
}
}
int main() {
fsm state = {0};
*state = IDLE;
update(state);
*state = WALKING;
update(state);
*state = WALKING;
update(state);
*state = RUNNING;
update(state);
*state = IDLE;
update(state);
}
*/