OperatorNode.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 === '=' ) {
  25. return typeA;
  26. } else if ( op === '==' || op === '&&' ) {
  27. return 'bool';
  28. } else if ( op === '<=' || op === '>' ) {
  29. const length = builder.getTypeLength( output );
  30. return length > 1 ? `bvec${ length }` : 'bool';
  31. } else {
  32. if ( typeA === 'float' && builder.isMatrix( typeB ) ) {
  33. return typeB;
  34. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  35. // matrix x vector
  36. return builder.getVectorFromMatrix( typeA );
  37. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  38. // vector x matrix
  39. return builder.getVectorFromMatrix( typeB );
  40. } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  41. // anytype x anytype: use the greater length vector
  42. return typeB;
  43. }
  44. return typeA;
  45. }
  46. }
  47. generate( builder, output ) {
  48. const op = this.op;
  49. const aNode = this.aNode;
  50. const bNode = this.bNode;
  51. const type = this.getNodeType( builder, output );
  52. let typeA = null;
  53. let typeB = null;
  54. if ( type !== 'void' ) {
  55. typeA = aNode.getNodeType( builder );
  56. typeB = bNode.getNodeType( builder );
  57. if ( op === '=' ) {
  58. typeB = typeA;
  59. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  60. // matrix x vector
  61. typeB = builder.getVectorFromMatrix( typeA );
  62. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  63. // vector x matrix
  64. typeA = builder.getVectorFromMatrix( typeB );
  65. } else {
  66. // anytype x anytype
  67. typeA = typeB = type;
  68. }
  69. } else {
  70. typeA = typeB = type;
  71. }
  72. const a = aNode.build( builder, typeA );
  73. const b = bNode.build( builder, typeB );
  74. const outputLength = builder.getTypeLength( output );
  75. if ( output !== 'void' ) {
  76. if ( op === '=' ) {
  77. builder.addFlowCode( `${a} ${this.op} ${b}` );
  78. return a;
  79. } else if ( op === '>' && outputLength > 1 ) {
  80. return `greaterThan( ${a}, ${b} )`;
  81. } else if ( op === '<=' && outputLength > 1 ) {
  82. return `lessThanEqual( ${a}, ${b} )`;
  83. } else {
  84. return `( ${a} ${this.op} ${b} )`;
  85. }
  86. } else if ( typeA !== 'void' ) {
  87. return `${a} ${this.op} ${b}`;
  88. }
  89. }
  90. }
  91. export default OperatorNode;