forked from gpujs/gpu.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionNode_webgl.js
More file actions
784 lines (687 loc) · 21.7 KB
/
functionNode_webgl.js
File metadata and controls
784 lines (687 loc) · 21.7 KB
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
// Closure capture for the ast function, prevent collision with existing AST functions
var functionNode_webgl = (function() {
var gpu, opt, jsFunctionString;
function isIdentifierConstant(paramName) {
if (!opt.constants) return false;
return opt.constants.indexOf(paramName) != -1;
}
function isIdentifierKernelParam(paramName, ast, funcParam) {
return funcParam.paramNames.indexOf(paramName) != -1;
}
function ensureIndentifierType(paramName, expectedType, ast, funcParam) {
var start = ast.loc.start;
if (!isIdentifierKernelParam(paramName, funcParam) && expectedType != 'float') {
throw "Error unxpected identifier " + paramName + " on line " + start.line;
} else {
var actualType = funcParam.paramType[funcParam.paramNames.indexOf(paramName)];
if (actualType != expectedType) {
throw "Error unxpected identifier " + paramName + " on line " + start.line;
}
}
}
///
/// Function: functionNode_webgl
///
/// [INTERNAL] Takes in a function node, and does all the AST voodoo required to generate its respective webGL code.
///
/// Parameter:
/// inNode - {functionNode} The function node object
///
/// Returns:
/// the converted webGL function string
///
function functionNode_webgl( inNode, _opt ) {
gpu = inNode.gpu;
opt = _opt || {};
if (opt.debug) {
console.log(inNode);
}
jsFunctionString = inNode.jsFunctionString;
if (opt.prototypeOnly) {
return ast_FunctionPrototype( inNode.getJS_AST(), [], inNode ).join("").trim();
} else {
inNode.webglFunctionString_array = ast_generic( inNode.getJS_AST(), [], inNode );
}
inNode.webglFunctionString = webgl_regex_optimize(
inNode.webglFunctionString_array.join("").trim()
);
return inNode.webglFunctionString;
}
var DECODE32_ENCODE32 = /decode32\(\s+encode32\(/g;
var ENCODE32_DECODE32 = /encode32\(\s+decode32\(/g;
///
/// Function: webgl_regex_optimize
///
/// [INTERNAL] Takes the near final webgl function string, and do regex search and replacments.
/// For voodoo optimize out the following
///
/// - decode32(encode32(
/// - encode32(decode32(
///
function webgl_regex_optimize( inStr ) {
return inStr
.replace(DECODE32_ENCODE32, "((")
.replace(ENCODE32_DECODE32, "((")
;
}
/// the AST error, with its location. To throw
///
/// @TODO: add location support fpr the AST error
///
/// @param error the error message output
/// @param ast the AST object where the error is
/// @param funcParam FunctionNode, that tracks compilation state
function ast_errorOutput(error, ast, funcParam) {
console.error(error, ast, funcParam);
return error;
}
/// Prases the abstract syntax tree, genericially to its respective function
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the prased openclgl string array
function ast_generic(ast, retArr, funcParam) {
if(ast === null) {
throw ast_errorOutput("NULL ast", ast, funcParam);
} else {
if (Array.isArray(ast)) {
for (var i=0; i<ast.length; i++) {
ast_generic(ast[i], retArr, funcParam);
}
return retArr;
}
switch(ast.type) {
case "FunctionDeclaration":
return ast_FunctionDeclaration(ast, retArr, funcParam);
case "FunctionExpression":
return ast_FunctionExpression(ast, retArr, funcParam);
case "ReturnStatement":
return ast_ReturnStatement(ast, retArr, funcParam);
case "Literal":
return ast_Literal(ast, retArr, funcParam);
case "BinaryExpression":
return ast_BinaryExpression(ast, retArr, funcParam);
case "Identifier":
return ast_IdentifierExpression(ast, retArr, funcParam);
case "AssignmentExpression":
return ast_AssignmentExpression(ast, retArr, funcParam);
case "ExpressionStatement":
return ast_ExpressionStatement(ast, retArr, funcParam);
case "EmptyStatement":
return ast_EmptyStatement(ast, retArr, funcParam);
case "BlockStatement":
return ast_BlockStatement(ast, retArr, funcParam);
case "IfStatement":
return ast_IfStatement(ast, retArr, funcParam);
case "BreakStatement":
return ast_BreakStatement(ast, retArr, funcParam);
case "ContinueStatement":
return ast_ContinueStatement(ast, retArr, funcParam);
case "ForStatement":
return ast_ForStatement(ast, retArr, funcParam);
case "WhileStatement":
return ast_WhileStatement(ast, retArr, funcParam);
case "VariableDeclaration":
return ast_VariableDeclaration(ast, retArr, funcParam);
case "VariableDeclarator":
return ast_VariableDeclarator(ast, retArr, funcParam);
case "ThisExpression":
return ast_ThisExpression(ast, retArr, funcParam);
case "SequenceExpression":
return ast_SequenceExpression(ast, retArr, funcParam);
case "UnaryExpression":
return ast_UnaryExpression(ast, retArr, funcParam);
case "UpdateExpression":
return ast_UpdateExpression(ast, retArr, funcParam);
case "LogicalExpression":
return ast_LogicalExpression(ast, retArr, funcParam);
case "MemberExpression":
return ast_MemberExpression(ast, retArr, funcParam);
case "CallExpression":
return ast_CallExpression(ast, retArr, funcParam);
}
throw ast_errorOutput("Unknown ast type : "+ast.type, ast, funcParam);
}
}
/// Prases the abstract syntax tree, to its named function declartion
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_FunctionDeclaration(ast, retArr, funcParam) {
// TODO: make this less hacky?
var lines = jsFunctionString.split(/\r?\n/);
var start = ast.loc.start;
var end = ast.loc.end;
var funcArr = [];
funcArr.push(lines[start.line-1].slice(start.column));
for (var i=start.line; i<end.line-1; i++) {
funcArr.push(lines[i]);
}
funcArr.push(lines[end.line-1].slice(0,end.column));
var funcStr = funcArr.join('\n');
gpu.addFunction(funcStr);
return retArr;
}
/// Prases the abstract syntax tree, to its named function prototype
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_FunctionPrototype(ast, retArr, funcParam) {
// Setup function return type and name
if(funcParam.isRootKernel) {
return retArr;
}
retArr.push(funcParam.returnType);
retArr.push(" ");
retArr.push(funcParam.functionName);
retArr.push("(");
// Arguments handling
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
if( i > 0 ) {
retArr.push(", ");
}
retArr.push( funcParam.paramType[i] );
retArr.push(" ");
retArr.push("user_");
retArr.push( funcParam.paramNames[i] );
}
retArr.push(");\n");
return retArr;
}
/// Prases the abstract syntax tree, to its named function
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_FunctionExpression(ast, retArr, funcParam) {
// Setup function return type and name
if(funcParam.isRootKernel) {
retArr.push("void");
} else {
retArr.push(funcParam.returnType);
}
retArr.push(" ");
retArr.push(funcParam.functionName);
retArr.push("(");
if(!funcParam.isRootKernel) {
// Arguments handling
for( var i = 0; i < funcParam.paramNames.length; ++i ) {
if( i > 0 ) {
retArr.push(", ");
}
retArr.push( funcParam.paramType[i] );
retArr.push(" ");
retArr.push("user_");
retArr.push( funcParam.paramNames[i] );
}
}
// Function opening
retArr.push(") {\n");
// Body statement iteration
for(var i=0; i<ast.body.length; ++i) {
ast_generic(ast.body[i], retArr, funcParam);
retArr.push("\n");
}
// Function closing
retArr.push("}\n");
return retArr;
}
/// Prases the abstract syntax tree, to return function
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_ReturnStatement(ast, retArr, funcParam) {
if(funcParam.isRootKernel) {
retArr.push("kernelResult = ");
ast_generic(ast.argument, retArr, funcParam);
retArr.push(";");
retArr.push("return;");
} else {
retArr.push("return ");
ast_generic(ast.argument, retArr, funcParam);
retArr.push(";");
}
//throw ast_errorOutput(
// "Non main function return, is not supported : "+funcParam.currentFunctionNamespace,
// ast, funcParam
//);
return retArr;
}
/// Prases the abstract syntax tree, literal value
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_Literal(ast, retArr, funcParam) {
// Reject non numeric literals
if( isNaN(ast.value) ) {
throw ast_errorOutput(
"Non-numeric literal not supported : "+ast.value,
ast, funcParam
);
}
// Push the literal value as a float/int
retArr.push( ast.value );
// If it was an int, node made a float
if( Number.isInteger(ast.value) ) {
retArr.push(".0");
}
return retArr;
}
/// Prases the abstract syntax tree, binary expression
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_BinaryExpression(ast, retArr, funcParam) {
retArr.push("(");
if (ast.operator == "%") {
retArr.push("mod(");
ast_generic(ast.left, retArr, funcParam);
retArr.push(",");
ast_generic(ast.right, retArr, funcParam);
retArr.push(")");
} else if (ast.operator == "===") {
ast_generic(ast.left, retArr, funcParam);
retArr.push("==");
ast_generic(ast.right, retArr, funcParam);
} else if (ast.operator == "!==") {
ast_generic(ast.left, retArr, funcParam);
retArr.push("!=");
ast_generic(ast.right, retArr, funcParam);
} else {
ast_generic(ast.left, retArr, funcParam);
retArr.push(ast.operator);
ast_generic(ast.right, retArr, funcParam);
}
retArr.push(")");
return retArr;
}
/// Prases the abstract syntax tree, identifier expression
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_IdentifierExpression(idtNode, retArr, funcParam) {
if (idtNode.type != "Identifier") {
throw ast_errorOutput(
"IdentifierExpression - not an Identifier",
ast, funcParam
);
}
if (idtNode.name == "gpu_threadX") {
retArr.push('threadId.x');
} else if (idtNode.name == "gpu_threadY") {
retArr.push('threadId.y');
} else if (idtNode.name == "gpu_threadZ") {
retArr.push('threadId.z');
} else if (idtNode.name == "gpu_dimensionsX") {
retArr.push('uOutputDim.x');
} else if (idtNode.name == "gpu_dimensionsY") {
retArr.push('uOutputDim.y');
} else if (idtNode.name == "gpu_dimensionsZ") {
retArr.push('uOutputDim.z');
} else {
retArr.push("user_"+idtNode.name);
}
return retArr;
}
/// Prases the abstract syntax tree, genericially to its respective function
///
/// @param ast the AST object to parse
///
/// @returns the prased openclgl string
function ast_ForStatement(forNode, retArr, funcParam) {
if (forNode.type != "ForStatement") {
throw ast_errorOutput(
"Invalid for statment",
ast, funcParam
);
}
if (forNode.test && forNode.test.type == "BinaryExpression") {
if (forNode.test.right.type == "Identifier"
&& forNode.test.operator == "<"
&& isIdentifierConstant(forNode.test.right.name) == false) {
if (opt.loopMaxIterations === undefined) {
console.warn("Warning: loopMaxIterations is not set! Using default of 100 which may result in unintended behavior.");
console.warn("Set loopMaxIterations or use a for loop of fixed length to silence this message.");
}
retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.test.left, retArr, funcParam);
retArr.push(forNode.test.operator);
retArr.push("LOOP_MAX");
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(")");
retArr.push("{\n");
retArr.push("if (");
ast_generic(forNode.test.left, retArr, funcParam);
retArr.push(forNode.test.operator);
ast_generic(forNode.test.right, retArr, funcParam);
retArr.push(") {\n");
if (forNode.body.type == "BlockStatement") {
for (var i = 0; i < forNode.body.body.length; i++) {
ast_generic(forNode.body.body[i], retArr, funcParam);
}
} else {
ast_generic(forNode.body, retArr, funcParam);
}
retArr.push("} else {\n");
retArr.push("break;\n");
retArr.push("}\n");
retArr.push("}\n");
return retArr;
} else {
retArr.push("for (float ");
ast_generic(forNode.init, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.test, retArr, funcParam);
retArr.push(";");
ast_generic(forNode.update, retArr, funcParam);
retArr.push(")");
ast_generic(forNode.body, retArr, funcParam);
return retArr;
}
}
throw ast_errorOutput(
"Invalid for statment",
ast, funcParam
);
}
/// Prases the abstract syntax tree, genericially to its respective function
///
/// @param ast the AST object to parse
///
/// @returns the prased openclgl string
function ast_WhileStatement(whileNode, retArr, funcParam) {
if (whileNode.type != "WhileStatement") {
throw ast_errorOutput(
"Invalid while statment",
ast, funcParam
);
}
retArr.push("for (float i=0.0; i<LOOP_MAX; i++) {");
retArr.push("if (");
ast_generic(whileNode.test, retArr, funcParam);
retArr.push(") {\n");
ast_generic(whileNode.body, retArr, funcParam);
retArr.push("} else {\n");
retArr.push("break;\n");
retArr.push("}\n");
retArr.push("}\n");
return retArr;
}
function ast_AssignmentExpression(assNode, retArr, funcParam) {
if(assNode.operator == "%=") {
ast_generic(assNode.left, retArr, funcParam);
retArr.push("=");
retArr.push("mod(");
ast_generic(assNode.left, retArr, funcParam);
retArr.push(",");
ast_generic(assNode.right, retArr, funcParam);
retArr.push(")");
} else {
ast_generic(assNode.left, retArr, funcParam);
retArr.push(assNode.operator);
ast_generic(assNode.right, retArr, funcParam);
return retArr;
}
}
function ast_EmptyStatement(eNode, retArr, funcParam) {
retArr.push(";\n");
return retArr;
}
function ast_BlockStatement(bNode, retArr, funcParam) {
retArr.push("{\n");
for (var i = 0; i < bNode.body.length; i++) {
ast_generic(bNode.body[i], retArr, funcParam);
}
retArr.push("}\n");
return retArr;
}
function ast_ExpressionStatement(esNode, retArr, funcParam) {
ast_generic(esNode.expression, retArr, funcParam);
retArr.push(";\n");
return retArr;
}
function ast_VariableDeclaration(vardecNode, retArr, funcParam) {
retArr.push("float ");
for (var i = 0; i < vardecNode.declarations.length; i++) {
if (i > 0) {
retArr.push(",");
}
ast_generic(vardecNode.declarations[i], retArr, funcParam);
}
retArr.push(";");
return retArr;
}
function ast_VariableDeclarator(ivardecNode, retArr, funcParam) {
ast_generic(ivardecNode.id, retArr, funcParam);
if (ivardecNode.init !== null) {
retArr.push("=");
ast_generic(ivardecNode.init, retArr, funcParam);
}
return retArr;
}
function ast_IfStatement(ifNode, retArr, funcParam) {
retArr.push("if(");
ast_generic(ifNode.test, retArr, funcParam);
retArr.push(")");
if (ifNode.consequent.type == "BlockStatement") {
ast_generic(ifNode.consequent, retArr, funcParam);
} else {
retArr.push(" {\n");
ast_generic(ifNode.consequent, retArr, funcParam);
retArr.push("\n}\n");
}
if (ifNode.alternate) {
retArr.push("else ");
if (ifNode.alternate.type == "BlockStatement") {
ast_generic(ifNode.alternate, retArr, funcParam);
} else {
retArr.push(" {\n");
ast_generic(ifNode.alternate, retArr, funcParam);
retArr.push("\n}\n");
}
}
return retArr;
}
function ast_BreakStatement(brNode, retArr, funcParam) {
retArr.push("break;\n");
return retArr;
}
function ast_ContinueStatemnt(crNode, retArr, funcParam) {
retArr.push("continue;\n");
return retArr;
}
function ast_LogicalExpression(logNode, retArr, funcParam) {
retArr.push("(");
ast_generic(logNode.left, retArr, funcParam);
retArr.push(logNode.operator);
ast_generic(logNode.right, retArr, funcParam);
retArr.push(")");
return retArr;
}
function ast_UpdateExpression(uNode, retArr, funcParam) {
if(uNode.prefix) {
retArr.push(uNode.operator);
ast_generic(uNode.argument, retArr, funcParam);
} else {
ast_generic(uNode.argument, retArr, funcParam);
retArr.push(uNode.operator);
}
return retArr;
}
function ast_UnaryExpression(uNode, retArr, funcParam) {
if(uNode.prefix) {
retArr.push(uNode.operator);
ast_generic(uNode.argument, retArr, funcParam);
} else {
ast_generic(uNode.argument, retArr, funcParam);
retArr.push(uNode.operator);
}
return retArr;
}
function ast_ThisExpression(tNode, retArr, funcParam) {
retArr.push("this");
return retArr;
}
// The prefixes to use
var jsMathPrefix = "Math.";
var localPrefix = "this.";
var constantsPrefix = "this.constants.";
function ast_MemberExpression(mNode, retArr, funcParam) {
if(mNode.computed) {
if (mNode.object.type == "Identifier") {
retArr.push("get(");
ast_generic(mNode.object, retArr, funcParam);
retArr.push(", vec2(");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Size[0],");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Size[1]), vec3(");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Dim[0],");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Dim[1],");
ast_generic(mNode.object, retArr, funcParam);
retArr.push("Dim[2]");
retArr.push("), ");
} else {
ast_generic(mNode.object, retArr, funcParam);
var last = retArr.pop();
retArr.push(",");
}
ast_generic(mNode.property, retArr, funcParam);
retArr.push(")");
} else {
// Unroll the member expression
var unrolled = ast_MemberExpression_unroll(mNode);
var unrolled_lc = unrolled.toLowerCase();
// Its a constant, remove this.constants.
if( unrolled.indexOf(constantsPrefix) === 0 ) {
unrolled = 'constants_'+unrolled.slice( constantsPrefix.length );
}
if (unrolled_lc == "this.thread.x") {
retArr.push('threadId.x');
} else if (unrolled_lc == "this.thread.y") {
retArr.push('threadId.y');
} else if (unrolled_lc == "this.thread.z") {
retArr.push('threadId.z');
} else if (unrolled_lc == "this.dimensions.x") {
retArr.push('uOutputDim.x');
} else if (unrolled_lc == "this.dimensions.y") {
retArr.push('uOutputDim.y');
} else if (unrolled_lc == "this.dimensions.z") {
retArr.push('uOutputDim.z');
} else {
retArr.push(unrolled);
}
}
return retArr;
}
function ast_SequenceExpression(sNode, retArr, funcParam) {
for (var i = 0; i < sNode.expressions.length; i++) {
if (i > 0) {
retArr.push(",");
}
ast_generic(sNode.expressions, retArr, funcParam);
}
return retArr;
}
/// Utility function for ast_CallExpression.
///
/// Prases the abstract syntax tree, binary expression.
///
/// @param ast the AST object to parse
///
/// @returns {String} the function namespace call, unrolled
function ast_MemberExpression_unroll(ast, funcParam) {
if( ast.type == "Identifier" ) {
return ast.name;
} else if( ast.type == "ThisExpression" ) {
return "this";
}
if( ast.type == "MemberExpression" ) {
if( ast.object && ast.property ) {
return (
ast_MemberExpression_unroll( ast.object, funcParam ) +
"." +
ast_MemberExpression_unroll( ast.property, funcParam )
);
}
}
// Failure, unknown expression
throw ast_errorOutput(
"Unknown CallExpression_unroll",
ast, funcParam
);
}
/// Prases the abstract syntax tree, binary expression
///
/// @param ast the AST object to parse
/// @param retArr return array string
/// @param funcParam FunctionNode, that tracks compilation state
///
/// @returns the appened retArr
function ast_CallExpression(ast, retArr, funcParam) {
if( ast.callee ) {
// Get the full function call, unrolled
var funcName = ast_MemberExpression_unroll(ast.callee);
// Its a math operator, remove the prefix
if( funcName.indexOf(jsMathPrefix) === 0 ) {
funcName = funcName.slice( jsMathPrefix.length );
}
// Its a local function, remove this
if( funcName.indexOf(localPrefix) === 0 ) {
funcName = funcName.slice( localPrefix.length );
}
// Register the function into the called registry
if( funcParam.calledFunctions.indexOf(funcName) < 0 ) {
funcParam.calledFunctions.push(funcName);
}
// Call the function
retArr.push( funcName );
// Open arguments space
retArr.push( "(" );
// Add the vars
for(var i=0; i<ast.arguments.length; ++i) {
if(i > 0) {
retArr.push(", ");
}
ast_generic(ast.arguments[i],retArr,funcParam);
}
// Close arguments space
retArr.push( ")" );
return retArr;
}
// Failure, unknown expression
throw ast_errorOutput(
"Unknown CallExpression",
ast, funcParam
);
return retArr;
}
return functionNode_webgl;
})();