@@ -3,6 +3,23 @@ var functionNode_webgl = (function() {
33
44 var gpu , jsFunctionString ;
55
6+ function isIdentifierKernelParam ( paramName , ast , funcParam ) {
7+ return funcParam . paramNames . indexOf ( paramName ) != - 1 ;
8+ }
9+
10+ function ensureIndentifierType ( paramName , expectedType , ast , funcParam ) {
11+ var start = ast . loc . start ;
12+
13+ if ( ! isIdentifierKernelParam ( paramName , funcParam ) && expectedType != 'float' ) {
14+ throw "Error unxpected identifier " + paramName + " on line " + start . line ;
15+ } else {
16+ var actualType = funcParam . paramType [ funcParam . paramNames . indexOf ( paramName ) ] ;
17+ if ( actualType != expectedType ) {
18+ throw "Error unxpected identifier " + paramName + " on line " + start . line ;
19+ }
20+ }
21+ }
22+
623 ///
724 /// Function: functionNode_webgl
825 ///
@@ -81,6 +98,8 @@ var functionNode_webgl = (function() {
8198 return ast_ContinueStatement ( ast , retArr , funcParam ) ;
8299 case "ForStatement" :
83100 return ast_ForStatement ( ast , retArr , funcParam ) ;
101+ case "WhileStatement" :
102+ return ast_WhileStatement ( ast , retArr , funcParam ) ;
84103 case "VariableDeclaration" :
85104 return ast_VariableDeclaration ( ast , retArr , funcParam ) ;
86105 case "VariableDeclarator" :
@@ -148,24 +167,26 @@ var functionNode_webgl = (function() {
148167
149168 // Setup function return type and name
150169 if ( funcParam . isRootKernel ) {
151- retArr . push ( "vec4 " ) ;
170+ retArr . push ( "float " ) ;
152171 } else {
153172 retArr . push ( funcParam . returnType ) ;
154173 }
155174 retArr . push ( " " ) ;
156175 retArr . push ( funcParam . functionName ) ;
157176 retArr . push ( "(" ) ;
158177
159- // Arguments handling
160- for ( var i = 0 ; i < funcParam . paramNames . length ; ++ i ) {
161- if ( i > 0 ) {
162- retArr . push ( ", " ) ;
178+ if ( ! funcParam . isRootKernel ) {
179+ // Arguments handling
180+ for ( var i = 0 ; i < funcParam . paramNames . length ; ++ i ) {
181+ if ( i > 0 ) {
182+ retArr . push ( ", " ) ;
183+ }
184+
185+ retArr . push ( funcParam . paramType [ i ] ) ;
186+ retArr . push ( " " ) ;
187+ retArr . push ( "user_" ) ;
188+ retArr . push ( funcParam . paramNames [ i ] ) ;
163189 }
164-
165- retArr . push ( funcParam . paramType [ i ] ) ;
166- retArr . push ( " " ) ;
167- retArr . push ( "user_" ) ;
168- retArr . push ( funcParam . paramNames [ i ] ) ;
169190 }
170191
171192 // Function opening
@@ -178,7 +199,7 @@ var functionNode_webgl = (function() {
178199 }
179200
180201 if ( funcParam . isRootKernel ) {
181- retArr . push ( "\nreturn vec4( 0.0) ;" ) ;
202+ retArr . push ( "\nreturn 0.0;" ) ;
182203 }
183204
184205 // Function closing
@@ -194,15 +215,9 @@ var functionNode_webgl = (function() {
194215 ///
195216 /// @returns the appened retArr
196217 function ast_ReturnStatement ( ast , retArr , funcParam ) {
197- if ( funcParam . isRootKernel ) {
198- retArr . push ( "return encode32(" ) ;
199- ast_generic ( ast . argument , retArr , funcParam ) ;
200- retArr . push ( "); " ) ;
201- } else {
202- retArr . push ( "return " ) ;
203- ast_generic ( ast . argument , retArr , funcParam ) ;
204- retArr . push ( ";" ) ;
205- }
218+ retArr . push ( "return " ) ;
219+ ast_generic ( ast . argument , retArr , funcParam ) ;
220+ retArr . push ( ";" ) ;
206221
207222 //throw ast_errorOutput(
208223 // "Non main function return, is not supported : "+funcParam.currentFunctionNamespace,
@@ -309,15 +324,79 @@ var functionNode_webgl = (function() {
309324 ast , funcParam
310325 ) ;
311326 }
312- retArr . push ( "for (float " ) ;
313- ast_generic ( forNode . init , retArr , funcParam ) ;
314- retArr . push ( ";" ) ;
315- ast_generic ( forNode . test , retArr , funcParam ) ;
316- retArr . push ( ";" ) ;
317- ast_generic ( forNode . update , retArr , funcParam ) ;
318- retArr . push ( ")" ) ;
319- ast_generic ( forNode . body , retArr , funcParam ) ;
327+
328+ if ( forNode . test && forNode . test . type == "BinaryExpression" ) {
329+ console . log ( forNode ) ;
330+ if ( forNode . test . right . type == "Identifier" ) {
331+ retArr . push ( "for (float " ) ;
332+ ast_generic ( forNode . init , retArr , funcParam ) ;
333+ retArr . push ( ";" ) ;
334+ ast_generic ( forNode . test . left , retArr , funcParam ) ;
335+ retArr . push ( forNode . test . operator ) ;
336+ retArr . push ( "LOOP_MAX" ) ;
337+ retArr . push ( ";" ) ;
338+ ast_generic ( forNode . update , retArr , funcParam ) ;
339+ retArr . push ( ")" ) ;
340+
341+ retArr . push ( "{\n" ) ;
342+ retArr . push ( "if (" ) ;
343+ ast_generic ( forNode . test . left , retArr , funcParam ) ;
344+ retArr . push ( forNode . test . operator ) ;
345+ ast_generic ( forNode . test . right , retArr , funcParam ) ;
346+ retArr . push ( ") {\n" ) ;
347+ for ( var i = 0 ; i < forNode . body . body . length ; i ++ ) {
348+ ast_generic ( forNode . body . body [ i ] , retArr , funcParam ) ;
349+ }
350+ retArr . push ( "} else {\n" ) ;
351+ retArr . push ( "break;\n" ) ;
352+ retArr . push ( "}\n" ) ;
353+ retArr . push ( "}\n" ) ;
354+
355+ return retArr ;
356+ } else {
357+ retArr . push ( "for (float " ) ;
358+ ast_generic ( forNode . init , retArr , funcParam ) ;
359+ retArr . push ( ";" ) ;
360+ ast_generic ( forNode . test , retArr , funcParam ) ;
361+ retArr . push ( ";" ) ;
362+ ast_generic ( forNode . update , retArr , funcParam ) ;
363+ retArr . push ( ")" ) ;
364+ ast_generic ( forNode . body , retArr , funcParam ) ;
365+ return retArr ;
366+ }
367+ }
368+
369+ throw ast_errorOutput (
370+ "Invalid for statment" ,
371+ ast , funcParam
372+ ) ;
373+ }
374+
375+ /// Prases the abstract syntax tree, genericially to its respective function
376+ ///
377+ /// @param ast the AST object to parse
378+ ///
379+ /// @returns the prased openclgl string
380+ function ast_WhileStatement ( whileNode , retArr , funcParam ) {
381+ throw ast_errorOutput (
382+ "While statements are not allowed" ,
383+ ast , funcParam
384+ ) ;
385+
386+ /*
387+ if (whileNode.type != "WhileStatement") {
388+ throw ast_errorOutput(
389+ "Invalid while statment",
390+ ast, funcParam
391+ );
392+ }
393+ retArr.push("while (");
394+ ast_generic(whileNode.test, retArr, funcParam);
395+ retArr.push(") {\n");
396+ ast_generic(whileNode.body, retArr, funcParam);
397+ retArr.push("}\n");
320398 return retArr;
399+ */
321400 }
322401
323402 function ast_AssignmentExpression ( assNode , retArr , funcParam ) {
0 commit comments