-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
Copy pathloops.js
895 lines (784 loc) · 21.3 KB
/
loops.js
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
#!/usr/bin/env node
/**
* @license Apache-2.0
*
* Copyright (c) 2023 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var path = require( 'path' );
var logger = require( 'debug' );
var readDir = require( '@stdlib/fs/read-dir' ).sync;
var unlink = require( '@stdlib/fs/unlink' ).sync;
var readFile = require( '@stdlib/fs/read-file' ).sync;
var readJSON = require( '@stdlib/fs/read-json' ).sync;
var writeFile = require( '@stdlib/fs/write-file' ).sync;
var replace = require( '@stdlib/string/replace' );
var substringBefore = require( '@stdlib/string/substring-before' );
var substringAfter = require( '@stdlib/string/substring-after' );
var uppercase = require( '@stdlib/string/uppercase' );
var dtypes = require( '@stdlib/ndarray/dtypes' );
var safeCasts = require( '@stdlib/ndarray/safe-casts' );
var dtypeChar = require( '@stdlib/ndarray/base/dtype-char' );
var dtypeDesc = require( '@stdlib/ndarray/base/dtype-desc' );
var dtype2c = require( '@stdlib/ndarray/base/dtype2c' );
var char2dtype = require( '@stdlib/ndarray/base/char2dtype' );
var bytesPerElement = require( '@stdlib/ndarray/base/bytes-per-element' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
var numel = require( '@stdlib/ndarray/base/numel' );
var gscal = require( '@stdlib/blas/base/gscal' );
var filledarray = require( '@stdlib/array/filled' );
var currentYear = require( '@stdlib/time/current-year' );
var format = require( '@stdlib/string/format' );
// VARIABLES //
var FOPTS = {
'encoding': 'utf8'
};
// Logger:
var debug = logger( 'ndarray-nullary-loops:script' );
// Get the current year:
var CURRENT_YEAR = currentYear().toString();
// Specify the copyright holder:
var COPYRIGHT = 'The Stdlib Authors';
// Templates:
var TMPL_HEADER = readFile( path.join( __dirname, 'templates', 'header.txt' ), FOPTS );
var TMPL_SOURCE = readFile( path.join( __dirname, 'templates', 'source.txt' ), FOPTS );
var TMPL_README = readFile( path.join( __dirname, 'templates', 'docs.txt' ), FOPTS );
// Output directories:
var INCLUDE_DIR = path.resolve( __dirname, '..', 'include', 'stdlib', 'ndarray', 'base', 'nullary' );
var SRC_DIR = path.resolve( __dirname, '..', 'src' );
// Main header file:
var INCLUDE_MAIN = INCLUDE_DIR + '.h';
// Manifest file:
var MANIFEST = path.resolve( __dirname, '..', 'manifest.json' );
// README file:
var README = path.resolve( __dirname, '..', 'README.md' );
// Data types to exclude when generating loops:
var EXCLUDE_DTYPES = [ 'binary', 'generic', 'uint8c' ];
// Resolve a list of dtypes for which we want to create loops:
var DTYPES = filter( dtypes(), EXCLUDE_DTYPES ).sort();
// Define "special" loops, which cannot be readily generated according to standardized rules:
var SPECIAL_LOOPS = [];
// Hash containing C macro names:
var MACROS = {
'default': 'CLBK',
'nocast': 'CLBK_RET_NOCAST',
'rcast': 'CLBK_RET_CAST_FCN'
};
// Regular expression to test for a "loop" file:
var RE_LOOP_FILE = /^[a-z](?:_as_[a-z]|)\.(?:h|c)$/;
// Regular expression to test for a "loop" file in the manifest.json:
var RE_MANIFEST_LOOP_FILE = /\.\/src\/[a-z](?:_as_[a-z]|)\.c$/;
// Regular expression to match output array dtype characters:
var RE_SIGNATURE = /^([a-z])/;
// Regular expression to match callback dtype characters:
var RE_CALLBACK = /_as_([a-z])$/;
// Specify array shapes:
var SHAPES = [
[],
[ 3 ],
[ 2, 2 ],
[ 2, 2, 2 ],
[ 1, 2, 2, 2 ],
[ 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 1, 1, 2, 2, 2 ],
[ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2 ]
];
// Array order:
var ORDER = 'row-major';
// FUNCTIONS //
/**
* Tests whether a one-letter character abbreviation corresponds to a complex number data type.
*
* @private
* @param {string} ch - one-letter character abbreviation
* @returns {boolean} test result
*
* @example
* var bool = isComplexChar( 'z' );
* // returns true
*/
function isComplexChar( ch ) {
return ( ch === 'c' || ch === 'z' );
}
/**
* Returns a callback body based on an output data type.
*
* @private
* @param {string} ch1 - one-letter character abbreviation for output data type
* @returns {string} callback body
*/
function callbackBody( ch1 ) {
if ( isComplexChar( ch1 ) ) {
return '// ...';
}
if ( ch1 === 'd' ) {
return 'return 10.0;';
}
if ( ch1 === 'f' ) {
return 'return 10.0f;';
}
return 'return 10;';
}
/**
* Removes a list of elements from a provided list.
*
* @private
* @param {ArrayLikeObject} src - list to filter
* @param {ArrayLikeObject} list - items to remove
* @returns {ArrayLikeObject} filtered list
*
* @example
* var src = [ 'a', 'b', 'c', 'd' ];
* var list = [ 'b', 'd' ];
*
* var out = filter( src, list );
* // returns [ 'a', 'c' ]
*/
function filter( src, list ) {
var out;
var M;
var N;
var v;
var i;
var j;
M = src.length;
N = list.length;
out = [];
for ( i = 0; i < M; i++ ) {
v = src[ i ];
for ( j = 0; j < N; j++ ) {
if ( v === list[ j ] ) {
break;
}
}
if ( j === N ) {
out.push( v );
}
}
return out;
}
/**
* Returns the intersection of two sorted lists.
*
* @private
* @param {ArrayLikeObject} list1 - first sorted list
* @param {ArrayLikeObject} list2 - second sorted list
* @returns {ArrayLikeObject} result
*
* @example
* var list1 = [ 'a', 'b', 'c', 'd' ];
* var list2 = [ 'b', 'd', 'e' ];
*
* var out = intersection( list1, list2 );
* // returns [ 'b', 'd' ]
*/
function intersection( list1, list2 ) {
var out;
var M;
var N;
var v;
var i;
var j;
var k;
M = list1.length;
N = list2.length;
out = [];
k = 0;
for ( i = 0; i < M; i++ ) {
if ( k >= N ) {
break;
}
v = list1[ i ];
for ( j = k; j < N; j++ ) {
if ( v === list2[ j ] ) {
k = j + 1;
out.push( v );
break;
}
}
}
return out;
}
/**
* Removes loop files from a directory.
*
* @private
* @param {string} dir - directory
*/
function removeLoopFiles( dir ) {
var list;
var i;
list = readDir( dir );
for ( i = 0; i < list.length; i++ ) {
if ( RE_LOOP_FILE.test( list[ i ] ) ) {
unlink( path.join( dir, list[ i ] ) );
}
}
}
/**
* Generates a list of loop signatures from a list of data types.
*
* @private
* @param {StringArray} dtypes - list of data types
* @returns {StringArray} list of loop signatures
*/
function signatures( dtypes ) {
var casts;
var out;
var ch1;
var ch2;
var t1;
var t2;
var N;
var s;
var i;
var j;
N = dtypes.length;
// Generate the list of signatures:
out = [];
for ( i = 0; i < N; i++ ) {
t1 = dtypes[ i ];
// Resolve single-letter dtype abbreviation:
ch1 = dtypeChar( t1 );
// Generate the input/output array signature:
s = ch1; // e.g., d
out.push( s );
// Resolve the list of safe casts for the input dtype:
casts = safeCasts( t1 );
// Remove the excluded dtypes:
casts = filter( casts, EXCLUDE_DTYPES );
// Remove safe casts which are not among the supported output dtypes:
casts = intersection( dtypes, casts.sort() );
// Generate signatures for allowed casts:
for ( j = 0; j < casts.length; j++ ) {
t2 = casts[ j ];
if ( t2 === t1 ) {
// For float dtypes, allow downcasting from a higher precision...
if ( ch1 === 'f' ) {
out.push( ch1+'_as_d' ); // e.g., `f` => `f_as_d`
} else if ( ch1 === 'c' ) {
out.push( ch1+'_as_z' ); // e.g., `c` => `c_as_z`
}
continue;
}
ch2 = dtypeChar( t2 );
out.push( ch2+'_as_'+ch1 ); // e.g., `z_as_d`
}
}
// Append any special loops:
for ( i = 0; i < SPECIAL_LOOPS.length; i++ ) {
out.push( SPECIAL_LOOPS[ i ] );
}
return out.sort();
}
/**
* Defines byte arrays in a provided template string.
*
* @private
* @param {string} tmpl - template string
* @param {PositiveInteger} nb1 - bytes per element for output array
* @returns {string} updated string
*/
function defineByteArrays( tmpl, nb1 ) {
var bytes;
var tmp;
var N;
var i;
bytes = filledarray( 0, nb1, 'generic' ).join( ', ' );
tmpl = replace( tmpl, '{{OUTPUT_NDARRAY_BYTES_0D}}', bytes );
for ( i = 1; i < SHAPES.length; i++ ) {
N = numel( SHAPES[ i ] );
tmp = '{{OUTPUT_NDARRAY_BYTES_'+i+'D}}';
bytes = filledarray( 0, nb1*N, 'generic' ).join( ', ' );
tmpl = replace( tmpl, tmp, bytes );
}
return tmpl;
}
/**
* Defines array strides in a provided template string.
*
* @private
* @param {string} tmpl - template string
* @param {PositiveInteger} nb1 - bytes per element for output array
* @returns {string} updated string
*/
function defineStrides( tmpl, nb1 ) {
var strides;
var tmp;
var st;
var i;
for ( i = 1; i < SHAPES.length; i++ ) {
strides = shape2strides( SHAPES[ i ], ORDER );
tmp = '{{OUTPUT_NDARRAY_STRIDES_'+i+'D}}';
st = gscal( strides.length, nb1, strides.slice(), 1 );
tmpl = replace( tmpl, tmp, st.join( ', ' ) );
}
return tmpl;
}
/**
* Defines array shapes in a provided template string.
*
* @private
* @param {string} tmpl - template string
* @returns {string} updated string
*/
function defineShapes( tmpl ) {
var i;
for ( i = 1; i < SHAPES.length; i++ ) {
tmpl = replace( tmpl, '{{NDARRAY_SHAPE_'+i+'D}}', SHAPES[ i ].join( ', ' ) );
}
return tmpl;
}
/**
* Creates a header file for a provided loop signature.
*
* @private
* @param {string} signature - loop signature
* @throws {Error} unexpected error
*/
function createHeaderFile( signature ) {
var fpath;
var file;
var err;
file = replace( TMPL_HEADER, '{{YEAR}}', CURRENT_YEAR );
file = replace( file, '{{COPYRIGHT}}', COPYRIGHT );
file = replace( file, '{{INCLUDE_GUARD}}', uppercase( signature ) );
file = replace( file, '{{SIGNATURE}}', signature );
fpath = path.join( INCLUDE_DIR, signature+'.h' );
debug( 'Creating header file: %s', fpath );
err = writeFile( fpath, file, FOPTS );
if ( err ) {
throw err;
}
}
/**
* Creates header files for a list of loop signatures.
*
* @private
* @param {StringArray} signatures - list of loop signatures
*/
function createHeaderFiles( signatures ) {
var i;
for ( i = 0; i < signatures.length; i++ ) {
createHeaderFile( signatures[ i ] );
}
}
/**
* Creates a source file for a provided loop signature.
*
* @private
* @param {string} signature - loop signature
* @throws {Error} unexpected error
*/
function createSourceFile( signature ) {
var match1;
var match2;
var macro;
var fpath;
var file;
var args;
var err;
var inc;
var tmp;
var ch1;
var ct1;
var nb1;
var t1;
file = replace( TMPL_SOURCE, '{{YEAR}}', CURRENT_YEAR );
file = replace( file, '{{COPYRIGHT}}', COPYRIGHT );
file = replace( file, '{{SIGNATURE}}', signature );
// Ensure the appropriate header files are included in source files:
inc = [];
if ( /c/.test( signature ) ) {
inc.push( '#include "stdlib/complex/float32.h"' );
}
if ( /z/.test( signature ) ) {
inc.push( '#include "stdlib/complex/float64.h"' );
}
if ( inc.length ) {
file = replace( file, '{{INCLUDES}}', '\n'+inc.join( '\n' ) );
} else {
file = replace( file, '{{INCLUDES}}', '' );
}
// Ensure the appropriate header files are included in source documentation examples:
inc = [];
tmp = signature.substring( 3 ); // explicit callback signature; e.g., _as_c, _as_z
if ( tmp === '' ) {
tmp = signature.substring( 0, 1 ); // implicit callback signature; e.g., c, z, d, f, etc
}
if ( /c/.test( tmp ) ) {
inc.push( '#include "stdlib/complex/float32.h"' );
}
if ( /z/.test( tmp ) ) {
inc.push( '#include "stdlib/complex/float64.h"' );
}
if ( inc.length ) {
file = replace( file, '{{EXAMPLE_INCLUDES}}', '\n* '+inc.join( '\n* ' ) );
} else {
file = replace( file, '{{EXAMPLE_INCLUDES}}', '' );
}
// Resolve the array data types:
match1 = signature.match( RE_SIGNATURE );
ch1 = match1[ 1 ];
t1 = char2dtype( ch1 );
// Define array data types:
file = replace( file, '{{OUTPUT_NDARRAY_DTYPE_UPPER}}', uppercase( t1 ) );
file = replace( file, '{{OUTPUT_NDARRAY_DTYPE_LOWER}}', t1 );
// Define the number of bytes per element for the respective arrays:
nb1 = bytesPerElement( t1 );
file = replace( file, '{{OUTPUT_NDARRAY_BYTES_PER_ELEMENT}}', nb1.toString() );
// Define the array shapes:
file = defineShapes( file );
// Define underlying byte arrays:
file = defineByteArrays( file, nb1 );
// Define array strides:
file = defineStrides( file, nb1 );
// Resolve the callback parameter data types:
match2 = signature.match( RE_CALLBACK );
if ( match2 ) {
ch1 = match2[ 1 ];
ct1 = dtype2c( char2dtype( ch1 ) );
} else {
ct1 = dtype2c( t1 );
}
file = replace( file, '{{CALLBACK_RETURN_DTYPE}}', ct1 );
file = replace( file, '{{CALLBACK_BODY}}', callbackBody( ch1 ) );
// Resolve the 0D callback expression:
if ( match2 ) {
if ( ch1 === 'z' ) {
if ( match1[ 1 ] === 'c' ) { // e.g., c_as_z
tmp = format( 'stdlib_complex128_to_%s( f() )', t1 );
} else {
// Based on safe casting rules, we shouldn't reach here.
throw new Error( format( 'unexpected error. Signature: %s', signature ) );
}
} else if ( ch1 === 'c' ) { // e.g., z_as_c
if ( match1[ 1 ] === 'z' ) {
tmp = format( 'stdlib_complex128_from_%s( f() )', char2dtype( ch1 ) );
} else {
// Based on safe casting rules, we shouldn't reach here.
throw new Error( format( 'unexpected error. Signature: %s', signature ) );
}
} else if ( match1[ 1 ] === 'z' ) { // e.g., z_as_d
tmp = format( 'stdlib_complex128_from_%s( f() )', char2dtype( ch1 ) );
} else if ( match1[ 1 ] === 'c' ) { // e.g., c_as_f
tmp = format( 'stdlib_complex64_from_%s( f() )', char2dtype( ch1 ) );
} else { // e.g., d_as_f, f_as_d
tmp = format( '(%s)f()', dtype2c( t1 ) );
}
} else { // e.g., c, z, f, d, b, etc
tmp = format( 'f()' );
}
file = replace( file, '{{CALLBACK_EXPRESSION_0D}}', tmp );
// Resolve the loop macro:
if ( match2 ) {
args = [ dtype2c( t1 ) ];
if ( isComplexChar( match1[ 1 ] ) ) {
macro = MACROS.rcast;
if ( ch1 === 'z' ) {
args.push( 'stdlib_complex128_to_'+t1 ); // e.g., `c_as_z`
} else {
args.push( 'stdlib_'+t1+'_from_'+char2dtype( ch1 ) ); // e.g., `z_as_d`
}
} else { // e.g., `f_as_d`
macro = MACROS.default;
}
} else if ( /[cz]/.test( signature ) ) {
macro = MACROS.nocast;
args = [ ct1 ];
} else {
macro = MACROS.default;
args = [ ct1 ];
}
file = replace( file, '{{LOOP_MACRO}}', macro );
file = replace( file, '{{LOOP_MACRO_ARGUMENTS}}', args.join( ', ' ) );
// Create the source file:
fpath = path.join( SRC_DIR, signature+'.c' );
debug( 'Creating source file: %s', fpath );
err = writeFile( fpath, file, FOPTS );
if ( err ) {
throw err;
}
}
/**
* Creates source files for a list of loop signatures.
*
* @private
* @param {StringArray} signatures - list of loop signatures
*/
function createSourceFiles( signatures ) {
var i;
for ( i = 0; i < signatures.length; i++ ) {
createSourceFile( signatures[ i ] );
}
}
/**
* Generates README documentation for a loop interface.
*
* @private
* @param {string} signature - loop signature
* @returns {string} documentation
*/
function createDoc( signature ) {
var match;
var inc;
var doc;
var ch1;
var ct1;
var tmp;
var nb1;
var t1;
doc = replace( TMPL_README, '{{SIGNATURE}}', signature );
// Ensure appropriate header files are included in documentation examples:
inc = [];
tmp = signature.substring( 3 ); // explicit callback signature; e.g., _as_c, _as_z
if ( tmp === '' ) {
tmp = signature.substring( 0, 1 ); // implicit callback signature; e.g., c, z, d, f, etc
}
if ( /c/.test( tmp ) ) {
inc.push( '#include "stdlib/complex/float32.h"' );
}
if ( /z/.test( tmp ) ) {
inc.push( '#include "stdlib/complex/float64.h"' );
}
if ( inc.length ) {
doc = replace( doc, '{{EXAMPLE_INCLUDES}}', '\n'+inc.join( '\n' ) );
} else {
doc = replace( doc, '{{EXAMPLE_INCLUDES}}', '' );
}
// Resolve the array data types:
match = signature.match( RE_SIGNATURE );
ch1 = match[ 1 ];
t1 = char2dtype( ch1 );
// Define array data types:
doc = replace( doc, '{{OUTPUT_NDARRAY_DTYPE_UPPER}}', uppercase( t1 ) );
// Define the number of bytes per element for the respective arrays:
nb1 = bytesPerElement( t1 );
doc = replace( doc, '{{OUTPUT_NDARRAY_BYTES_PER_ELEMENT}}', nb1.toString() );
// Define the array shapes:
doc = defineShapes( doc );
// Define underlying byte arrays:
doc = defineByteArrays( doc, nb1 );
// Define array strides:
doc = defineStrides( doc, nb1 );
// Resolve the callback parameter data types:
match = signature.match( RE_CALLBACK );
if ( match ) {
ch1 = match[ 1 ];
ct1 = dtype2c( char2dtype( ch1 ) );
} else {
ct1 = dtype2c( t1 );
}
doc = replace( doc, '{{CALLBACK_RETURN_DTYPE}}', ct1 );
doc = replace( doc, '{{CALLBACK_BODY}}', callbackBody( ch1 ) );
return doc;
}
/**
* Returns a character code list item.
*
* @private
* @param {string} dtype - data type
* @returns {string} list item
*/
function charCodeListItem( dtype ) {
return [
'- **',
dtypeChar( dtype ),
'**: `',
dtype,
'` (',
dtypeDesc( dtype ),
').'
].join( '' );
}
/**
* Returns a list of character code descriptions.
*
* @private
* @param {StringArray} dtypes - list of data types
* @returns {StringArray} list of character code descriptions
*/
function charCodeList( dtypes ) {
var out;
var i;
out = [];
for ( i = 0; i < dtypes.length; i++ ) {
out.push( charCodeListItem( dtypes[ i ] ) );
}
return out;
}
/**
* Update the package README.
*
* @private
* @param {StringArray} signatures - list of (sorted) loop signatures
* @throws {Error} unexpected error
*/
function updateREADME( signatures ) {
var parts;
var file;
var docs;
var out;
var err;
var i;
file = readFile( README, FOPTS );
if ( file instanceof Error ) {
throw file;
}
docs = [];
for ( i = 0; i < signatures.length; i++ ) {
docs.push( createDoc( signatures[ i ] ) );
}
out = [];
parts = file.split( '\n<!-- charcodes -->' );
out.push( parts[ 0 ] );
out.push( '<!-- charcodes -->' );
out.push( '' );
out.push( charCodeList( DTYPES ).join( '\n' ) );
out.push( '' );
parts = parts[ 1 ].split( '<!-- ./charcodes -->\n' );
out.push( '<!-- ./charcodes -->' );
parts = parts[ 1 ].split( '\n<!-- loops -->' );
out.push( parts[ 0 ] );
out.push( '<!-- loops -->' );
out.push( '' );
out.push( docs.join( '\n' ) );
parts = parts[ 1 ].split( '<!-- ./loops -->\n' );
out.push( '<!-- ./loops -->' );
out.push( parts[ 1 ] );
err = writeFile( README, out.join( '\n' ), FOPTS );
if ( err ) {
throw err;
}
}
/**
* Updates the main header file.
*
* @private
* @param {StringArray} signatures - list of (sorted) loop signatures
* @throws {Error} unexpected error
*/
function updateMainHeader( signatures ) {
var file;
var list;
var err;
var sig;
var ch;
var i;
file = readFile( INCLUDE_MAIN, FOPTS );
if ( file instanceof Error ) {
throw file;
}
list = [];
ch = signatures[ 0 ].charAt( 0 );
for ( i = 0; i < signatures.length; i++ ) {
sig = signatures[ i ];
if ( sig.charAt( 0 ) !== ch ) {
ch = sig.charAt( 0 );
list.push( '' );
}
list.push( '#include "nullary/'+sig+'.h"' );
}
file = [
substringBefore( file, '\n// BEGIN LOOPS' ),
'// BEGIN LOOPS',
list.join( '\n' ),
'// END LOOPS',
substringAfter( file, '// END LOOPS\n' )
].join( '\n' );
err = writeFile( INCLUDE_MAIN, file, FOPTS );
if ( err ) {
throw err;
}
}
/**
* Updates the package manifest.
*
* @private
* @param {StringArray} signatures - list of (sorted) loop signatures
* @throws {Error} unexpected error
*/
function updateManifest( signatures ) {
var file;
var list;
var tmp;
var err;
var l;
var i;
var j;
file = readJSON( MANIFEST, FOPTS );
if ( file instanceof Error ) {
throw file;
}
list = [];
for ( i = 0; i < signatures.length; i++ ) {
list.push( './src/'+signatures[ i ]+'.c' );
}
for ( j = 0; j < file.confs.length; j++ ) {
l = list.slice();
// Copy over non-signature source files...
tmp = file.confs[ j ].src;
for ( i = 0; i < tmp.length; i++ ) {
if ( RE_MANIFEST_LOOP_FILE.test( tmp[ i ] ) === false ) {
l.push( tmp[ i ] );
}
}
// Replace the list of source files:
file.confs[ j ].src = l;
}
err = writeFile( MANIFEST, JSON.stringify( file, null, 2 )+'\n', FOPTS );
if ( err ) {
throw err;
}
}
// MAIN //
/**
* Main execution sequence.
*
* @private
*/
function main() {
var sigs;
debug( 'Data types: %s', DTYPES.join( ', ' ) );
// Generate the list of loop signatures:
sigs = signatures( DTYPES );
debug( 'Signatures: %s', '\n'+sigs.join( '\n' ) );
// Remove loop files from output directories:
debug( 'Clearing include directory: %s', INCLUDE_DIR );
removeLoopFiles( INCLUDE_DIR );
debug( 'Clearing source directory: %s', SRC_DIR );
removeLoopFiles( SRC_DIR );
// Create header files for the list of loop signatures:
debug( 'Creating header files...' );
createHeaderFiles( sigs );
// Create source files for the list of loop signatures:
debug( 'Creating source files...' );
createSourceFiles( sigs );
// Update the main header file to include the loop header files:
debug( 'Updating main header file: %s', INCLUDE_MAIN );
updateMainHeader( sigs );
// Update the package manifest to include the loop source files:
debug( 'Updating manifest file: %s', MANIFEST );
updateManifest( sigs );
// Update the package README to include the loop interfaces:
debug( 'Updating README file: %s', README );
updateREADME( sigs );
debug( 'Finished.' );
}
main();