diff --git a/src/backend/functionBuilder.js b/src/backend/functionBuilder.js index 6639e6e4..04388b4a 100644 --- a/src/backend/functionBuilder.js +++ b/src/backend/functionBuilder.js @@ -15,8 +15,9 @@ var functionBuilder = (function() { /// /// [Constructor] Blank constructor, which initializes the properties /// - function functionBuilder() { + function functionBuilder(gpu) { this.nodeMap = {}; + this.gpu = gpu; } /// @@ -24,14 +25,15 @@ var functionBuilder = (function() { /// /// Creates the functionNode, and add it to the nodeMap /// - /// Parameters: + /// Parameters: + /// gpu - {GPU} The GPU instance /// functionName - {String} Function name to assume, if its null, it attempts to extract from the function - /// jsFunction - {JS Function} JS Function to do conversion + /// jsFunction - {JS Function} JS Function to do conversion /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null /// returnType - {String} The return type, assumes "float" if null /// function addFunction( functionName, jsFunction, paramTypeArray, returnType ) { - this.addFunctionNode( new functionNode( functionName, jsFunction, paramTypeArray, returnType ) ); + this.addFunctionNode( new functionNode( this.gpu, functionName, jsFunction, paramTypeArray, returnType ) ); } functionBuilder.prototype.addFunction = addFunction; @@ -40,7 +42,7 @@ var functionBuilder = (function() { /// /// Add the funciton node directly /// - /// Parameters: + /// Parameters: /// inNode - {functionNode} functionNode to add /// function addFunctionNode( inNode ) { @@ -56,7 +58,7 @@ var functionBuilder = (function() { /// This allow for "uneeded" functions to be automatically optimized out. /// Note that the 0-index, is the starting function trace. /// - /// Parameters: + /// Parameters: /// functionName - {String} Function name to trace from, default to "kernel" /// retList - {[String,...]} Returning list of function names that is traced. Including itself. /// @@ -88,7 +90,7 @@ var functionBuilder = (function() { /// /// Function: webglString_fromFunctionNames /// - /// Parameters: + /// Parameters: /// functionList - {[String,...]} List of function to build the webgl string. /// /// Returns: @@ -109,7 +111,7 @@ var functionBuilder = (function() { /// /// Function: webglString /// - /// Parameters: + /// Parameters: /// functionName - {String} Function name to trace from. If null, it returns the WHOLE builder stack /// /// Returns: @@ -118,7 +120,7 @@ var functionBuilder = (function() { function webglString(functionName) { if(functionName) { return this.webglString_fromFunctionNames( this.traceFunctionCalls(functionName, []).reverse() ); - } + } return this.webglString_fromFunctionNames(Object.keys(this.nodeMap)); } functionBuilder.prototype.webglString = webglString; diff --git a/src/backend/functionNode.js b/src/backend/functionNode.js index 10f6661f..7bc36958 100644 --- a/src/backend/functionNode.js +++ b/src/backend/functionNode.js @@ -31,12 +31,15 @@ var functionNode = (function() { /// [Constructor] Builds the function with the given JS function, and argument type array. /// /// Parameters: + /// gpu - {GPU} The GPU instance /// functionName - {String} Function name to assume, if its null, it attempts to extract from the function /// jsFunction - {JS Function} JS Function to do conversion /// paramTypeArray - {[String,...]} Parameter type array, assumes all parameters are "float" if null /// returnType - {String} The return type, assumes "float" if null /// - function functionNode( functionName, jsFunction, paramTypeArray, returnType ) { + function functionNode( gpu, functionName, jsFunction, paramTypeArray, returnType ) { + + this.gpu = gpu; // // Internal vars setup diff --git a/src/backend/functionNode_webgl.js b/src/backend/functionNode_webgl.js index 583d0a07..603516b8 100644 --- a/src/backend/functionNode_webgl.js +++ b/src/backend/functionNode_webgl.js @@ -1,6 +1,8 @@ // Closure capture for the ast function, prevent collision with existing AST functions var functionNode_webgl = (function() { + var gpu, jsFunctionString; + /// /// Function: functionNode_webgl /// @@ -13,6 +15,8 @@ var functionNode_webgl = (function() { /// the converted webGL function string /// function functionNode_webgl( inNode ) { + gpu = inNode.gpu; + jsFunctionString = inNode.jsFunctionString; inNode.webglFunctionString_array = ast_generic( inNode.getJS_AST(), [], inNode ); inNode.webglFunctionString = inNode.webglFunctionString_array.join("").trim(); return inNode.webglFunctionString; @@ -49,6 +53,8 @@ var functionNode_webgl = (function() { } switch(ast.type) { + case "FunctionDeclaration": + return ast_FunctionDeclaration(ast, retArr, funcParam); case "FunctionExpression": return ast_FunctionExpression(ast, retArr, funcParam); case "ReturnStatement": @@ -99,6 +105,38 @@ var functionNode_webgl = (function() { } } + /// Prases the abstract syntax tree, to its named function declartion + /// + /// @param ast the AST object to parse + /// @param retArr return array string + /// @param funcParam FunctionNode, that tracks compilation state + /// + /// @returns the appened retArr + function ast_FunctionDeclaration(ast, retArr, funcParam) { + // TODO: make this less hacky? + var lines = jsFunctionString.split(/\r?\n/); + + var start = ast.loc.start; + var end = ast.loc.end; + + var funcArr = []; + + funcArr.push(lines[start.line-1].slice(start.column)); + for (var i=start.line; i