OperatorNode.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { TempNode } from '../core/TempNode.js';
  2. class OperatorNode extends TempNode {
  3. constructor( a, b, op ) {
  4. super();
  5. this.a = a;
  6. this.b = b;
  7. this.op = op;
  8. }
  9. getType( builder ) {
  10. const a = this.a.getType( builder ),
  11. b = this.b.getType( builder );
  12. if ( builder.isTypeMatrix( a ) ) {
  13. return 'v4';
  14. } else if ( builder.getTypeLength( b ) > builder.getTypeLength( a ) ) {
  15. // use the greater length vector
  16. return b;
  17. }
  18. return a;
  19. }
  20. generate( builder, output ) {
  21. const type = this.getType( builder );
  22. const a = this.a.build( builder, type ),
  23. b = this.b.build( builder, type );
  24. return builder.format( '( ' + a + ' ' + this.op + ' ' + b + ' )', type, output );
  25. }
  26. copy( source ) {
  27. super.copy( source );
  28. this.a = source.a;
  29. this.b = source.b;
  30. this.op = source.op;
  31. return this;
  32. }
  33. toJSON( meta ) {
  34. let data = this.getJSONNode( meta );
  35. if ( ! data ) {
  36. data = this.createJSONNode( meta );
  37. data.a = this.a.toJSON( meta ).uuid;
  38. data.b = this.b.toJSON( meta ).uuid;
  39. data.op = this.op;
  40. }
  41. return data;
  42. }
  43. }
  44. OperatorNode.ADD = '+';
  45. OperatorNode.SUB = '-';
  46. OperatorNode.MUL = '*';
  47. OperatorNode.DIV = '/';
  48. OperatorNode.prototype.nodeType = 'Operator';
  49. export { OperatorNode };