OperatorNode.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 'bool';
  20. } else {
  21. const typeA = this.a.getNodeType( builder );
  22. const typeB = this.b.getNodeType( builder );
  23. if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  24. // matrix x vector
  25. return builder.getVectorFromMatrix( typeA );
  26. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  27. // vector x matrix
  28. return builder.getVectorFromMatrix( typeB );
  29. } else if ( builder.getTypeLength( typeB ) > builder.getTypeLength( typeA ) ) {
  30. // anytype x anytype: use the greater length vector
  31. return typeB;
  32. }
  33. return typeA;
  34. }
  35. }
  36. generate( builder, output ) {
  37. let typeA = this.a.getNodeType( builder );
  38. let typeB = this.b.getNodeType( builder );
  39. let type = this.getNodeType( builder );
  40. if ( builder.isMatrix( typeA ) && builder.isVector( typeB ) ) {
  41. // matrix x vector
  42. type = typeB = builder.getVectorFromMatrix( typeA );
  43. } else if ( builder.isVector( typeA ) && builder.isMatrix( typeB ) ) {
  44. // vector x matrix
  45. type = typeB = builder.getVectorFromMatrix( typeB );
  46. } else {
  47. // anytype x anytype
  48. typeA = typeB = type;
  49. }
  50. const a = this.a.build( builder, typeA );
  51. const b = this.b.build( builder, typeB );
  52. return `( ${a} ${this.op} ${b} )`;
  53. }
  54. }
  55. export default OperatorNode;