OperatorNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 ) {
  17. const op = this.op;
  18. const aNode = this.aNode;
  19. const bNode = this.bNode;
  20. if ( op === '=' ) {
  21. return aNode.getNodeType( builder );
  22. } else if ( op === '==' || op === '>' || op === '&&' ) {
  23. return 'bool';
  24. } else {
  25. const typeA = aNode.getNodeType( builder );
  26. const typeB = bNode.getNodeType( builder );
  27. if ( typeA === 'float' && builder.isMatrix( typeB ) ) {
  28. return typeB;
  29. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  30. // matrix x vector
  31. return builder.getVectorFromMatrix( typeA );
  32. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  33. // vector x matrix
  34. return builder.getVectorFromMatrix( typeB );
  35. } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  36. // anytype x anytype: use the greater length vector
  37. return typeB;
  38. }
  39. return typeA;
  40. }
  41. }
  42. generate( builder, output ) {
  43. const op = this.op;
  44. const aNode = this.aNode;
  45. const bNode = this.bNode;
  46. let typeA = aNode.getNodeType( builder );
  47. let typeB = bNode.getNodeType( builder );
  48. if ( op === '=' ) {
  49. typeB = typeA;
  50. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  51. // matrix x vector
  52. typeB = builder.getVectorFromMatrix( typeA );
  53. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  54. // vector x matrix
  55. typeA = builder.getVectorFromMatrix( typeB );
  56. } else {
  57. // anytype x anytype
  58. typeA = typeB = this.getNodeType( builder );
  59. }
  60. const a = aNode.build( builder, typeA );
  61. const b = bNode.build( builder, typeB );
  62. if ( output !== 'void' ) {
  63. if ( op === '=' ) {
  64. builder.addFlowCode( `${a} ${this.op} ${b}` );
  65. return a;
  66. } else {
  67. return `( ${a} ${this.op} ${b} )`;
  68. }
  69. } else {
  70. return `${a} ${this.op} ${b}`;
  71. }
  72. }
  73. }
  74. export default OperatorNode;