CondNode.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Node from '../core/Node.js';
  2. import PropertyNode from '../core/PropertyNode.js';
  3. import ContextNode from '../core/ContextNode.js';
  4. class CondNode extends Node {
  5. constructor( condNode, ifNode, elseNode ) {
  6. super();
  7. this.condNode = condNode;
  8. this.ifNode = ifNode;
  9. this.elseNode = elseNode;
  10. }
  11. getNodeType( builder ) {
  12. const ifType = this.ifNode.getNodeType( builder );
  13. const elseType = this.elseNode.getNodeType( builder );
  14. if ( builder.getTypeLength( elseType ) > builder.getTypeLength( ifType ) ) {
  15. return elseType;
  16. }
  17. return ifType;
  18. }
  19. generate( builder ) {
  20. const type = this.getNodeType( builder );
  21. const context = { tempWrite: false };
  22. const nodeProperty = new PropertyNode( null, type ).build( builder );
  23. const nodeSnippet = new ContextNode( this.condNode/*, context*/ ).build( builder, 'bool' ),
  24. ifSnippet = new ContextNode( this.ifNode, context ).build( builder, type ),
  25. elseSnippet = new ContextNode( this.elseNode, context ).build( builder, type );
  26. builder.addFlowCode( `if ( ${nodeSnippet} ) {
  27. \t\t${nodeProperty} = ${ifSnippet};
  28. \t} else {
  29. \t\t${nodeProperty} = ${elseSnippet};
  30. \t}` );
  31. return nodeProperty;
  32. }
  33. }
  34. export default CondNode;