CondNode.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @author sunag / http://www.sunag.com.br/
  3. */
  4. import { TempNode } from '../core/TempNode.js';
  5. function CondNode( a, b, op, ifNode, elseNode ) {
  6. TempNode.call( this );
  7. this.a = a;
  8. this.b = b;
  9. this.op = op;
  10. this.ifNode = ifNode;
  11. this.elseNode = elseNode;
  12. }
  13. CondNode.EQUAL = '==';
  14. CondNode.NOT_EQUAL = '!=';
  15. CondNode.GREATER = '>';
  16. CondNode.GREATER_EQUAL = '>=';
  17. CondNode.LESS = '<';
  18. CondNode.LESS_EQUAL = '<=';
  19. CondNode.prototype = Object.create( TempNode.prototype );
  20. CondNode.prototype.constructor = CondNode;
  21. CondNode.prototype.nodeType = "Cond";
  22. CondNode.prototype.getType = function ( builder ) {
  23. if ( this.ifNode ) {
  24. var ifType = this.ifNode.getType( builder );
  25. var elseType = this.elseNode.getType( builder );
  26. if ( builder.getTypeLength( elseType ) > builder.getTypeLength( ifType ) ) {
  27. return elseType;
  28. }
  29. return ifType;
  30. }
  31. return 'b';
  32. };
  33. CondNode.prototype.getCondType = function ( builder ) {
  34. if ( builder.getTypeLength( this.b.getType( builder ) ) > builder.getTypeLength( this.a.getType( builder ) ) ) {
  35. return this.b.getType( builder );
  36. }
  37. return this.a.getType( builder );
  38. };
  39. CondNode.prototype.generate = function ( builder, output ) {
  40. var type = this.getType( builder ),
  41. condType = this.getCondType( builder ),
  42. a = this.a.build( builder, condType ),
  43. b = this.b.build( builder, condType ),
  44. code;
  45. if ( this.ifNode ) {
  46. var ifCode = this.ifNode.build( builder, type ),
  47. elseCode = this.elseNode.build( builder, type );
  48. code = '( ' + [ a, this.op, b, '?', ifCode, ':', elseCode ].join( ' ' ) + ' )';
  49. } else {
  50. code = '( ' + a + ' ' + this.op + ' ' + b + ' )';
  51. }
  52. return builder.format( code, this.getType( builder ), output );
  53. };
  54. CondNode.prototype.copy = function ( source ) {
  55. TempNode.prototype.copy.call( this, source );
  56. this.a = source.a;
  57. this.b = source.b;
  58. this.op = source.op;
  59. this.ifNode = source.ifNode;
  60. this.elseNode = source.elseNode;
  61. return this;
  62. };
  63. CondNode.prototype.toJSON = function ( meta ) {
  64. var data = this.getJSONNode( meta );
  65. if ( ! data ) {
  66. data = this.createJSONNode( meta );
  67. data.a = this.a.toJSON( meta ).uuid;
  68. data.b = this.b.toJSON( meta ).uuid;
  69. data.op = this.op;
  70. if ( data.ifNode ) data.ifNode = this.ifNode.toJSON( meta ).uuid;
  71. if ( data.elseNode ) data.elseNode = this.elseNode.toJSON( meta ).uuid;
  72. }
  73. return data;
  74. };
  75. export { CondNode };