ConditionalNode.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import Node from '../core/Node.js';
  2. import { property } from '../core/PropertyNode.js';
  3. import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
  4. /**
  5. * Represents a logical `if/else` statement. Can be used as an alternative
  6. * to the `If()`/`Else()` syntax.
  7. *
  8. * The corresponding TSL `select()` looks like so:
  9. * ```js
  10. * velocity = position.greaterThanEqual( limit ).select( velocity.negate(), velocity );
  11. * ```
  12. * The `select()` method is called in a chaining fashion on a condition. The parameter nodes of `select()`
  13. * determine the outcome of the entire statement.
  14. *
  15. * @augments Node
  16. */
  17. class ConditionalNode extends Node {
  18. static get type() {
  19. return 'ConditionalNode';
  20. }
  21. /**
  22. * Constructs a new conditional node.
  23. *
  24. * @param {Node} condNode - The node that defines the condition.
  25. * @param {Node} ifNode - The node that is evaluate when the condition ends up `true`.
  26. * @param {?Node} [elseNode=null] - The node that is evaluate when the condition ends up `false`.
  27. */
  28. constructor( condNode, ifNode, elseNode = null ) {
  29. super();
  30. /**
  31. * The node that defines the condition.
  32. *
  33. * @type {Node}
  34. */
  35. this.condNode = condNode;
  36. /**
  37. * The node that is evaluate when the condition ends up `true`.
  38. *
  39. * @type {Node}
  40. */
  41. this.ifNode = ifNode;
  42. /**
  43. * The node that is evaluate when the condition ends up `false`.
  44. *
  45. * @type {?Node}
  46. * @default null
  47. */
  48. this.elseNode = elseNode;
  49. }
  50. /**
  51. * This method is overwritten since the node type is inferred from the if/else
  52. * nodes.
  53. *
  54. * @param {NodeBuilder} builder - The current node builder.
  55. * @return {string} The node type.
  56. */
  57. getNodeType( builder ) {
  58. const { ifNode, elseNode } = builder.getNodeProperties( this );
  59. if ( ifNode === undefined ) {
  60. // fallback setup
  61. this.setup( builder );
  62. return this.getNodeType( builder );
  63. }
  64. const ifType = ifNode.getNodeType( builder );
  65. if ( elseNode !== null ) {
  66. const elseType = elseNode.getNodeType( builder );
  67. if ( builder.getTypeLength( elseType ) > builder.getTypeLength( ifType ) ) {
  68. return elseType;
  69. }
  70. }
  71. return ifType;
  72. }
  73. setup( builder ) {
  74. const condNode = this.condNode.cache();
  75. const ifNode = this.ifNode.cache();
  76. const elseNode = this.elseNode ? this.elseNode.cache() : null;
  77. //
  78. const currentNodeBlock = builder.context.nodeBlock;
  79. builder.getDataFromNode( ifNode ).parentNodeBlock = currentNodeBlock;
  80. if ( elseNode !== null ) builder.getDataFromNode( elseNode ).parentNodeBlock = currentNodeBlock;
  81. //
  82. const properties = builder.getNodeProperties( this );
  83. properties.condNode = condNode;
  84. properties.ifNode = ifNode.context( { nodeBlock: ifNode } );
  85. properties.elseNode = elseNode ? elseNode.context( { nodeBlock: elseNode } ) : null;
  86. }
  87. generate( builder, output ) {
  88. const type = this.getNodeType( builder );
  89. const nodeData = builder.getDataFromNode( this );
  90. if ( nodeData.nodeProperty !== undefined ) {
  91. return nodeData.nodeProperty;
  92. }
  93. const { condNode, ifNode, elseNode } = builder.getNodeProperties( this );
  94. const needsOutput = output !== 'void';
  95. const nodeProperty = needsOutput ? property( type ).build( builder ) : '';
  96. nodeData.nodeProperty = nodeProperty;
  97. const nodeSnippet = condNode.build( builder, 'bool' );
  98. builder.addFlowCode( `\n${ builder.tab }if ( ${ nodeSnippet } ) {\n\n` ).addFlowTab();
  99. let ifSnippet = ifNode.build( builder, type );
  100. if ( ifSnippet ) {
  101. if ( needsOutput ) {
  102. ifSnippet = nodeProperty + ' = ' + ifSnippet + ';';
  103. } else {
  104. ifSnippet = 'return ' + ifSnippet + ';';
  105. }
  106. }
  107. builder.removeFlowTab().addFlowCode( builder.tab + '\t' + ifSnippet + '\n\n' + builder.tab + '}' );
  108. if ( elseNode !== null ) {
  109. builder.addFlowCode( ' else {\n\n' ).addFlowTab();
  110. let elseSnippet = elseNode.build( builder, type );
  111. if ( elseSnippet ) {
  112. if ( needsOutput ) {
  113. elseSnippet = nodeProperty + ' = ' + elseSnippet + ';';
  114. } else {
  115. elseSnippet = 'return ' + elseSnippet + ';';
  116. }
  117. }
  118. builder.removeFlowTab().addFlowCode( builder.tab + '\t' + elseSnippet + '\n\n' + builder.tab + '}\n\n' );
  119. } else {
  120. builder.addFlowCode( '\n\n' );
  121. }
  122. return builder.format( nodeProperty, type, output );
  123. }
  124. }
  125. export default ConditionalNode;
  126. /**
  127. * TSL function for creating a conditional node.
  128. *
  129. * @tsl
  130. * @function
  131. * @param {Node} condNode - The node that defines the condition.
  132. * @param {Node} ifNode - The node that is evaluate when the condition ends up `true`.
  133. * @param {?Node} [elseNode=null] - The node that is evaluate when the condition ends up `false`.
  134. * @returns {ConditionalNode}
  135. */
  136. export const select = /*@__PURE__*/ nodeProxy( ConditionalNode );
  137. addMethodChaining( 'select', select );
  138. // Deprecated
  139. /**
  140. * @tsl
  141. * @function
  142. * @deprecated since r168. Use {@link select} instead.
  143. *
  144. * @param {...any} params
  145. * @returns {ConditionalNode}
  146. */
  147. export const cond = ( ...params ) => { // @deprecated, r168
  148. console.warn( 'TSL.ConditionalNode: cond() has been renamed to select().' );
  149. return select( ...params );
  150. };
  151. addMethodChaining( 'cond', cond );
粤ICP备19079148号