ae2f_docs
Loading...
Searching...
No Matches
main.c
1#include "./main.auto.h"
2#include <stdio.h>
3#include <stddef.h>
4
5int main(void);
6
7#define BOOK (ae2f_MAC_KEYWORD "(")
8
10#define dbg_puts(s) fputs(s, stdout)
11#else
12#define dbg_puts(s)
13#endif
14
15#define SZBOOK ((size_t)(sizeof(BOOK) - 1))
16
17char SEE[sizeof(BOOK)] = {
18 0,
19};
20
21#define prm_cmt prmbool.m_cmt
22#define prm_space_found prmbool.m_space_found
23#define prm_first prmbool.m_first
24
25char PARAM[SZPARAM + 2 + (CMT_REQUIRED) * 4] = {
26 0,
27};
28char TPARAMS[SZTPARAM + 1] = {
29 0,
30};
31
32char ENDL[] = " \\\n";
33
34#define STATE_OK 0
35#define STATE_OUTFAILED 1
36#define STATE_UNEXPECTED 2
37#define STATE_OVERRUN 3
38
39#define casenewlines
40 case '\n': case
41 '\r'
42
43#define caseblanks
44 case ' ': case
45 '\t': casenewlines
46
47
48#define isAlph(c)
49 ((((c) >= 'A') && ((c) <= 'Z')) || (((c) >= 'a') && ((c) <= 'z')))
50#define isNumber(c) (((c) >= '0') && ((c) <= '9'))
51#define isVarName(c) ((isAlph(c) || isNumber(c)) || (c) == '_')
52#define isNewLine(c) ((c) == '\n' || (c) == '\r')
53
54int main(void) {
55 /** register char prmtype = 0; */
56
57 register int c;
58 int l;
59 register int tprm = 0;
60 register int prm = 0;
61 register int stack = 0;
62
63 register struct __prmbool {
64 unsigned m_cmt : 1;
65 unsigned m_space_found : 1;
66 unsigned m_first : 1;
67 unsigned m_is_fn_arg : 1;
68 } prmbool = {0, 0, 0, 0 };
69
70
71 puts("#undef\t__ae2f_MACRO_GENERATED\n"
72 "#define\t__ae2f_MACRO_GENERATED\t1");
73 while ((c = fgetc(stdin)) != EOF) {
74
75 goto __START;
76__REST:
77 if (c != (BOOK)[0]) {
78 l = fputc(c, stdout);
79 if (l < 0)
80 return 1;
81 }
82 continue;
83
84__START:
85 if (c == '\"') goto STRING;
86
87 if (c == '#') {
88 const char _INCLUDE[sizeof("#include") - 1] = "#include";
89 char _SEE_INC[sizeof(_INCLUDE)];
90 size_t i = 1;
91 _SEE_INC[0] = '#';
92 fputc('#', stdout);
93 for (; i < (size_t)sizeof(_INCLUDE); i++) {
94 _SEE_INC[i] = (char)fgetc(stdin);
95 fputc(_SEE_INC[i], stdout);
96
97 if (_SEE_INC[i] != _INCLUDE[i]) {
98 break;
99 }
100 }
101
102 if (i == sizeof(_INCLUDE)) {
103 while ((c = fgetc(stdin)) != EOF && !isNewLine(c)) {
104 fputc(c, stdout);
105 }
106
107 puts("\n#undef __ae2f_MACRO_GENERATED\n"
108 "#define __ae2f_MACRO_GENERATED 1");
109 }
110 }
111
112 else if (c == (BOOK)[0]) {
113 size_t i = 1;
114 SEE[0] = (BOOK)[0];
115 for (; i < SZBOOK; i++) {
116 SEE[i] = (char)fgetc(stdin);
117
118 if (SEE[i] != (BOOK)[i]) {
119 SEE[i + 1] = 0;
120 break;
121 }
122 }
123
124 if (i < SZBOOK) {
125 if (fputs(SEE, stdout) < 0)
126 return 1;
127
128 continue;
129 }
130
131 l = fputs("#define _", stdout);
132
133 if (l < 0)
134 return STATE_OUTFAILED;
135
136 tprm = 0;
137 prm = 0;
138 stack = 0; /** init */
139 /** prmtype = 1; */
140
141 while (1) {
142 switch ((c = fgetc(stdin))) {
143 case EOF:
144 return STATE_UNEXPECTED;
145
146 case ')':
147 if(!(stack--))
148 goto TPARAMED;
149 break;
150
151 case '(':
152 stack++;
153 break;
154
155
156 default: {
157 TPARAMS[tprm] = c;
158 if (tprm == SZTPARAM)
159 return STATE_OVERRUN;
160 tprm++;
161 }
162
164 break;
165 }
166 } /** tparam */
167
168TPARAMED:
169 while (1) {
170 switch (c = fgetc(stdin)) {
171 case EOF:
172 return 1;
173 default:
174 l = fputc(c, stdout);
175 if (l < 0)
176 return STATE_OUTFAILED;
177 if (c == '(')
178 goto FNED;
180 break;
181 }
182 } /** fn */
183
184FNED:
185
186 dbg_puts(" \\\n\t/** tparam */ \\\n\t\t");
187 TPARAMS[tprm] = 0;
188 if (fputs(TPARAMS, stdout) < 0)
189 return STATE_OUTFAILED;
190
191 dbg_puts(" \\\n \\\n\t/** param */ \\\n\t\t");
192
193 prm = 0;
194 prm_cmt = 1;
195 prm_first = 1;
196 stack = 0;
197 prmbool.m_is_fn_arg = 0;
198
199 while (1) {
200
201 if (prm_cmt) {
202 if (fputs("/* ", stdout) < 0)
203 return STATE_OUTFAILED;
204 prm_cmt = 0;
205 }
206
207 switch (c = fgetc(stdin)) {
208 case EOF:
209 return 1;
210
212 fputc(' ', stdout);
213PRM_CASE_BLANK:
214 prm_space_found = 1;
215 break;
216
217 default:
218 if (!isVarName(c)) {
219 PARAM[prm] = 0;
220 fputs(PARAM, stdout);
221 fputc(c, stdout);
222 prm = 0;
223 goto PRM_CASE_BLANK;
224 }
225
226 if (prm_first) {
227 prm_first = 0;
228 if (fputc(',', stdout) < 0)
229 return STATE_OUTFAILED;
230 }
231
232 if (prm_space_found) {
233 PARAM[prm] = 0;
234 fputs(PARAM, stdout);
235 prm = 0;
236 prm_space_found = 0;
237 }
238
239 PARAM[prm] = c;
240 if (prm == SZPARAM)
241 return STATE_OVERRUN;
242 prm++;
243 break;
244
245 case '(':
246 if(prmbool.m_is_fn_arg) {
247 while((fgetc(stdin)) != ')');
248 } else {
249 stack++;
250 prmbool.m_is_fn_arg = 1;
251 }
252
253 break;
254
255 case ',':
256 prmbool.m_is_fn_arg = 0;
257 case ')':
258 if(c == ')' && stack--) {
259 break;
260 }
261
262 PARAM[prm] = c;
263 PARAM[prm + 1] = 0;
264
265 if (fputs(" */ ", stdout) < 0)
266 return STATE_OUTFAILED;
267
268 prm_cmt = 1;
269
270#if CMT_REQUIRED
271 if (c == ')') {
272 PARAM[prm] = ' ';
273 PARAM[prm + 1] = '\\';
274 PARAM[prm + 2] = '\n';
275 PARAM[prm + 3] = c;
276 PARAM[prm + 4] = 0;
277 l = fputs(PARAM, stdout);
278 } else
279#endif
280 l = fputs(PARAM, stdout);
281
282 if (l < 0)
283 return STATE_OUTFAILED;
284
285 prm = 0;
286
287 if (',' == c) {
288 dbg_puts(" \\\n\t\t");
289 goto PRM_CASE_BLANK;
290 } else {
291 goto PRMED;
292 }
293 }
294 }
295PRMED:
296
297 stack = 0;
298
299 while (1) {
300 switch (c = fgetc(stdin)) {
301 case EOF:
302 return 1;
303
305 ENDL[2] = c;
306 l = fputs(ENDL, stdout);
307 if (l < 0)
308 return STATE_OUTFAILED;
309
310 break;
311
312 default:
313 l = fputc(c, stdout);
314 if (l < 0)
315 return STATE_OUTFAILED;
316 break;
317
318 case '{':
319 if (!(++stack)) {
320 return STATE_OVERRUN;
321 }
322 l = fputc('{', stdout);
323 if (l < 0)
324 return STATE_OUTFAILED;
325 break;
326
327 case '}':
328 l = fputc('}', stdout);
329 if (l < 0)
330 return STATE_OUTFAILED;
331
332 if (!(--stack)) {
333 goto STACKED;
334 }
335 break;
336 }
337 }
338 /* stack */
339STACKED:;
340 }
341
342 else {
343 goto __REST;
344 }
345 }
346
347 puts(
348 "\n#undef\t__ae2f_MACRO_GENERATED\n"
349 "\n#define\t__ae2f_MACRO_GENERATED\t0\n"
350 );
351
352 return 0;
353
354STRING:
355 fputc('\"', stdout);
356 while((c = fgetc(stdin)) != EOF) {
357 switch(c) {
358 case '\"':
359 goto __REST;
360
361 case '\\':
362 fputc(c, stdout);
363 c = fgetc(stdin);
364
365 default:
366 fputc(c, stdout);
367 break;
368 }
369 }
370
371 return -1;
372}
#define ae2f_MAC_KEYWORD
Definition main.auto.h:1
#define SZPARAM
Definition main.auto.h:3
#define CMT_REQUIRED
Definition main.auto.h:2
#define SZTPARAM
Definition main.auto.h:4
#define prm_cmt
Definition main.c:21
#define STATE_OUTFAILED
Definition main.c:35
#define caseblanks
Definition main.c:43
#define isNewLine(c)
Definition main.c:52
#define SZBOOK
Definition main.c:15
#define dbg_puts(s)
Definition main.c:10
#define STATE_OVERRUN
Definition main.c:37
#define BOOK
Definition main.c:7
#define isNumber(c)
Definition main.c:50
#define prm_first
Definition main.c:23
#define casenewlines
Definition main.c:39
#define prm_space_found
Definition main.c:22
#define isAlph(c)
Definition main.c:48
#define STATE_UNEXPECTED
Definition main.c:36
#define isVarName(c)
Definition main.c:51