FunctionCallNode.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import TempNode from './TempNode.js';
  2. class FunctionCallNode extends TempNode {
  3. constructor( functionNode = null, parameters = {} ) {
  4. super();
  5. this.functionNode = functionNode;
  6. this.parameters = parameters;
  7. }
  8. setParameters( parameters ) {
  9. this.parameters = parameters;
  10. return this;
  11. }
  12. getParameters() {
  13. return this.parameters;
  14. }
  15. getType( builder ) {
  16. return this.functionNode.getType( builder );
  17. }
  18. generate( builder, output ) {
  19. const params = [];
  20. const functionNode = this.functionNode;
  21. const inputs = functionNode.getInputs( builder );
  22. const parameters = this.parameters;
  23. for ( const inputNode of inputs ) {
  24. const node = parameters[ inputNode.name ];
  25. if ( node !== undefined ) {
  26. params.push( node.build( builder, inputNode.type ) );
  27. } else {
  28. throw new Error( `FunctionCallNode: Input '${inputNode.name}' not found in FunctionNode.` );
  29. }
  30. }
  31. const type = this.getType( builder );
  32. const functionName = functionNode.build( builder, 'property' );
  33. const callSnippet = `${functionName}( ${params.join( ', ' )} )`;
  34. return builder.format( callSnippet, type, output );
  35. }
  36. }
  37. export default FunctionCallNode;