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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
// main.cpp
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../source/core/secure-crt.h"
struct StringSpan
{
char const* begin;
char const* end;
};
StringSpan makeEmptySpan()
{
StringSpan span = { 0, 0 };
return span;
}
StringSpan makeSpan(char const* begin, char const* end)
{
StringSpan span;
span.begin = begin;
span.end = end;
return span;
}
struct Node
{
// The textual range covered by this node
// (does not including the opening sigil)
StringSpan span;
// The textual range of the identifier
// part of this node (if any)
StringSpan id;
// The textual range of the body part of
// this node (if any)
StringSpan body;
// The parent of this node
Node* parent;
// The first child node of this node
Node* firstChild;
// The next node belonging to the same parent
Node* nextSibling;
};
struct NodeBuilder
{
Node* node;
Node** childLink;
unsigned int curlyCount;
unsigned int nestedCurlyCount;
};
Node* createNode()
{
Node* result = (Node*) malloc(sizeof(Node));
memset(result, 0, sizeof(Node));
return result;
}
void addNode(
NodeBuilder* builder,
Node* node)
{
node->parent = builder->node;
*builder->childLink = node;
builder->childLink = &node->nextSibling;
}
bool isAlpha(int c)
{
return ((c >= 'a') && (c <= 'z'))
|| ((c >= 'A') && (c <= 'Z'))
|| (c == '_');
}
Node* readInput(
char const* inputBegin,
char const* inputEnd)
{
static const int kMaxDepth = 16;
NodeBuilder nodeStack[kMaxDepth];
NodeBuilder* nodeStackEnd = &nodeStack[kMaxDepth];
Node* root = createNode();
root->span.begin = inputBegin;
root->span.end = inputEnd;
root->body = root->span;
NodeBuilder* builder = &nodeStack[0];
builder->node = root;
builder->childLink = &root->firstChild;
builder->curlyCount = (unsigned int)(-1);
builder->nestedCurlyCount = 0;
char const* cursor = inputBegin;
for(;;)
{
int c = *cursor;
switch(c)
{
default:
// ordinary text, so we continue the current span
cursor++;
continue;
case 0:
// possible end of input
if(cursor == inputEnd)
{
return root;
}
// Otherwise it is just an embedded NULL
cursor++;
continue;
case '$':
// We've hit our dedicated meta-character, which means
// we are being asked to do some kind of splicing.
{
cursor++;
switch(*cursor)
{
case '$':
// This is an escaped single `$`.
// We need to create an empty node to
// represent it
{
Node* node = createNode();
addNode(builder, node);
node->span.begin = cursor;
cursor++;
node->span.end = cursor;
continue;
}
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '\'':
case '\"':
case ')':
// HACK: allow existing usage through
cursor++;
continue;
default:
break;
}
Node* node = createNode();
addNode(builder, node);
node->span.begin = cursor-1;
char const* nodeBegin = cursor;
// Piece one is an optional "identifier" section
node->id.begin = cursor;
while( isAlpha(*cursor) )
{
cursor++;
}
node->id.end = cursor;
// Next we have an optional `{}`-delimeted span
if( *cursor == '{' )
{
unsigned int count = 0;
while( *cursor == '{' )
{
count++;
cursor++;
}
node->body.begin = cursor;
assert(builder != nodeStackEnd);
builder++;
builder->node = node;
builder->childLink = &node->firstChild;
builder->curlyCount = count;
builder->nestedCurlyCount = 0;
}
else
{
node->body.begin = cursor;
node->body.end = cursor;
node->span.end = cursor;
}
continue;
}
break;
case '{':
builder->nestedCurlyCount++;
cursor++;
continue;
case '}':
{
// Possible end of an open span
unsigned int count = 0;
char const* cc = cursor;
while( *cc == '}' )
{
count++;
cc++;
}
unsigned int expected = builder->curlyCount;
if( expected == 1 )
{
unsigned int nested = builder->nestedCurlyCount;
// The user isn't guarding for unmatched braces,
// so some of these braces might go to cancel
// out any open braces inside this scope:
if( count > nested )
{
// There are more available braces than our
// nesting depth, so we need to close them
// out and move on.
cursor += builder->nestedCurlyCount;
count -= builder->nestedCurlyCount;
builder->nestedCurlyCount = 0;
}
else
{
// These braces are only being used to close out
// nested constructs that were already opened.
builder->nestedCurlyCount -= count;
cursor += count;
continue;
}
}
if(count >= expected)
{
// There are enough braces there to close out this construct
Node* node = builder->node;
node->body.end = cursor;
cursor += expected;
node->span.end = cursor;
builder--;
continue;
}
else
{
cursor += count;
continue;
}
}
}
}
}
void emitRaw(
FILE* stream,
char const* begin,
char const* end)
{
// We will write the raw text to our output file.
// TODO: need to output `#line` directives as well
fputs("sb << \"", stream);
for( char const* cc = begin; cc != end; ++cc )
{
int c = *cc;
switch( c )
{
case '\\':
fputs("\\\\", stream);
break;
case '\r': break;
case '\t': fputs("\\t", stream); break;
case '\"': fputs("\\\"", stream); break;
case '\n':
fputs("\\n\";\n", stream);
fputs("sb << \"", stream);
break;
default:
if((c >= 32) && (c <= 126))
{
fputc(c, stream);
}
else
{
assert(false);
}
}
}
fprintf(stream, "\";\n");
}
void emitCode(
FILE* stream,
char const* begin,
char const* end)
{
for( auto cc = begin; cc != end; ++cc )
{
if(*cc == '\r')
continue;
fputc(*cc, stream);
}
}
void emitNode(
FILE* stream,
Node* node)
{
// TODO: need to look at the identifier part of the node in case
// there are custom instructions there...
char const* cursor = node->body.begin;
for( auto nn = node->firstChild; nn; nn = nn->nextSibling )
{
emitCode(stream, cursor, nn->span.begin);
cursor = nn->span.end;
}
emitCode(stream, cursor, node->body.end);
}
void emitBody(
FILE* stream,
Node* node)
{
char const* cursor = node->body.begin;
for( auto nn = node->firstChild; nn; nn = nn->nextSibling )
{
emitRaw(stream, cursor, nn->span.begin);
emitNode(stream, nn);
cursor = nn->span.end;
}
emitRaw(stream, cursor, node->body.end);
}
void usage(char const* appName)
{
fprintf(stderr, "usage: %s <input>\n", appName);
}
char* readAllText(char const * fileName)
{
FILE * f;
fopen_s(&f, fileName, "rb");
if (!f)
{
return "";
}
else
{
fseek(f, 0, SEEK_END);
auto size = ftell(f);
char * buffer = new char[size + 1];
memset(buffer, 0, size + 1);
fseek(f, 0, SEEK_SET);
fread(buffer, sizeof(char), size, f);
fclose(f);
return buffer;
}
}
void writeAllText(char const *srcFileName, char const* fileName, char* content)
{
FILE * f = nullptr;
fopen_s(&f, fileName, "wb");
if (!f)
{
printf("%s(0): error G0001: cannot write file %s\n", srcFileName, fileName);
}
else
{
fwrite(content, 1, strlen(content), f);
fclose(f);
}
}
int main(
int argc,
char** argv)
{
char** argCursor = argv;
char** argEnd = argv + argc;
char const* appName = "slang-generate";
if( argCursor != argEnd )
{
appName = *argCursor++;
}
char const* inputPath = nullptr;
if( argCursor != argEnd )
{
inputPath = *argCursor++;
}
else
{
usage(appName);
exit(1);
}
if( argCursor != argEnd )
{
usage(appName);
exit(1);
}
// Read the contents o the file and translate it into a "template" file
FILE* inputStream;
fopen_s(&inputStream, inputPath, "rb");
fseek(inputStream, 0, SEEK_END);
size_t inputSize = ftell(inputStream);
fseek(inputStream, 0, SEEK_SET);
char* input = (char*) malloc(inputSize + 1);
fread(input, inputSize, 1, inputStream);
input[inputSize] = 0;
char const* inputEnd = input + inputSize;
Node* node = readInput(input, inputEnd);
// write output to a temporary file first
char outputPath[1024];
sprintf_s(outputPath, "%s.temp.h", inputPath);
FILE* outputStream;
fopen_s(&outputStream, outputPath, "w");
emitBody(outputStream, node);
fclose(outputStream);
// update final output only when content has changed
char outputPathFinal[1024];
sprintf_s(outputPathFinal, "%s.h", inputPath);
char * allTextOld = readAllText(outputPathFinal);
char * allTextNew = readAllText(outputPath);
if (strcmp(allTextNew, allTextOld) != 0)
{
writeAllText(inputPath, outputPathFinal, allTextNew);
}
remove(outputPath);
return 0;
}
|