class CppParameter { name = ""; type = ""; } class CppClassMember { name = ""; isMethod = false; isVirtual = false; isStatic = false; type = "void"; parameters = []; constructor( nameDefinition, body ) { if ( nameDefinition.endsWith( "()" ) ) { this.isMethod = true; nameDefinition = nameDefinition.replace( /\(\)$/, "" ); } let names = nameDefinition.split( /\s+/ ); this.name = names[ names.length -1 ]; for ( let i = 0; i < names.length - 1; i++ ) { if ( "virtual" === names[ i ] ) { this.isVirtual = true; } if ( "static" === names[ i ] ) { this.isStatic = true; } } if ( this.isMethod ) { this.parseMethodBody( body ); } } parseMethodBody( body ) { if ( ! body ) { return; } this.type = body.type || "void"; let parameters = body.parameters; if ( ! parameters ) { return; } for ( let i = 0; i < parameters.length; i++ ) { let pm = parameters[ i ]; let cppParameter = new CppParameter(); for ( let it in pm ) { if ( ! pm.hasOwnProperty( it ) ) { continue; } cppParameter.name = it; cppParameter.type = pm[ it ]; } this.parameters.push( cppParameter ); } } getMethodBinding( className ) { if ( this.isVirtual ) { return `GDVIRTUAL_BIND( ${this.name} );`; } else { return `ClassDB::bind_method( D_METHOD( "${this.name}" ) , &${className}::${this.name} );` } } getHeaderDefinition() { if ( this.isMethod ) { return this.getMethodHeaderDefinition(); } else { return this.getVariableHeaderDefinition(); } } getParametersDefinition() { return this.parameters.map( p => p.type + " " + p.name ).join( ", " ); } getMethodHeaderDefinition() { if ( this.isVirtual ) { let numParameters = this.parameters.length; let methodReturnType = this.type === "void" ? "" : "R"; let returnTypeDefinition = this.type === "void" ? "" : ( this.type + ", " ) ; let parametersDefinition = ""; if ( numParameters > 0 ) { parametersDefinition = ", " + this.parameters.map( p => p.type ).join( ", " ); } return `GDVIRTUAL${numParameters}${methodReturnType}( ${returnTypeDefinition}${this.name}${parametersDefinition} );` } return `${this.type} ${this.name}(${this.parametersDefinition});` } getVariableHeaderDefinition() { return `${this.type} ${this.name};` } } module.exports = { CppParameter, CppClassMember }