-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.js
1269 lines (1067 loc) · 43.1 KB
/
ast.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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Ast = {};
(function(exports){
// The symbol table
// Nodes that define new scopes are responsible for pushing a new scope
// onto the stack when needed and popping it off when they are done
function Scope(parnt) {
this.parnt = parnt;
this._ = {};
this.add = function(sym) {
this._[sym.iden] = sym;
};
this.find = function(sym) {
return this._[sym];
};
this.search = function(sym) {
var found = this._[sym];
if(found) return found;
if(this.parnt) return this.parnt.search(sym);
return false;
};
}
function Symbol(identifier, type) {
this.iden = identifier;
this.type = type;
}
function VaryingSymbol(identifier, type) {
this.iden = identifier;
this.type = type;
this.is_varying = true;
}
function FunctionSymbol(identifier, type_signatures) {
this.is_function = true;
this.iden = identifier;
this.type_signatures = type_signatures;
}
function CompoundSymbol(identifier) {
this.is_compound = true;
this.iden = identifier;
this.fields = {};
}
function AttributeSymbol(identifier, type) {
this.is_attribute = true;
this.iden = identifier;
this.type = type;
}
function IndexSymbol(identifier) {
// It is an error to reference this symbol in the field // TODO
// Its mere presence is enough to fundamentally change how we run the shader
this.is_index = true;
this.iden = identifier;
}
// The current scope will always be available here. Nodes that define new
// scopes will set this as necessary
var sym_tab = new Scope(undefined);
// TODO document builtins
// Add built in variables and functions to global scope
var builtins = [
new FunctionSymbol('vec4', [
['vec4', ['vec3', 'float']],
['vec4', ['float', 'float', 'float', 'float']],
['vec4', ['float']],
]),
new FunctionSymbol('vec3', [
['vec3', ['vec2', 'float']],
['vec3', ['float', 'float', 'float']],
['vec3', ['float']],
]),
new FunctionSymbol('vec2', [
['vec2', ['float', 'float']],
['vec2', ['float']],
]),
new FunctionSymbol('texture2D', [['vec4', ['sampler2D', 'vec2']]]),
new FunctionSymbol('normalize', [
['vec4', ['vec4']],
['vec3', ['vec3']],
]),
new FunctionSymbol('dot', [
['float', ['vec4', 'vec4']],
['float', ['vec3', 'vec3']],
]),
new FunctionSymbol('pow', [['float', ['float', 'float']]]),
new FunctionSymbol('max', [['float', ['float', 'float']]]),
new FunctionSymbol('reflect', [['vec3', ['vec3', 'vec3']]]),
new FunctionSymbol('length', [
['float', ['vec2']],
['float', ['vec3']],
['float', ['vec4']],
]),
new Symbol('gl_Position', 'vec4'),
new Symbol('gl_FragColor', 'vec4'),
];
for(var i in builtins) {
sym_tab.add(builtins[i]);
}
// Ast node types
// Most semantic checking and code gen code is expressed as ast members.
// Tree traversal is done through recursive ast calls.
// Prototype for all ast nodes
function Node() {
// The node type is used internally to ensure correct ast construction
this.node_type = "node";
this.node_parent = null;
// All nodes have a type field set by the check method
this.type = undefined;
// Check performs semantic analysis first on all children of this
// node and then on this node. Will set the type field before returning
this.check = undefined;
// Generates code for this node, calls code gen for child nodes
this.code_gen = undefined;
}
var node_proto = new Node();
// Expression node, used for error checking ast types
function Expr() {
this.node_type = "expr";
this.node_parent = "node";
}
Expr.prototype = node_proto;
var expr_proto = new Expr();
function IdenExpr(iden) {
this.node_type = "iden_expr";
this.node_parent = "expr";
this.type = undefined;
this.iden = iden.value;
this.check = function() {
var symbol = sym_tab.search(this.iden);
if(!symbol) {
throw "Symbol " + this.iden + " not declared";
}
this.type = symbol.type;
this.symbol = symbol;
}
this.code_gen = function() {
emit(this.iden);
}
}
IdenExpr.prototype = expr_proto;
exports.IdenExpr = IdenExpr;
// Statement node, used for error checking ast types
function Stmt() {
this.node_type = "stmt";
this.node_parent = "node";
// Statements don't have types, but they must type check correctly
this.type = undefined;
}
Stmt.prototype = node_proto;
var stmt_proto = new Stmt();
// Constant expressions
function Int(int_tkn) {
this.node_type = "int";
this.node_parent = "expr";
// Yes, I know it's an int, but the check function will fill it in
this.type = undefined;
this.value = int_tkn.value;
this.check = function() {
this.type = "int";
};
this.code_gen = function() {
emit("" + this.value);
};
}
Int.prototype = node_proto;
exports.Int = Int;
function Float(float_tkn) {
this.node_type = "float";
this.node_parent = "expr";
// Yes, I know it's an int, but the check function will fill it in
this.type = undefined;
this.value = float_tkn.value;
this.check = function() {
this.type = "float";
};
this.code_gen = function() {
emit("" + this.value);
if(this.value % 1.0 === 0) {
emit(".0");
}
};
}
Float.prototype = node_proto;
exports.Float = Float;
function Infix(name, oper) {
var f = function(expr1, expr2) {
this.node_type = name;
this.node_parent = "expr";
this.type = undefined;
this.expr1 = expr1;
this.expr2 = expr2;
this.check = function() {
assert_node_type(this.expr1, "expr");
assert_node_type(this.expr2, "expr");
this.expr1.check();
this.expr2.check();
// TODO figure out this mess
/*
assert(this.expr1.type === this.expr2.type,
"Operands of " + oper + " expression not of the same type");
assert(this.expr1.type === "int"
|| this.expr1.type === "float"
, oper + " operation applied to non-valid type"
);
//TODO other valid types?
*/
// Assuming that operands must be of same type (int, float etc.)
// then type of an arithmetic infix is just that same type
this.type = this.expr1.type;
};
this.code_gen = function() {
expr1.code_gen();
emit(" " + oper + " ");
expr2.code_gen();
};
};
f.prototype = expr_proto;
exports[name] = f;
}
Infix("Plus", "+");
Infix("Minus", "-");
Infix("Divide", "/");
//Infix("Multiply", "*");
Infix("Mod", "%");
Infix("Equ", "==");
Infix("Ltn", "<");
Infix("Gtn", ">");
Infix("Lte", "<=");
Infix("Gte", ">=");
function Multiply(left, right) {
this.node_type = "Multiply";
this.node_parent = "expr";
this.left = left;
this.right = right;
this.check = function() {
assert_node_type(this.left, "expr");
assert_node_type(this.right, "expr");
this.left.check();
this.right.check();
assert(this.left.type && this.right.type);
// The type inferrence rules here depend upon the types on the left
// and right
if(this.left.type === this.right.type) {
this.type = this.left.type;
} else if((this.left.type === "mat4") &&
(this.right.type === "vec4")) {
this.type = "vec4";
} else if(this.left.type == "vec4" && this.right.type == "float") {
this.type = "vec4";
} else if(this.left.type == "vec3" && this.right.type == "float") {
this.type = "vec3";
} else if(this.left.type == "float" && this.right.type == "vec3") {
this.type = "vec3";
} else if(this.left.type == "float" && this.right.type == "vec4") {
this.type = "vec4";
} else {
throw "Unhandled case: " + this.left.type +
" * " + this.right.type;
}
}
this.code_gen = function() {
this.left.code_gen();
emit(" * ");
this.right.code_gen();
}
}
this.prototype = expr_proto;
exports.Multiply = Multiply;
function FunCall(funct, actuals) {
this.node_type = "fun_call";
this.node_parent = "expr";
this.type = undefined;
this.funct_id = funct.value;
this.actuals = actuals;
this.check = function() {
var actuals_types = [];
for(var i in this.actuals) {
this.actuals[i].check();
actuals_types.push(this.actuals[i].type);
}
var funct_sym = sym_tab.search(this.funct_id);
if(!funct_sym) {
throw "Function " + this.funct_id + " is not defined";
} else if(!funct_sym.is_function) {
throw "Symbol " + this.funct_id + " called as if it were a function";
} else {
for(var i in funct_sym.type_signatures) {
var signature = funct_sym.type_signatures[i];
var return_type = signature[0];
var parameter_types = signature[1];
var failed = false;
for(var j in parameter_types) {
var param_type = parameter_types[j];
if(actuals_types[j] !== param_type) {
failed = true;
break;
}
}
if(!failed) {
this.type = return_type;
return;
}
}
throw "Actual parameters " + actuals_types + " to function " + funct_sym.iden
+ " do not match formal parameters";
}
}
this.code_gen = function() {
emit(this.funct_id);
emit('(');
var len = this.actuals.length;
if(len > 0) {
this.actuals[0].code_gen();
for(var i = 1; i < len; i++) {
emit(', ');
this.actuals[i].code_gen();
}
}
emit(')');
}
}
FunCall.prototype = expr_proto;
exports.FunCall = FunCall;
function FieldAccess(base, field) {
this.node_type = "field_access";
this.node_parent = "expr";
this.type = null;
this.baseExpr = base;
this.fieldExpr = field;
this.check = function() {
this.baseExpr.check();
// Two possibilities:
// 1. Access field of uniform variable e.g. mesh.pos
// 2. Swizzling e.g. myVec.xyz
var base_sym = this.baseExpr.symbol;
if(base_sym && base_sym.is_compound) {
// Compound types are not vectors, they have subfields and can't be swizzled
/*
var msg = "Access of field on uniform variable requires identifier";
assert(this.baseExpr.node_type === "iden_expr", msg);
assert(this.fieldExpr.node_type === "iden_expr", msg);
*/
var fieldIden = this.fieldExpr.iden;
var index_symbol = null;
// This hack, TODO do this in general for compound symbols
if(base_sym.is_this) {
function infer_symbol_from_parameter(formal, actual) {
var new_symbol;
if(Array.isArray(actual) ||
(actual.constructor.name === "Float32Array")) {
switch(actual.length) {
case 16:
new_symbol = new Symbol(formal, 'mat4');
break;
case 9:
new_symbol = new Symbol(formal, 'mat3');
break;
case 4:
// Could be a mat2 but uncommon, assume vec4
new_symbol = new Symbol(formal, 'vec4');
break;
case 3:
new_symbol = new Symbol(formal, 'vec3');
break;
case 2:
new_symbol = new Symbol(formal, 'vec2');
break;
// TODO boolean, integer types
default:
throw "Unrecognizable parameter type " + actual;
}
} else if(typeof(actual) === "object") {
// Special type (like image) or compound type
if(actual.TEXTURE) {
new_symbol = new Symbol(formal, 'sampler2D');
} else if(actual.ATTRIBUTE) {
new_symbol = new AttributeSymbol(formal, actual.type);
} else if(actual.INDEX) {
// TODO when referenced in the body will be an error
new_symbol = new IndexSymbol(formal);
if(index_symbol) {
throw "A shader program may only have one index parameter. Found second index parameter "
+ formal + " but index parameter " + index_symbol.inden + " already processed.";
} else {
index_symbol = new_symbol;
}
} else {
// Assume compound type
new_symbol = new CompoundSymbol(formal);
for(var field in actual) {
new_symbol.fields[field] =
infer_symbol_from_parameter(field, actual[field]);
}
}
} else if(typeof(actual) === "number") {
// TODO figure out how to distinguish ints and floats
new_symbol = new Symbol(formal, 'float');
}
// TODO booleans
if(new_symbol === undefined) {
throw "Unable to infer type of " + formal;
}
return new_symbol;
}
// On demand check the type of this field
var actualField = base_sym.actual_this[fieldIden];
base_sym.fields[fieldIden] = infer_symbol_from_parameter(fieldIden, actualField);
if(index_symbol) {
__shade_p__.index_symbol = index_symbol;
}
}
field_sym = base_sym.fields[fieldIden];
assert(field_sym, "Field " + fieldIden + " not defined on " + base_sym.iden);
this.type = field_sym.type;
this.symbol = field_sym;
} else {
// Not a compund, only valid is a swizzle on a vector type
var match = this.fieldExpr.iden.match(/^[wxyzrgba]{1,4}$/);
assert(match, "Invalid swizzle format");
var dimension = this.fieldExpr.iden.length;
if(dimension == 1) {
this.type = 'float';
} else {
this.type = 'vec' + dimension;
}
this.is_swizzle = true;
}
}
this.code_gen = function() {
this.baseExpr.code_gen();
if(this.is_swizzle) {
emit('.');
} else {
emit('_');
}
this.fieldExpr.code_gen();
}
}
FieldAccess.prototype = expr_proto;
exports.FieldAccess = FieldAccess;
function ParensExpr(internalExpr) {
this.node_type = "parens_expr";
this.node_parent = "expr";
this.internalExpr = internalExpr;
this.type = null;
this.check = function() {
this.internalExpr.check();
this.type = this.internalExpr.type;
}
this.code_gen = function() {
emit('(');
this.internalExpr.code_gen();
emit(')');
}
}
ParensExpr.prototype = expr_proto;
exports.ParensExpr = ParensExpr;
function EmptyStmt() {
this.node_type = "empty_stmt";
this.node_parent = "stmt";
this.type = undefined;
this.check = function() {
// no-op
}
this.code_gen = function() {
// no-op
}
}
EmptyStmt.prototype = stmt_proto;
exports.EmptyStmt = EmptyStmt;
function ExprStmt(expr) {
this.node_type = "expr_stmt";
this.node_parent = "stmt";
this.type = undefined;
this.expr = expr;
this.check = function() {
this.expr.check();
}
this.code_gen = function() {
this.expr.code_gen();
emit('; ');
}
}
ExprStmt.prototype = stmt_proto;
exports.ExprStmt = ExprStmt;
function VarDeclStmt(new_var) {
this.node_type = "vardecl_stmt";
this.node_parent = "stmt";
this.type = undefined;
this.new_var = new_var.value;
this.check = function() {
// Newly defined variables should not already have
// been declared in this scope
var sym = sym_tab.search(this.new_var);
if(sym) {
// Declared in some scope (this or above)
if(sym.is_varying) {
// We won't actually hide the varying
} else if(sym_tab.find(this.new_var)) {
// Already declared in this scope, a no-no
throw "Variable " + this.new_var + " has already been " +
"declared in this scope";
} else {
// We're hiding an outer scope variable, OK
sym_tab.add(new Symbol(this.new_var));
}
} else {
// Not yet declared anywhere, OK
sym_tab.add(new Symbol(this.new_var));
}
}
this.code_gen = function() {
var symbol = sym_tab.search(this.new_var);
// We added it in check, better be there
assert(symbol, "Symbol " + this.new_var + " not defined");
// Code check on first definition should have given it a type
assert(symbol.type, "Symbol " + this.new_var + " declared but not defined");
if(!symbol.is_varying) {
emit(symbol.type + " ");
emit(symbol.iden);
emit('; ');
}
}
}
VarDeclStmt.prototype = stmt_proto;
exports.VarDeclStmt= VarDeclStmt;
function VarDefStmt(iden, expr) {
this.node_type = "vardef_stmt";
this.node_parent = "stmt";
this.type = undefined;
this.iden = iden.value;
this.expr = expr;
this.check = function() {
this.expr.check();
assert(this.expr.type, "Expr type check failed");
// Identifier must already have been declared
var symbol = sym_tab.search(this.iden);
if(!symbol) {
throw "Variable " + this.iden + " has not been declared";
}
if(!symbol.type) {
// This variable has not been defined yet, first assignment sets its type
symbol.type = this.expr.type;
} else {
// All following redefinitions must conform to the initial type
assert(symbol.type === this.expr.type, "Attempt to set variable of type " +
symbol.type + " to value of type " + this.expr.type);
}
}
this.code_gen = function() {
emit(this.iden);
emit(" = ");
this.expr.code_gen();
}
}
VarDefStmt.prototype = stmt_proto;
exports.VarDefStmt = VarDefStmt;
function VarDeclDefStmt(iden, expr) {
this.node_type = "vardecldef_stmt";
this.node_parent = "stmt";
this.type = undefined;
// We treat this case as declaration followed by a definition
this.decl = new VarDeclStmt(iden);
this.def = new VarDefStmt(iden, expr);
this.check = function() {
this.decl.check();
this.def.check();
}
this.code_gen = function() {
this.decl.code_gen();
this.def.code_gen();
}
}
VarDeclDefStmt.prototype = stmt_proto;
exports.VarDeclDefStmt = VarDeclDefStmt;
function StmtBlk(stmt_array) {
this.node_type = "stmt_blk";
this.node_parent = "node";
this.type = undefined;
this.statements = stmt_array;
this.check = function() {
for(var i in this.statements) {
this.statements[i].check();
}
}
this.code_gen = function() {
emit("{ ");
for(var i in this.statements) {
this.statements[i].code_gen();
emit('; ');
}
emit(" }");
}
}
StmtBlk.prototype = node_proto;
exports.StmtBlk = StmtBlk;
// TODO IfStmt and other scope defining statments should define a new scope
function IfStmt(cond, then_clause, else_clause) {
this.node_type = "if_stmt";
this.node_parent = "stmt";
this.type = undefined;
this.cond = cond;
this.then_clause = then_clause;
this.else_clause = else_clause;
this.check = function() {
this.cond.check();
assert(this.cond.type === "bool",
"Conditional of if statement must evaluate to a boolean");
this.then_clause.check();
if(this.else_clause) {
this.else_clause.check();
}
}
this.code_gen = function() {
emit("if( ");
this.cond.code_gen();
emit(" ) ");
this.then_cause.code_gen();
if(this.else_clause) {
emit(" else ");
this.else_clause.code_gen();
}
}
}
IfStmt.prototype = stmt_proto;
exports.IfStmt = IfStmt;
function VaryingParameter(iden, expr) {
this.node_type = "varying";
this.node_parent = "node";
this.type = undefined;
this.iden = iden.value;
this.expr = expr;
this.check = function() {
var symbol = sym_tab.find(this.iden);
assert(symbol, "I added it, should be here");
if(this.expr) {
// This varying declaration includes a [re]definition
this.expr.check();
if(symbol.type) {
// This varying was already set, it must be reset to the same type
assert(this.expr.type === symbol.type,
"Variable " + symbol.iden + " set to wrong type");
} else {
// We're initializing its type as well
symbol.type = this.expr.type;
}
} else {
// This is a simple declaration that this variable is a varying
// Make sure that it has been initialized
assert(symbol.type,
"Varying parameter " + symbol.iden + " never initialized");
}
this.type = symbol.type;
}
this.code_gen = function() {
// Generates the definitions of the varying parameter
// if it has an associated expression
if(this.expr) {
emit(this.iden);
emit(' = ');
this.expr.code_gen();
emit(';');
}
}
}
VaryingParameter.prototype = node_proto;
exports.VaryingParameter = VaryingParameter;
function VertexShader(vertex_statements, varyings) {
this.node_type = "vertex_shader";
this.node_parent = "node";
this.type = undefined;
this.statements = vertex_statements;
this.varyings = varyings;
this.check = function() {
for(var i in this.statements) {
this.statements[i].check();
}
// Varyings should already have been checked
}
this.code_gen = function() {
emit('{ ');
for(var i in this.statements) {
this.statements[i].code_gen();
emit('; ');
}
for(var i in this.varyings) {
this.varyings[i].code_gen();
}
emit(' }');
}
}
// TODO URGH!!!!
var __shade_p__ = null;
function ShaderProgram(uniforms, vertex_shader, varyings, fragment_shader) {
this.node_type = "shader_program";
this.node_parent = "node";
this.type = undefined;
this.uniforms = uniforms;
this.varyings = varyings;
this.vertex_shader = new VertexShader(vertex_shader, varyings);
this.fragment_shader = fragment_shader;
this.inferred_uniform_symbols = undefined;
this.index_symbol = null; // Will be set if we're given an index
this.fragment_shader_text = null;
this.vertex_shader_text = null;
this.shader_program = null;
// Infer the types of the uniform parameters from the actual parameters
this.check_with_actual_parameters = function(ths, actual_parameters) {
__shade_p__ = this;
var index_symbol = null;
function infer_symbol_from_parameter(formal, actual) {
var new_symbol;
if(Array.isArray(actual) ||
(actual.constructor.name === "Float32Array")) {
switch(actual.length) {
case 16:
new_symbol = new Symbol(formal, 'mat4');
break;
case 9:
new_symbol = new Symbol(formal, 'mat3');
break;
case 4:
// Could be a mat2 but uncommon, assume vec4
new_symbol = new Symbol(formal, 'vec4');
break;
case 3:
new_symbol = new Symbol(formal, 'vec3');
break;
case 2:
new_symbol = new Symbol(formal, 'vec2');
break;
// TODO boolean, integer types
default:
throw "Unrecognizable parameter type " + actual;
}
} else if(typeof(actual) === "object") {
// Special type (like image) or compound type
if(actual.TEXTURE) {
new_symbol = new Symbol(formal, 'sampler2D');
} else if(actual.ATTRIBUTE) {
new_symbol = new AttributeSymbol(formal, actual.type);
} else if(actual.INDEX) {
// TODO when referenced in the body will be an error
new_symbol = new IndexSymbol(formal);
if(index_symbol) {
throw "A shader program may only have one index parameter. Found second index parameter "
+ formal + " but index parameter " + this.index_symbol.inden + " already processed.";
} else {
index_symbol = new_symbol;
}
} else {
// Assume compound type
new_symbol = new CompoundSymbol(formal);
for(var field in actual) {
new_symbol.fields[field] =
infer_symbol_from_parameter(field, actual[field]);
}
}
} else if(typeof(actual) === "number") {
// TODO figure out how to distinguish ints and floats
new_symbol = new Symbol(formal, 'float');
}
// TODO booleans
if(new_symbol === undefined) {
throw "Unable to infer type of " + formal;
}
return new_symbol;
}
this.inferred_uniform_symbols = [];
for(var i in actual_parameters) {
var parameter = actual_parameters[i];
var iden = this.uniforms[i];
var new_symbol = infer_symbol_from_parameter(iden, parameter);
this.inferred_uniform_symbols.push(new_symbol);
}
// Add this symbol
var this_symbol = new CompoundSymbol('this');
// This is pretty hacky. The idea is that we don't want
// to compute the types of everything defined on this. Instead
// we'll on demand check the types of fields actually used.
// TODO we should probably take this approach to all uniforms
this_symbol.is_this = true;
this_symbol.actual_this = ths;
this.inferred_uniform_symbols.push(this_symbol);
this.index_symbol = index_symbol;
this.check();
};
this.check = function() {
__shade_p__ = this;
this.global_scope = sym_tab;
// Add uniforms and varyings to top level scope
assert(this.inferred_uniform_symbols);
for(var i in this.inferred_uniform_symbols) {
// Uniform types are inferred from the types of the actual arguments
sym_tab.add(this.inferred_uniform_symbols[i]);
}
// Varyings don't yet have types
for(var i in this.varyings) {
sym_tab.add(new VaryingSymbol(this.varyings[i].iden));
}
// Vertex shader defines new scope
this.vertex_scope = new Scope(this.global_scope);
sym_tab = this.vertex_scope;
// Check vertex shader
this.vertex_shader.check();
// Pop scope back to global
sym_tab = this.global_scope;
// Varyings assigned to in the vertex shader should have types now
// Other varyings will get types defined now
for(var i in this.varyings) {
this.varyings[i].check();
}
// Fragment shader defines a scope as well
this.fragment_scope = new Scope(this.global_scope);
sym_tab = this.fragment_scope;
// All varyings should have types defined
this.fragment_shader.check();
// Pop scope
sym_tab = this.global_scope;
}
function gen_attribute(iden_prefix, symbol) {
assert(!symbol.is_function, "Attribute can't be a function");
if(symbol.is_compound) {
iden_prefix += symbol.iden + '_';
for(f in symbol.fields) {
var field_sym = symbol.fields[f];
gen_attribute(iden_prefix, field_sym);
}
} else if(symbol.is_attribute) {
emit("attribute ");
emit(symbol.type + ' ');
emit(iden_prefix + symbol.iden);
emit(';');
} // else not the scope of this function
}
function gen_attributes(symbols) {
for(var i in symbols) {
var symbol = symbols[i];
gen_attribute('', symbol);
}
}