/
generate.c
295 lines (262 loc) · 6.85 KB
/
generate.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Generate a .c file for parsing commands, and a .h file for functions the user
// must provide to process them.
#include "co.h"
#define CO_WRAP_COLUMN 80
static uint32 coLinePos;
static char *coPrefix;
// Write text to coFile. Print a new line if there is not enough space.
void coPrintW(
char *newLineText,
char *format,
...)
{
char *buf;
va_list ap;
uint32 length;
va_start(ap, format);
buf = utVsprintf(format, ap);
length = strlen(buf);
if(length + coLinePos > CO_WRAP_COLUMN) {
coLinePos = fprintf(coFile, "\n%s", newLineText);
}
coLinePos += fprintf(coFile, "%s", buf);
}
// Call coPrint, and print a new line.
void coPrintlnW(
char *newLineText,
char *format,
...)
{
char *buf;
va_list ap;
uint32 length;
va_start(ap, format);
buf = utVsprintf(format, ap);
length = strlen(buf);
if(length + coLinePos > CO_WRAP_COLUMN) {
fprintf(coFile, "\n%s", newLineText);
}
fprintf(coFile, "%s\n", buf);
coLinePos = 0;
}
// Write text to coFile.
void coPrint(
char *format,
...)
{
char *buf;
va_list ap;
va_start(ap, format);
buf = utVsprintf(format, ap);
coLinePos += fprintf(coFile, "%s", buf);
}
// Call coPrint, and print a new line.
void coPrintln(
char *format,
...)
{
char *buf;
va_list ap;
va_start(ap, format);
buf = utVsprintf(format, ap);
fprintf(coFile, "%s\n", buf);
coLinePos = 0;
}
// Declare enumerated types.
static void declareEnums(void)
{
coEnum theEnum;
coEntry entry;
coForeachRootEnum(coTheRoot, theEnum) {
coPrint("typedef enum {");
coForeachEnumEntry(theEnum, entry) {
coPrintW(" ", "%s", coEntryGetName(entry));
if(coEntryGetNextEnumEntry(entry) != coEntryNull) {
coPrint(", ");
}
} coEndEnumEntry;
coPrintlnW(" ", "} %s%s;", coPrefix, coEnumGetName(theEnum));
coPrintln("");
} coEndRootEnum;
}
static void writeType(coTyperef typeref, uint16 depth);
// Indent by the number of depth levels.
static void indent(
uint16 depth)
{
while(depth--) {
coPrint(" ");
}
}
// Write the tuple type.
static void writeTupleFields(
coType type,
uint16 depth)
{
coField field;
coForeachTypeField(type, field) {
indent(depth);
writeType(coFieldGetTyperef(field), depth);
coPrintln(" %s;", coFieldGetName(field));
} coEndTypeField;
}
// Write function parameters.
static void writeParameters(
coType type)
{
coField field;
coForeachTypeField(type, field) {
if(coTypeGetFirstField(type) != field) {
coPrintW(" ", ", ");
}
writeType(coFieldGetTyperef(field), 1);
coPrintW(" ", " %s", coFieldGetName(field));
} coEndTypeField;
}
// Write the type definition.
static void writeType(
coTyperef typeref,
uint16 depth)
{
coType type = coTyperefGetType(typeref);
switch(coTypeGetValtype(type)) {
case CO_INT:
coPrintW(" ", "int%u", coTypeGetWidth(type));
break;
case CO_UINT:
coPrintW(" ", "int%u", coTypeGetWidth(type));
break;
case CO_STRING:
coPrintW(" ", "char *");
break;
case CO_FLOAT:
coPrintW(" ", "float");
break;
case CO_DOUBLE:
coPrintW(" ", "double");
break;
case CO_BOOL:
coPrintW(" ", "bool");
break;
case CO_IDENT:
coPrintW(" ", "%s%s", coPrefix, utSymGetName(coTypeGetNameVal(type)));
break;
case CO_TUPLE:
coPrintln("struct {");
writeTupleFields(type, depth + 1);
indent(depth);
coPrint("}");
break;
case CO_POINTER:
writeType(coTypeGetTyperefVal(type), depth);
coPrintW(" ", " *");
break;
default:
utExit("unknown type");
}
}
// Declare a typedef.
static void declareTypedef(
coTypedef theTypedef)
{
coPrint("typedef ");
writeType(coTypedefGetTyperef(theTypedef), 0);
coPrintlnW(" ", " %s%s;", coPrefix, coTypedefGetName(theTypedef));
coPrintln("");
}
// Declare the typedefs.
static void declareTypedefs(void)
{
coTypedef theTypedef;
coForeachRootTypedef(coTheRoot, theTypedef) {
declareTypedef(theTypedef);
} coEndRootTypedef;
}
// Declare a function.
static void declareFunction(
coFunction function)
{
coTyperef returnType = coFunctionGetReturnTyperef(function);
coType paramType = coTyperefGetType(coFunctionGetParameterTyperef(function));
if(returnType == coTyperefNull) {
coPrint("void");
} else {
writeType(returnType, 0);
}
coPrint(" %s(", coFunctionGetName(function));
if(coTypeGetFirstField(paramType) == coFieldNull) {
coPrint("void");
} else {
writeParameters(paramType);
}
coPrintln(");");
coPrintln("");
}
// Declare functions.
static void declareFunctions(void)
{
coFunction function;
coForeachRootFunction(coTheRoot, function) {
coPrintln("/* %s */", coFunctionGetDescription(function));
declareFunction(function);
} coEndRootFunction;
}
// Generate the header file for functions the user must provide.
static void generateHFile(void)
{
declareEnums();
declareTypedefs();
declareFunctions();
}
// Create a wrapper for the function. These things take argc argv, and fill out
// the various structures needed by the user's function.
static void writeFunctionWrapper(
coFunction function)
{
}
// Generate code to parse commands and call the related function.
static void generateCFile(
char *HName)
{
coFunction function;
coPrintln("#include <ddutil.h>");
coPrintln("#include \"%s\"", HName);
coPrintln("#include \"rpclite.h\"\n");
coForeachRootFunction(coTheRoot, function) {
writeFunctionWrapper(function);
} coEndRootFunction;
coPrint(
"/* Register functions */\n"
"void rlRegisterFunctions(\n"
" rlServer server)\n"
"{\n");
coForeachRootFunction(coTheRoot, function) {
coPrintln(" rlServerRegisterFunction(server, \"%s\", coFunctionGetName(function));");
} coEndRootFunction;
coPrintln("}");
}
// Generate both the hearder file and command parser.
void coGenerateCommandParser(
char *fileName,
char *prefix)
{
char *HName = utAllocString(utReplaceSuffix(fileName, ".h"));
char *CName = utAllocString(utReplaceSuffix(fileName, ".c"));
coPrefix = prefix;
coFile = fopen(HName, "w");
if(coFile == NULL) {
utExit("Unable to open file %s for writing", HName);
}
coLinePos = 0;
generateHFile();
fclose(coFile);
coFile = fopen(CName, "w");
if(coFile == NULL) {
utExit("Unable to open file %s for writing", CName);
}
coLinePos = 0;
generateCFile(HName);
fclose(coFile);
utFree(HName);
utFree(CName);
}