2012-04-10 21:26:23 +00:00
|
|
|
// Generated by CoffeeScript 1.3.1
|
2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2010-11-02 03:58:03 +00:00
|
|
|
var Scope, extend, last, _ref;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-10-01 23:21:34 +00:00
|
|
|
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-23 18:50:52 +00:00
|
|
|
exports.Scope = Scope = (function() {
|
2011-12-24 11:49:21 +00:00
|
|
|
|
2011-12-19 01:00:09 +00:00
|
|
|
Scope.name = 'Scope';
|
2011-12-24 11:46:09 +00:00
|
|
|
|
2010-12-06 06:01:57 +00:00
|
|
|
Scope.root = null;
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-11-21 01:39:35 +00:00
|
|
|
function Scope(parent, expressions, method) {
|
|
|
|
this.parent = parent;
|
|
|
|
this.expressions = expressions;
|
|
|
|
this.method = method;
|
2010-11-12 02:48:08 +00:00
|
|
|
this.variables = [
|
|
|
|
{
|
|
|
|
name: 'arguments',
|
|
|
|
type: 'arguments'
|
2010-10-05 03:21:16 +00:00
|
|
|
}
|
2010-11-12 02:48:08 +00:00
|
|
|
];
|
|
|
|
this.positions = {};
|
2012-04-10 18:57:45 +00:00
|
|
|
if (!this.parent) {
|
|
|
|
Scope.root = this;
|
|
|
|
}
|
2010-11-12 02:48:08 +00:00
|
|
|
}
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-14 02:24:32 +00:00
|
|
|
Scope.prototype.add = function(name, type, immediate) {
|
2012-04-10 18:57:45 +00:00
|
|
|
if (this.shared && !immediate) {
|
|
|
|
return this.parent.add(name, type, immediate);
|
|
|
|
}
|
2011-11-14 16:18:45 +00:00
|
|
|
if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
|
2011-11-10 08:08:38 +00:00
|
|
|
return this.variables[this.positions[name]].type = type;
|
2010-11-09 04:07:51 +00:00
|
|
|
} else {
|
|
|
|
return this.positions[name] = this.variables.push({
|
|
|
|
name: name,
|
|
|
|
type: type
|
|
|
|
}) - 1;
|
|
|
|
}
|
2010-10-19 23:51:34 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-08-25 02:19:53 +00:00
|
|
|
Scope.prototype.find = function(name, options) {
|
2012-04-10 18:57:45 +00:00
|
|
|
if (this.check(name, options)) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-21 03:09:06 +00:00
|
|
|
this.add(name, 'var');
|
2010-03-30 13:02:51 +00:00
|
|
|
return false;
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope.prototype.parameter = function(name) {
|
2012-04-10 18:57:45 +00:00
|
|
|
if (this.shared && this.parent.check(name, true)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-10-21 03:09:06 +00:00
|
|
|
return this.add(name, 'param');
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-11-02 04:31:42 +00:00
|
|
|
Scope.prototype.check = function(name, immediate) {
|
2012-03-01 04:46:03 +00:00
|
|
|
var found, _ref1;
|
2010-11-02 04:31:42 +00:00
|
|
|
found = !!this.type(name);
|
2012-04-10 18:57:45 +00:00
|
|
|
if (found || immediate) {
|
|
|
|
return found;
|
|
|
|
}
|
2012-03-01 04:46:03 +00:00
|
|
|
return !!((_ref1 = this.parent) != null ? _ref1.check(name) : void 0);
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-10-20 08:13:43 +00:00
|
|
|
Scope.prototype.temporary = function(name, index) {
|
2010-11-28 17:28:46 +00:00
|
|
|
if (name.length > 1) {
|
2012-03-01 04:46:03 +00:00
|
|
|
return '_' + name + (index > 1 ? index - 1 : '');
|
2010-11-28 17:28:46 +00:00
|
|
|
} else {
|
|
|
|
return '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
|
|
|
|
}
|
2010-09-19 13:18:01 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-10-19 23:36:50 +00:00
|
|
|
Scope.prototype.type = function(name) {
|
2012-03-01 04:46:03 +00:00
|
|
|
var v, _i, _len, _ref1;
|
|
|
|
_ref1 = this.variables;
|
|
|
|
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
|
|
|
v = _ref1[_i];
|
2012-04-10 18:57:45 +00:00
|
|
|
if (v.name === name) {
|
|
|
|
return v.type;
|
|
|
|
}
|
2010-10-19 23:36:50 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2011-12-14 17:38:19 +00:00
|
|
|
Scope.prototype.freeVariable = function(name, reserve) {
|
2010-11-28 02:04:40 +00:00
|
|
|
var index, temp;
|
2012-04-10 18:57:45 +00:00
|
|
|
if (reserve == null) {
|
|
|
|
reserve = true;
|
|
|
|
}
|
2010-09-19 14:03:45 +00:00
|
|
|
index = 0;
|
2011-12-14 17:38:19 +00:00
|
|
|
while (this.check((temp = this.temporary(name, index)))) {
|
2010-09-19 14:03:45 +00:00
|
|
|
index++;
|
2010-02-28 00:46:45 +00:00
|
|
|
}
|
2012-04-10 18:57:45 +00:00
|
|
|
if (reserve) {
|
|
|
|
this.add(temp, 'var', true);
|
|
|
|
}
|
2010-09-19 12:50:17 +00:00
|
|
|
return temp;
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope.prototype.assign = function(name, value) {
|
2010-12-07 04:32:32 +00:00
|
|
|
this.add(name, {
|
2010-10-19 23:51:34 +00:00
|
|
|
value: value,
|
|
|
|
assigned: true
|
2011-11-10 08:08:38 +00:00
|
|
|
}, true);
|
2010-12-07 04:32:32 +00:00
|
|
|
return this.hasAssignments = true;
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-12-12 17:16:27 +00:00
|
|
|
Scope.prototype.hasDeclarations = function() {
|
|
|
|
return !!this.declaredVariables().length;
|
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.declaredVariables = function() {
|
2012-03-01 04:46:03 +00:00
|
|
|
var realVars, tempVars, v, _i, _len, _ref1;
|
2010-12-07 04:32:32 +00:00
|
|
|
realVars = [];
|
|
|
|
tempVars = [];
|
2012-03-01 04:46:03 +00:00
|
|
|
_ref1 = this.variables;
|
|
|
|
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
|
|
|
v = _ref1[_i];
|
2011-08-08 16:55:22 +00:00
|
|
|
if (v.type === 'var') {
|
|
|
|
(v.name.charAt(0) === '_' ? tempVars : realVars).push(v.name);
|
|
|
|
}
|
2010-11-02 03:58:03 +00:00
|
|
|
}
|
2010-12-07 04:32:32 +00:00
|
|
|
return realVars.sort().concat(tempVars.sort());
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-06-12 23:05:13 +00:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2012-03-01 04:46:03 +00:00
|
|
|
var v, _i, _len, _ref1, _results;
|
|
|
|
_ref1 = this.variables;
|
2010-11-09 05:17:08 +00:00
|
|
|
_results = [];
|
2012-03-01 04:46:03 +00:00
|
|
|
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
|
|
|
v = _ref1[_i];
|
2012-04-10 18:57:45 +00:00
|
|
|
if (v.type.assigned) {
|
|
|
|
_results.push("" + v.name + " = " + v.type.value);
|
|
|
|
}
|
2010-07-17 02:31:36 +00:00
|
|
|
}
|
2010-11-09 05:17:08 +00:00
|
|
|
return _results;
|
2010-02-28 00:46:45 +00:00
|
|
|
};
|
2011-09-18 22:16:39 +00:00
|
|
|
|
2010-02-28 00:46:45 +00:00
|
|
|
return Scope;
|
2011-12-14 15:39:20 +00:00
|
|
|
|
2010-12-23 18:50:52 +00:00
|
|
|
})();
|
2011-12-14 15:39:20 +00:00
|
|
|
|
2010-09-21 07:53:58 +00:00
|
|
|
}).call(this);
|