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

Method getType

src/backend/function-node.js:357–572  ·  view source on GitHub ↗

* Recursively looks up type for ast expression until it's found * @param ast * @returns {String|null}

(ast)

Source from the content-addressed store, hash-verified

355 * @returns {String|null}
356 */
357 getType(ast) {
358 if (Array.isArray(ast)) {
359 return this.getType(ast[ast.length - 1]);
360 }
361 switch (ast.type) {
362 case 'BlockStatement':
363 return this.getType(ast.body);
364 case 'ArrayExpression':
365 const childType = this.getType(ast.elements[0]);
366 switch (childType) {
367 case 'Array(2)':
368 case 'Array(3)':
369 case 'Array(4)':
370 return `Matrix(${ast.elements.length})`;
371 }
372 return `Array(${ ast.elements.length })`;
373 case 'Literal':
374 const literalKey = this.astKey(ast);
375 if (this.literalTypes[literalKey]) {
376 return this.literalTypes[literalKey];
377 }
378 if (Number.isInteger(ast.value)) {
379 return 'LiteralInteger';
380 } else if (ast.value === true || ast.value === false) {
381 return 'Boolean';
382 } else {
383 return 'Number';
384 }
385 case 'AssignmentExpression':
386 return this.getType(ast.left);
387 case 'CallExpression':
388 if (this.isAstMathFunction(ast)) {
389 return 'Number';
390 }
391 if (!ast.callee || !ast.callee.name) {
392 if (ast.callee.type === 'SequenceExpression' && ast.callee.expressions[ast.callee.expressions.length - 1].property.name) {
393 const functionName = ast.callee.expressions[ast.callee.expressions.length - 1].property.name;
394 this.inferArgumentTypesIfNeeded(functionName, ast.arguments);
395 return this.lookupReturnType(functionName, ast, this);
396 }
397 if (this.getVariableSignature(ast.callee, true) === 'this.color') {
398 return null;
399 }
400 if (ast.callee.type === 'MemberExpression' && ast.callee.object && ast.callee.property && ast.callee.property.name && ast.arguments) {
401 const functionName = ast.callee.property.name;
402 this.inferArgumentTypesIfNeeded(functionName, ast.arguments);
403 return this.lookupReturnType(functionName, ast, this);
404 }
405 throw this.astErrorOutput('Unknown call expression', ast);
406 }
407 if (ast.callee && ast.callee.name) {
408 const functionName = ast.callee.name;
409 this.inferArgumentTypesIfNeeded(functionName, ast.arguments);
410 return this.lookupReturnType(functionName, ast, this);
411 }
412 throw this.astErrorOutput(`Unhandled getType Type "${ ast.type }"`, ast);
413 case 'LogicalExpression':
414 return 'Boolean';

Callers 15

traceFunctionASTMethod · 0.95
runFunction · 0.80
lookupReturnTypeMethod · 0.80
getKernelResultTypeMethod · 0.80
getReturnTypesMethod · 0.80
astFunctionMethod · 0.80
astReturnStatementMethod · 0.80
astBinaryExpressionMethod · 0.80

Calls 15

astKeyMethod · 0.95
isAstMathFunctionMethod · 0.95
getVariableSignatureMethod · 0.95
astErrorOutputMethod · 0.95
isStateMethod · 0.95
getDeclarationMethod · 0.95
isAstVariableMethod · 0.95
getCheckVariableTypeMethod · 0.95
findIdentifierOriginMethod · 0.95
getLookupTypeMethod · 0.95
getConstantTypeMethod · 0.95

Tested by

no test coverage detected