OperatorNode.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 length = builder.getTypeLength( output );
  32. return length > 1 ? `bvec${ length }` : '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 ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  62. // matrix x vector
  63. typeB = builder.getVectorFromMatrix( typeA );
  64. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  65. // vector x matrix
  66. typeA = builder.getVectorFromMatrix( typeB );
  67. } else {
  68. // anytype x anytype
  69. typeA = typeB = type;
  70. }
  71. } else {
  72. typeA = typeB = type;
  73. }
  74. const a = aNode.build( builder, typeA );
  75. const b = bNode.build( builder, typeB );
  76. const outputLength = builder.getTypeLength( output );
  77. if ( output !== 'void' ) {
  78. if ( op === '=' ) {
  79. builder.addFlowCode( `${a} ${this.op} ${b}` );
  80. return a;
  81. } else if ( op === '>' && outputLength > 1 ) {
  82. return builder.format( `${ builder.getMethod( 'greaterThan' ) }( ${a}, ${b} )`, type, output );
  83. } else if ( op === '<=' && outputLength > 1 ) {
  84. return builder.format( `${ builder.getMethod( 'lessThanEqual' ) }( ${a}, ${b} )`, type, output );
  85. } else {
  86. return builder.format( `( ${a} ${this.op} ${b} )`, type, output );
  87. }
  88. } else if ( typeA !== 'void' ) {
  89. return builder.format( `${a} ${this.op} ${b}`, type, output );
  90. }
  91. }
  92. serialize( data ) {
  93. super.serialize( data );
  94. data.op = this.op;
  95. }
  96. deserialize( data ) {
  97. super.deserialize( data );
  98. this.op = data.op;
  99. }
  100. }
  101. export default OperatorNode;