StructNode.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import CodeNode from './CodeNode.js';
  2. import StructVarNode from './StructVarNode.js';
  3. class StructNode extends CodeNode {
  4. constructor( inputs = {}, name = '' ) {
  5. super();
  6. this.inputs = inputs;
  7. this.name = name;
  8. }
  9. getNodeType( builder ) {
  10. if ( this.name !== '' ) {
  11. return this.name;
  12. } else {
  13. const codeNode = builder.getCodeFromNode( this, 'code' );
  14. return codeNode.name;
  15. }
  16. }
  17. create( inputs = {} ) {
  18. return new StructVarNode( this, inputs );
  19. }
  20. generate( builder, output ) {
  21. const type = this.getNodeType( builder );
  22. const inputs = this.inputs;
  23. let code = `struct ${type} {\n`;
  24. for ( const inputName in inputs ) {
  25. const inputType = inputs[ inputName ];
  26. code += `\t${inputType} ${inputName};\n`;
  27. }
  28. code += `};`;
  29. this.code = code;
  30. super.generate( builder, output );
  31. if ( output === 'var' ) {
  32. const nodeData = builder.getDataFromNode( this );
  33. if ( nodeData.index === undefined ) {
  34. nodeData.index = 0;
  35. }
  36. return `structVar${nodeData.index ++}`;
  37. } else {
  38. return code;
  39. }
  40. }
  41. }
  42. export default StructNode;