-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
151 lines (130 loc) · 3.53 KB
/
example.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/* This Works is placed under the terms of the Copyright Less License,
* see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY.
*
* Read: This is free as free beer, free speech and free baby.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "pp_repeat.h"
static void
OOPS(const char *s, ...)
{
va_list list;
fprintf(stderr, "%s", s);
va_start(list, s);
for (; s; s=va_arg(list, const char *))
fprintf(stderr, ": %s", s);
va_end(list);
fprintf(stderr, "\n");
fflush(stderr);
exit(23);
}
#define TYPE long long
#define PTYPE "%lld"
#define STYPE strtoll
/* Here is a recipe on how to use PP_APPLY3() properly
*/
#ifndef LINEFEED
#define LINEFEED /* cpp -DLINEFEED=LINEFEED example.c | sed -n 's/LINEFEED/\n/gp' */
#endif
#define ENUM1(...) LINEFEED enum my_enums {
#define ENUM2(N,NAME,...) LINEFEED MY_ENUM_##NAME,
#define ENUM3(...) LINEFEED };
#define FUNCTION1(...)
#define FUNCTION2(N,NAME,ARGS,FN,HELP) LINEFEED TYPE MY_FN_##NAME ARGS { return FN; }
#define FUNCTION3(...)
struct my_struct
{
const char *name;
TYPE (*fn)();
int nargs;
const char *help;
};
#define STRUCT1(...) LINEFEED struct my_struct my_struct_data[] = {
#define STRUCT2(N,NAME,ARGS,FN,HELP) LINEFEED { #NAME, (TYPE(*)())MY_FN_##NAME, N, HELP },
#define STRUCT3(...) LINEFEED 0 };
/* Generate the ENUM constants with offsets into the structure
* including with the function
*/
PP_APPLY3(
/* Note: Perhaps we can calculate the number of args using a macro.
* But my experiments with this failed for unknown reason.
* (PP_NARGS did not work in the expansion)
* vvv */
( (2, add, (TYPE x, TYPE y), x+y, "add two numbers")
, (2, sub, (TYPE x, TYPE y), x-y, "subtract second number from first one")
, (1, neg, (TYPE x), -x, "negate number")
, (2, mul, (TYPE x, TYPE y), x*y, "multiply two numbers")
/* you can add more functions here easily */
)
, ENUM
, FUNCTION
, STRUCT
)
static int
usage(void)
{
struct my_struct *ptr;
for (ptr=my_struct_data; ptr->name; ptr++)
{
printf("%s\t%s\n", ptr->name, ptr->help);
switch (ptr->nargs)
{
default:
OOPS("internal error: wrong argument count for %s", ptr->name, NULL);
case 1:
printf("\texample: %s %d: "PTYPE"\n", ptr->name, 1, ((TYPE (*)(TYPE))ptr->fn)(1));
break;
case 2:
printf("\texample: %s %d %d: "PTYPE"\n", ptr->name, 2, 3, ((TYPE (*)(TYPE,TYPE))ptr->fn)(1, 2));
break;
}
}
return 42;
}
struct _
{
int n;
char **a;
};
/* This is not meant to be optimal.
* In fact it is probably the most stupid implementation.
*/
static TYPE
answer(struct _ *_)
{
struct my_struct *ptr;
const char *arg;
char *end;
TYPE n;
/* get the next token */
if (!_->n--)
OOPS("out of arguments", NULL);
arg = *_->a++;
/* check token being a function */
for (ptr=my_struct_data; ptr->name; ptr++)
if (!strcmp(arg, ptr->name))
switch (ptr->nargs)
{
TYPE x;
case 1: return ((TYPE (*)(TYPE))ptr->fn)(answer(_));
case 2: x = answer(_); return ((TYPE (*)(TYPE,TYPE))ptr->fn)(x,answer(_));
}
n = STYPE(arg, &end, 0);
if (!end || *end)
OOPS("cannot interpret", arg, NULL);
return n;
}
int
main(int argc, char **argv)
{
if (argc<2)
return usage();
struct _ args = { argc-1, argv+1 };
printf("the answer is "PTYPE"\n", answer(&args));
if (args.n)
OOPS("not all arguments were used starting with", *args.a, NULL);
return 0;
}