* @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)
| 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) { |
nothing calls this directly
no test coverage detected