OperatorNode.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import TempNode from '../core/TempNode.js';
  2. class OperatorNode extends TempNode {
  3. constructor( op, aNode, bNode, ...params ) {
  4. super();
  5. this.op = op;
  6. if ( params.length > 0 ) {
  7. let finalBNode = bNode;
  8. for ( let i = 0; i < params.length; i ++ ) {
  9. finalBNode = new OperatorNode( op, finalBNode, params[ i ] );
  10. }
  11. bNode = finalBNode;
  12. }
  13. this.aNode = aNode;
  14. this.bNode = bNode;
  15. }
  16. getNodeType( builder, output ) {
  17. const op = this.op;
  18. const aNode = this.aNode;
  19. const bNode = this.bNode;
  20. const typeA = aNode.getNodeType( builder );
  21. const typeB = bNode.getNodeType( builder );
  22. if ( typeA === 'void' || typeB === 'void' ) {
  23. return 'void';
  24. } else if ( op === '=' || op === '%' ) {
  25. return typeA;
  26. } else if ( op === '&' || op === '|' || op === '^' || op === '>>' || op === '<<' ) {
  27. return 'int';
  28. } else if ( op === '==' || op === '&&' || op === '||' || op === '^^' ) {
  29. return 'bool';
  30. } else if ( op === '<' || op === '>' || op === '<=' || op === '>=' ) {
  31. const typeLength = builder.getTypeLength( output );
  32. return typeLength > 1 ? `bvec${ typeLength }` : 'bool';
  33. } else {
  34. if ( typeA === 'float' && builder.isMatrix( typeB ) ) {
  35. return typeB;
  36. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  37. // matrix x vector
  38. return builder.getVectorFromMatrix( typeA );
  39. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  40. // vector x matrix
  41. return builder.getVectorFromMatrix( typeB );
  42. } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  43. // anytype x anytype: use the greater length vector
  44. return typeB;
  45. }
  46. return typeA;
  47. }
  48. }
  49. generate( builder, output ) {
  50. const op = this.op;
  51. const aNode = this.aNode;
  52. const bNode = this.bNode;
  53. const type = this.getNodeType( builder, output );
  54. let typeA = null;
  55. let typeB = null;
  56. if ( type !== 'void' ) {
  57. typeA = aNode.getNodeType( builder );
  58. typeB = bNode.getNodeType( builder );
  59. if ( op === '=' ) {
  60. typeB = typeA;
  61. } else if ( op === '<' || op === '>' || op === '<=' || op === '>=' ) {
  62. if ( builder.isVector( typeA ) ) {
  63. typeB = typeA;
  64. } else {
  65. typeA = typeB = 'float';
  66. }
  67. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  68. // matrix x vector
  69. typeB = builder.getVectorFromMatrix( typeA );
  70. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  71. // vector x matrix
  72. typeA = builder.getVectorFromMatrix( typeB );
  73. } else {
  74. // anytype x anytype
  75. typeA = typeB = type;
  76. }
  77. } else {
  78. typeA = typeB = type;
  79. }
  80. const a = aNode.build( builder, typeA );
  81. const b = bNode.build( builder, typeB );
  82. const outputLength = builder.getTypeLength( output );
  83. if ( output !== 'void' ) {
  84. if ( op === '=' ) {
  85. builder.addFlowCode( `${a} ${this.op} ${b}` );
  86. return a;
  87. } else if ( op === '>' && outputLength > 1 ) {
  88. return builder.format( `${ builder.getMethod( 'greaterThan' ) }( ${a}, ${b} )`, type, output );
  89. } else if ( op === '<=' && outputLength > 1 ) {
  90. return builder.format( `${ builder.getMethod( 'lessThanEqual' ) }( ${a}, ${b} )`, type, output );
  91. } else {
  92. return builder.format( `( ${a} ${this.op} ${b} )`, type, output );
  93. }
  94. } else if ( typeA !== 'void' ) {
  95. return builder.format( `${a} ${this.op} ${b}`, type, output );
  96. }
  97. }
  98. serialize( data ) {
  99. super.serialize( data );
  100. data.op = this.op;
  101. }
  102. deserialize( data ) {
  103. super.deserialize( data );
  104. this.op = data.op;
  105. }
  106. }
  107. export default OperatorNode;