3 * Test the control-flow metaprogramming macros
5 * (c) 2022 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
40 #define STEP(s) check_step(s, __FILE__ ": " STR(__LINE__))
41 #define MISSTEP STEP(-1)
42 static void check_step(int s, const char *where)
45 fprintf(stderr, "misstep at %s: expected %d but found %d\n",
52 #define LASTSTEP(s) laststep(s, __FILE__ ": " STR(__LINE__))
53 static void laststep(int s, const char *where)
54 { check_step(s, where); step = 0; }
56 #define FORELSE(head) \
58 MC_LABEL(out) MC_ACT(;) \
59 MC_LABEL(top) ALLOWELSE(els) \
60 AFTER(outer, GOELSE(els)) \
66 #define FOR_FIZZBUZZ(var, base, limit) \
68 MC_LABEL(out) MC_ACT({ ; }) \
69 MC_LABEL(top) DECL(bounds, \
70 int _i = base COMMA _limit = limit) \
71 for (; _i < _limit; _i++) \
72 DECL(buf, char _buf[24]) \
73 DECL(var, const char *var) \
79 case 3: case 6: case 9: case 12: \
86 sprintf(_buf, "%d", _i); var = _buf; \
97 BEFORE(before0, STEP(0)) STEP(1);
98 AFTER(after0, STEP(3)) STEP(2);
101 WRAP(wrap0, STEP(0), STEP(2), MISSTEP) STEP(1);
102 WRAP(wrap1, STEP(3), MISSTEP, STEP(5)) { STEP(4); break; }
107 AFTER(after1, STEP(2); break) STEP(1);
112 FORELSE (i = 0; i < 10; i++) {
119 FORELSE (i = 0; i < 10; i++) {
127 MC_ACT(STEP(0); MC_GOTO(in_plain)) \
128 MC_LABEL(done_plain) MC_ACT(STEP(5); GOELSE(elsie)) \
129 MC_LABEL(in_plain) WRAP(outer_wrap, { STEP(1); }, \
133 WRAP(inner_wrap, { STEP(2); }, \
135 MC_GOTO(done_plain); }, \
144 #if __STDC_VERSION__ >= 199901 || defined(__cplusplus)
146 DECL(decl0, int j = 1) STEP(j);
150 FOR_FIZZBUZZ(fb, 19, 32) printf("%s\n", fb);
155 /*----- That's all, folks -------------------------------------------------*/