OperatorNode.js 2.5 KB

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