js-slang
Version:
Javascript-based implementations of Source, written in Typescript
80 lines • 3.72 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.cloneAndStripImportSpecifier = exports.createInvokedFunctionResultVariableDeclaration = exports.createImportedNameDeclaration = exports.createListCallExpression = exports.createPairCallExpression = void 0;
const localImport_prelude_1 = require("../../../stdlib/localImport.prelude");
const create = require("../../../utils/ast/astCreator");
/**
* Constructs a call to the `pair` function.
*
* @param head The head of the pair.
* @param tail The tail of the pair.
*/
const createPairCallExpression = (head, tail) => create.callExpression(create.identifier('pair'), [head, tail]);
exports.createPairCallExpression = createPairCallExpression;
/**
* Constructs a call to the `list` function.
*
* @param listElements The elements of the list.
*/
const createListCallExpression = (listElements) => create.callExpression(create.identifier('list'), listElements);
exports.createListCallExpression = createListCallExpression;
/**
* Constructs the AST equivalent of:
* const importedName = __access_export__(functionName, lookupName);
*
* @param functionName The name of the transformed function declaration to import from.
* @param importedName The name of the import.
* @param lookupName The name to lookup in the transformed function declaration.
*/
const createImportedNameDeclaration = (functionName, importedName, lookupName) => {
const callExpression = create.callExpression(create.identifier(localImport_prelude_1.accessExportFunctionName), [
create.identifier(functionName),
create.literal(lookupName)
]);
return create.constantDeclaration(importedName.name, callExpression);
};
exports.createImportedNameDeclaration = createImportedNameDeclaration;
/**
* Constructs the AST equivalent of:
* const variableName = functionName(...functionArgs);
*
* @param functionName The name of the transformed function declaration to invoke.
* @param variableName The name of the variable holding the result of the function invocation.
* @param functionArgs The arguments to be passed when invoking the function.
*/
const createInvokedFunctionResultVariableDeclaration = (functionName, variableName, functionArgs) => {
const callExpression = create.callExpression(create.identifier(functionName), functionArgs);
return create.constantDeclaration(variableName, callExpression);
};
exports.createInvokedFunctionResultVariableDeclaration = createInvokedFunctionResultVariableDeclaration;
/**
* Clones the import specifier, but only the properties
* that are part of its ESTree AST type. This is useful for
* stripping out extraneous information on the import
* specifier AST nodes (such as the location information
* that the Acorn parser adds).
*
* @param importSpecifier The import specifier to be cloned.
*/
const cloneAndStripImportSpecifier = (importSpecifier) => {
switch (importSpecifier.type) {
case 'ImportSpecifier':
return {
type: 'ImportSpecifier',
local: create.identifier(importSpecifier.local.name),
imported: create.identifier(importSpecifier.imported.name)
};
case 'ImportDefaultSpecifier':
return {
type: 'ImportDefaultSpecifier',
local: create.identifier(importSpecifier.local.name)
};
case 'ImportNamespaceSpecifier':
return {
type: 'ImportNamespaceSpecifier',
local: create.identifier(importSpecifier.local.name)
};
}
};
exports.cloneAndStripImportSpecifier = cloneAndStripImportSpecifier;
//# sourceMappingURL=contextSpecificConstructors.js.map
;