MCPcopy Index your code
hub / github.com/gpujs/gpu.js / astForStatement

Method astForStatement

src/backend/cpu/function-node.js:155–220  ·  view source on GitHub ↗

* @desc Parses the abstract syntax tree for *for-loop* expression * @param {Object} forNode - An ast Node * @param {Array} retArr - return array string * @returns {Array} the parsed webgl string

(forNode, retArr)

Source from the content-addressed store, hash-verified

153 * @returns {Array} the parsed webgl string
154 */
155 astForStatement(forNode, retArr) {
156 if (forNode.type !== 'ForStatement') {
157 throw this.astErrorOutput('Invalid for statement', forNode);
158 }
159
160 const initArr = [];
161 const testArr = [];
162 const updateArr = [];
163 const bodyArr = [];
164 let isSafe = null;
165
166 if (forNode.init) {
167 this.pushState('in-for-loop-init');
168 this.astGeneric(forNode.init, initArr);
169 for (let i = 0; i < initArr.length; i++) {
170 if (initArr[i].includes && initArr[i].includes(',')) {
171 isSafe = false;
172 }
173 }
174 this.popState('in-for-loop-init');
175 } else {
176 isSafe = false;
177 }
178
179 if (forNode.test) {
180 this.astGeneric(forNode.test, testArr);
181 } else {
182 isSafe = false;
183 }
184
185 if (forNode.update) {
186 this.astGeneric(forNode.update, updateArr);
187 } else {
188 isSafe = false;
189 }
190
191 if (forNode.body) {
192 this.pushState('loop-body');
193 this.astGeneric(forNode.body, bodyArr);
194 this.popState('loop-body');
195 }
196
197 // have all parts, now make them safe
198 if (isSafe === null) {
199 isSafe = this.isSafe(forNode.init) && this.isSafe(forNode.test);
200 }
201
202 if (isSafe) {
203 retArr.push(`for (${initArr.join('')};${testArr.join('')};${updateArr.join('')}){\n`);
204 retArr.push(bodyArr.join(''));
205 retArr.push('}\n');
206 } else {
207 const iVariableName = this.getInternalVariableName('safeI');
208 if (initArr.length > 0) {
209 retArr.push(initArr.join(''), ';\n');
210 }
211 retArr.push(`for (let ${iVariableName}=0;${iVariableName}<LOOP_MAX;${iVariableName}++){\n`);
212 if (testArr.length > 0) {

Callers

nothing calls this directly

Calls 6

astErrorOutputMethod · 0.80
astGenericMethod · 0.80
isSafeMethod · 0.80
pushStateMethod · 0.45
popStateMethod · 0.45

Tested by

no test coverage detected