1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/lib/coffee-script/scope.js

156 lines
4 KiB
JavaScript
Raw Normal View History

2015-09-03 12:37:36 -04:00
// Generated by CoffeeScript 1.10.0
(function() {
var Scope,
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
2010-12-23 13:50:52 -05:00
exports.Scope = Scope = (function() {
function Scope(parent, expressions, method, referencedVars) {
var ref, ref1;
this.parent = parent;
this.expressions = expressions;
this.method = method;
this.referencedVars = referencedVars;
this.variables = [
{
name: 'arguments',
type: 'arguments'
2010-10-04 23:21:16 -04:00
}
];
this.positions = {};
2012-04-10 14:57:45 -04:00
if (!this.parent) {
this.utilities = {};
2012-04-10 14:57:45 -04:00
}
this.root = (ref = (ref1 = this.parent) != null ? ref1.root : void 0) != null ? ref : this;
}
Scope.prototype.add = function(name, type, immediate) {
2012-04-10 14:57:45 -04:00
if (this.shared && !immediate) {
return this.parent.add(name, type, immediate);
}
if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
return this.variables[this.positions[name]].type = type;
} else {
return this.positions[name] = this.variables.push({
name: name,
type: type
}) - 1;
}
};
Scope.prototype.namedMethod = function() {
var ref;
if (((ref = this.method) != null ? ref.name : void 0) || !this.parent) {
2012-04-11 19:32:40 -04:00
return this.method;
}
return this.parent.namedMethod();
};
Scope.prototype.find = function(name) {
if (this.check(name)) {
2012-04-10 14:57:45 -04:00
return true;
}
2010-10-20 23:09:06 -04:00
this.add(name, 'var');
return false;
};
Scope.prototype.parameter = function(name) {
2012-04-10 14:57:45 -04:00
if (this.shared && this.parent.check(name, true)) {
return;
}
2010-10-20 23:09:06 -04:00
return this.add(name, 'param');
};
Scope.prototype.check = function(name) {
var ref;
return !!(this.type(name) || ((ref = this.parent) != null ? ref.check(name) : void 0));
};
Scope.prototype.temporary = function(name, index, single) {
if (single == null) {
single = false;
}
if (single) {
return (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
} else {
return name + (index || '');
}
};
Scope.prototype.type = function(name) {
var i, len, ref, v;
ref = this.variables;
for (i = 0, len = ref.length; i < len; i++) {
v = ref[i];
2012-04-10 14:57:45 -04:00
if (v.name === name) {
return v.type;
}
}
return null;
};
Scope.prototype.freeVariable = function(name, options) {
var index, ref, temp;
if (options == null) {
options = {};
2012-04-10 14:57:45 -04:00
}
index = 0;
while (true) {
temp = this.temporary(name, index, options.single);
if (!(this.check(temp) || indexOf.call(this.root.referencedVars, temp) >= 0)) {
break;
}
index++;
}
if ((ref = options.reserve) != null ? ref : true) {
2012-04-10 14:57:45 -04:00
this.add(temp, 'var', true);
}
return temp;
};
Scope.prototype.assign = function(name, value) {
2010-12-06 23:32:32 -05:00
this.add(name, {
value: value,
assigned: true
}, true);
2010-12-06 23:32:32 -05:00
return this.hasAssignments = true;
};
Scope.prototype.hasDeclarations = function() {
return !!this.declaredVariables().length;
};
Scope.prototype.declaredVariables = function() {
var v;
return ((function() {
var i, len, ref, results;
ref = this.variables;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
v = ref[i];
if (v.type === 'var') {
results.push(v.name);
}
}
return results;
}).call(this)).sort();
};
Scope.prototype.assignedVariables = function() {
var i, len, ref, results, v;
ref = this.variables;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
v = ref[i];
2012-04-10 14:57:45 -04:00
if (v.type.assigned) {
results.push(v.name + " = " + v.type.value);
2012-04-10 14:57:45 -04:00
}
}
return results;
};
return Scope;
2011-12-14 10:39:20 -05:00
2010-12-23 13:50:52 -05:00
})();
2011-12-14 10:39:20 -05:00
}).call(this);