OperatorNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import TempNode from '../core/TempNode.js';
  2. class OperatorNode extends TempNode {
  3. constructor( op, a, b, ...params ) {
  4. super();
  5. this.op = op;
  6. if ( params.length > 0 ) {
  7. let finalB = b;
  8. for ( let i = 0; i < params.length; i ++ ) {
  9. finalB = new OperatorNode( op, finalB, params[ i ] );
  10. }
  11. b = finalB;
  12. }
  13. this.a = a;
  14. this.b = b;
  15. }
  16. getNodeType( builder ) {
  17. const op = this.op;
  18. if ( op === '=' ) {
  19. return this.a.getNodeType( builder );
  20. } else if ( op === '==' || op === '>' || op === '&&' ) {
  21. return 'bool';
  22. } else {
  23. const typeA = this.a.getNodeType( builder );
  24. const typeB = this.b.getNodeType( builder );
  25. if ( typeA === 'float' && builder.isMatrix( typeB ) ) {
  26. return typeB;
  27. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  28. // matrix x vector
  29. return builder.getVectorFromMatrix( typeA );
  30. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  31. // vector x matrix
  32. return builder.getVectorFromMatrix( typeB );
  33. } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  34. // anytype x anytype: use the greater length vector
  35. return typeB;
  36. }
  37. return typeA;
  38. }
  39. }
  40. generate( builder, output ) {
  41. const op = this.op;
  42. let typeA = this.a.getNodeType( builder );
  43. let typeB = this.b.getNodeType( builder );
  44. if ( op === '=' ) {
  45. typeB = typeA;
  46. } else if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  47. // matrix x vector
  48. typeB = builder.getVectorFromMatrix( typeA );
  49. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  50. // vector x matrix
  51. typeA = builder.getVectorFromMatrix( typeB );
  52. } else {
  53. // anytype x anytype
  54. typeA = typeB = this.getNodeType( builder );
  55. }
  56. const a = this.a.build( builder, typeA );
  57. const b = this.b.build( builder, typeB );
  58. return `( ${a} ${this.op} ${b} )`;
  59. }
  60. }
  61. export default OperatorNode;