OperatorNode.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import TempNode from '../core/TempNode.js';
  2. class OperatorNode extends TempNode {
  3. constructor( op, a, b ) {
  4. super();
  5. this.op = op;
  6. this.a = a;
  7. this.b = b;
  8. }
  9. getType( builder ) {
  10. const typeA = this.a.getType( builder );
  11. const typeB = this.b.getType( builder );
  12. if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  13. // matrix x vector
  14. return typeB;
  15. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  16. // vector x matrix
  17. return typeA;
  18. } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  19. // anytype x anytype: use the greater length vector
  20. return typeB;
  21. }
  22. return typeA;
  23. }
  24. generate( builder, output ) {
  25. let typeA = this.a.getType( builder );
  26. let typeB = this.b.getType( builder );
  27. let type = this.getType( builder );
  28. if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  29. // matrix x vector
  30. type = typeB = builder.getVectorFromMatrix( typeA );
  31. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  32. // vector x matrix
  33. type = typeB = builder.getVectorFromMatrix( typeB );
  34. } else {
  35. // anytype x anytype
  36. typeA = typeB = type;
  37. }
  38. const a = this.a.build( builder, typeA );
  39. const b = this.b.build( builder, typeB );
  40. return builder.format( `( ${a} ${this.op} ${b} )`, type, output );
  41. }
  42. }
  43. export default OperatorNode;