MCPcopy
hub / github.com/gpujs/gpu.js / astReturnStatement

Method astReturnStatement

src/backend/web-gl/function-node.js:131–207  ·  view source on GitHub ↗

* @desc Parses the abstract syntax tree for to *return* statement * @param {Object} ast - the AST object to parse * @param {Array} retArr - return array string * @returns {Array} the append retArr

(ast, retArr)

Source from the content-addressed store, hash-verified

129 * @returns {Array} the append retArr
130 */
131 astReturnStatement(ast, retArr) {
132 if (!ast.argument) throw this.astErrorOutput('Unexpected return statement', ast);
133 this.pushState('skip-literal-correction');
134 const type = this.getType(ast.argument);
135 this.popState('skip-literal-correction');
136
137 const result = [];
138
139 if (!this.returnType) {
140 if (type === 'LiteralInteger' || type === 'Integer') {
141 this.returnType = 'Number';
142 } else {
143 this.returnType = type;
144 }
145 }
146
147 switch (this.returnType) {
148 case 'LiteralInteger':
149 case 'Number':
150 case 'Float':
151 switch (type) {
152 case 'Integer':
153 result.push('float(');
154 this.astGeneric(ast.argument, result);
155 result.push(')');
156 break;
157 case 'LiteralInteger':
158 this.castLiteralToFloat(ast.argument, result);
159
160 // Running astGeneric forces the LiteralInteger to pick a type, and here, if we are returning a float, yet
161 // the LiteralInteger has picked to be an integer because of constraints on it we cast it to float.
162 if (this.getType(ast) === 'Integer') {
163 result.unshift('float(');
164 result.push(')');
165 }
166 break;
167 default:
168 this.astGeneric(ast.argument, result);
169 }
170 break;
171 case 'Integer':
172 switch (type) {
173 case 'Float':
174 case 'Number':
175 this.castValueToInteger(ast.argument, result);
176 break;
177 case 'LiteralInteger':
178 this.castLiteralToInteger(ast.argument, result);
179 break;
180 default:
181 this.astGeneric(ast.argument, result);
182 }
183 break;
184 case 'Array(4)':
185 case 'Array(3)':
186 case 'Array(2)':
187 case 'Matrix(2)':
188 case 'Matrix(3)':

Callers

nothing calls this directly

Calls 8

castLiteralToFloatMethod · 0.95
castValueToIntegerMethod · 0.95
castLiteralToIntegerMethod · 0.95
astErrorOutputMethod · 0.80
getTypeMethod · 0.80
astGenericMethod · 0.80
pushStateMethod · 0.45
popStateMethod · 0.45

Tested by

no test coverage detected