]> pd.if.org Git - lice/blob - tests/cgoto.c
autocommit for files dated 2014-11-17 20:13:34
[lice] / tests / cgoto.c
1 // labels as values (computed goto)
2
3 int main(void) {
4     // implement a small dispatch table of instructions for a mini
5     // state machine, to test computed goto
6
7     // test state
8     int instruction_0_state = 0;
9     int instruction_1_state = 0;
10     int instruction_2_state = 0;
11
12     unsigned char bytecode[] = {0x01, 0x05, 0x01, 0x02, 0x02, 0x03};
13     unsigned char *pc        = bytecode;
14
15     void *dispatch[] = {
16         0,
17         &&instruction_0,
18         &&instruction_1,
19         &&instruction_2
20     };
21
22     #define DISPATCH(INDEX)      \
23         do {                     \
24             pc += (INDEX);       \
25             goto *dispatch[*pc]; \
26         } while (0)
27
28     // machine loop
29     do {
30
31         instruction_0:
32             instruction_0_state += *(pc + 1);
33             DISPATCH(2);
34
35         instruction_1:
36             instruction_1_state ++;
37             DISPATCH(1);
38
39         instruction_2:
40             instruction_2_state --;
41             break;
42
43     } while (1);
44
45     expecti(instruction_0_state, 7);
46     expecti(instruction_1_state, 1);
47     expecti(instruction_2_state, -1);
48
49     return 0;
50 }