You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.6 KiB
JavaScript
109 lines
2.6 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = function createScopeTracker() {
|
|
let scopeLevel = 0;
|
|
let lastFunctionScopeLevel = 0;
|
|
let scopes = [];
|
|
let references = [];
|
|
|
|
let scopeId = 0;
|
|
let declarationId = 0;
|
|
|
|
function createDeclaration(type, name) {
|
|
return {
|
|
id: declarationId++,
|
|
type: type,
|
|
name: name
|
|
};
|
|
}
|
|
|
|
function createScope(type) {
|
|
return {
|
|
_declarations: [],
|
|
_declarationNames: new Set(),
|
|
id: scopeId++,
|
|
type: type,
|
|
addDeclaration: function addDeclaration(type, name) {
|
|
this._declarations.push(createDeclaration(type, name));
|
|
this._declarationNames.add(name);
|
|
},
|
|
hasDeclaration: function hasDeclaration(name) {
|
|
return this._declarationNames.has(name);
|
|
}
|
|
};
|
|
}
|
|
|
|
return {
|
|
_openScope: function _openScope(type) {
|
|
scopeLevel++;
|
|
scopes[scopeLevel] = createScope(type);
|
|
references[scopeLevel] = [];
|
|
},
|
|
openBlockScope: function openBlockScope() {
|
|
this._openScope("block");
|
|
},
|
|
openFunctionScope: function openFunctionScope() {
|
|
this._openScope("function");
|
|
lastFunctionScopeLevel = scopeLevel;
|
|
},
|
|
markDeclaration: function (node) {
|
|
if (node.kind === "var") {
|
|
scopes[lastFunctionScopeLevel].addDeclaration(node.kind, name);
|
|
} else if (node.kind === "let" || node.kind === "const") {
|
|
scopes[scopeLevel].addDeclaration(node.kind, name);
|
|
}
|
|
},
|
|
markReference: function markReference(node) {
|
|
/* FIXME: Temporal dead zone! */
|
|
references[scopeLevel].push(node);
|
|
},
|
|
_closeScope: function _closeScope(type) {
|
|
if (scopes[scopeLevel].type === type) {
|
|
let scope = scopes[scopeLevel];
|
|
|
|
/* MARKER: reconcile references */
|
|
let remainingReferences = [];
|
|
let satisfiedReferences = [];
|
|
|
|
for (let reference of references) {
|
|
if (scope.hasDeclaration(reference.name)) {
|
|
satisfiedReferences.push(reference);
|
|
} else {
|
|
remainingReferences.push(reference);
|
|
}
|
|
}
|
|
|
|
references = remainingReferences;
|
|
|
|
scopeLevel--;
|
|
|
|
if (lastFunctionScopeLevel > scopeLevel) {
|
|
/* Our last-known function scope level is no longer valid, so we need to recalculate it. */
|
|
for (let i = scopeLevel; i >= 0; i--) {
|
|
if (scopes[i].type === "function") {
|
|
lastFunctionScopeLevel = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Should this ever happen? */
|
|
lastFunctionScopeLevel = 0;
|
|
}
|
|
|
|
return {
|
|
scope: scope,
|
|
references: references
|
|
};
|
|
} else {
|
|
throw new Error(`Bug! Expected ${type} scope, but found ${scopes[scopeLevel].type} scope`);
|
|
}
|
|
},
|
|
closeBlockScope: function closeBlockScope() {
|
|
return this._closeScope("block");
|
|
},
|
|
closeFunctionScope: function closeFunctionScope() {
|
|
return this._closeScope("function");
|
|
}
|
|
};
|
|
};
|