2010-07-24 11:31:43 -04:00
|
|
|
(function() {
|
2010-11-01 23:58:03 -04:00
|
|
|
var Scope, extend, last, _ref;
|
2010-10-01 19:21:34 -04:00
|
|
|
_ref = require('./helpers'), extend = _ref.extend, last = _ref.last;
|
2010-02-27 19:46:45 -05:00
|
|
|
exports.Scope = (function() {
|
2010-10-04 23:21:16 -04:00
|
|
|
Scope = (function() {
|
2010-10-11 12:13:01 -04:00
|
|
|
function Scope(_arg, _arg2, _arg3) {
|
2010-10-04 23:21:16 -04:00
|
|
|
this.parent = _arg;
|
2010-10-26 05:52:43 -04:00
|
|
|
this.expressions = _arg2;
|
|
|
|
this.method = _arg3;
|
2010-10-21 00:07:21 -04:00
|
|
|
this.variables = [
|
|
|
|
{
|
|
|
|
name: 'arguments',
|
|
|
|
type: 'arguments'
|
|
|
|
}
|
|
|
|
];
|
2010-10-24 21:42:37 -04:00
|
|
|
this.positions = {};
|
2010-10-06 21:19:05 -04:00
|
|
|
if (this.parent) {
|
|
|
|
this.garbage = this.parent.garbage;
|
|
|
|
} else {
|
|
|
|
this.garbage = [];
|
2010-10-04 23:21:16 -04:00
|
|
|
Scope.root = this;
|
|
|
|
}
|
|
|
|
return this;
|
2010-10-24 20:51:55 -04:00
|
|
|
}
|
2010-10-11 12:13:01 -04:00
|
|
|
return Scope;
|
2010-10-04 23:21:16 -04:00
|
|
|
})();
|
2010-03-30 19:21:14 -04:00
|
|
|
Scope.root = null;
|
2010-10-20 23:09:06 -04:00
|
|
|
Scope.prototype.add = function(name, type) {
|
2010-10-24 21:42:37 -04:00
|
|
|
var pos;
|
|
|
|
return typeof (pos = this.positions[name]) === 'number' ? this.variables[pos].type = type : this.positions[name] = this.variables.push({
|
2010-10-24 21:23:32 -04:00
|
|
|
name: name,
|
|
|
|
type: type
|
2010-10-24 21:42:37 -04:00
|
|
|
}) - 1;
|
2010-10-19 19:51:34 -04:00
|
|
|
};
|
2010-10-06 21:19:05 -04:00
|
|
|
Scope.prototype.startLevel = function() {
|
2010-10-20 04:13:43 -04:00
|
|
|
this.garbage.push([]);
|
|
|
|
return this;
|
2010-10-06 21:19:05 -04:00
|
|
|
};
|
|
|
|
Scope.prototype.endLevel = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var name, _i, _len, _ref;
|
|
|
|
_ref = this.garbage.pop();
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
name = _ref[_i];
|
2010-10-20 04:13:43 -04:00
|
|
|
if (this.type(name) === 'var') {
|
2010-10-20 23:09:06 -04:00
|
|
|
this.add(name, 'reuse');
|
2010-10-06 21:19:05 -04:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 04:13:43 -04:00
|
|
|
return this;
|
2010-10-06 21:19:05 -04:00
|
|
|
};
|
2010-08-24 22:19:53 -04:00
|
|
|
Scope.prototype.find = function(name, options) {
|
|
|
|
if (this.check(name, options)) {
|
2010-02-27 19:46:45 -05:00
|
|
|
return true;
|
|
|
|
}
|
2010-10-20 23:09:06 -04:00
|
|
|
this.add(name, 'var');
|
2010-02-27 19:46:45 -05:00
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.any = function(fn) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var v, _i, _len, _ref;
|
|
|
|
_ref = this.variables;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
v = _ref[_i];
|
2010-10-20 04:13:43 -04:00
|
|
|
if (fn(v)) {
|
2010-03-30 09:02:51 -04:00
|
|
|
return true;
|
|
|
|
}
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-03-30 09:02:51 -04:00
|
|
|
return false;
|
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.parameter = function(name) {
|
2010-10-20 23:09:06 -04:00
|
|
|
return this.add(name, 'param');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-11-02 00:31:42 -04:00
|
|
|
Scope.prototype.check = function(name, immediate) {
|
|
|
|
var found, _ref;
|
|
|
|
found = !!this.type(name);
|
|
|
|
if (found || immediate) {
|
|
|
|
return found;
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
2010-11-02 00:31:42 -04:00
|
|
|
return !!((_ref = this.parent) != null ? _ref.check(name) : void 0);
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-10-20 04:13:43 -04:00
|
|
|
Scope.prototype.temporary = function(name, index) {
|
|
|
|
return name.length > 1 ? '_' + name + (index > 1 ? index : '') : '_' + (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
|
2010-09-19 09:18:01 -04:00
|
|
|
};
|
2010-10-19 19:36:50 -04:00
|
|
|
Scope.prototype.type = function(name) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var v, _i, _len, _ref;
|
|
|
|
_ref = this.variables;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
v = _ref[_i];
|
2010-10-19 19:36:50 -04:00
|
|
|
if (v.name === name) {
|
|
|
|
return v.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
2010-09-19 07:55:19 -04:00
|
|
|
Scope.prototype.freeVariable = function(type) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var index, temp, _ref;
|
2010-09-19 10:03:45 -04:00
|
|
|
index = 0;
|
2010-11-02 00:31:42 -04:00
|
|
|
while (this.check((temp = this.temporary(type, index)), true) && this.type(temp) !== 'reuse') {
|
2010-09-19 10:03:45 -04:00
|
|
|
index++;
|
2010-02-27 19:46:45 -05:00
|
|
|
}
|
2010-10-20 23:09:06 -04:00
|
|
|
this.add(temp, 'var');
|
2010-11-02 00:31:42 -04:00
|
|
|
if ((_ref = last(this.garbage)) != null) {
|
|
|
|
_ref.push(temp);
|
2010-10-23 05:06:04 -04:00
|
|
|
}
|
2010-09-19 08:50:17 -04:00
|
|
|
return temp;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-05-14 23:40:04 -04:00
|
|
|
Scope.prototype.assign = function(name, value) {
|
2010-10-20 23:09:06 -04:00
|
|
|
return this.add(name, {
|
2010-10-19 19:51:34 -04:00
|
|
|
value: value,
|
|
|
|
assigned: true
|
2010-10-19 19:36:50 -04:00
|
|
|
});
|
2010-10-19 19:02:38 -04:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.hasDeclarations = function(body) {
|
2010-10-20 04:13:43 -04:00
|
|
|
return body === this.expressions && this.any(function(v) {
|
2010-11-02 00:31:42 -04:00
|
|
|
var _ref;
|
|
|
|
return (_ref = v.type) === 'var' || _ref === 'reuse';
|
2010-03-30 09:02:51 -04:00
|
|
|
});
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.hasAssignments = function(body) {
|
2010-10-20 04:13:43 -04:00
|
|
|
return body === this.expressions && this.any(function(v) {
|
|
|
|
return v.type.assigned;
|
2010-03-30 19:42:09 -04:00
|
|
|
});
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.declaredVariables = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var tmp, usr, v, _i, _len, _ref, _ref2;
|
2010-11-01 23:58:03 -04:00
|
|
|
usr = [];
|
|
|
|
tmp = [];
|
2010-11-02 00:31:42 -04:00
|
|
|
_ref = this.variables;
|
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
v = _ref[_i];
|
|
|
|
if ((_ref2 = v.type) === 'var' || _ref2 === 'reuse') {
|
|
|
|
(v.name.charAt(0) === '_' ? tmp : usr).push(v.name);
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-11-01 23:58:03 -04:00
|
|
|
}
|
|
|
|
return usr.sort().concat(tmp.sort());
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.assignedVariables = function() {
|
2010-11-02 00:31:42 -04:00
|
|
|
var v, _i, _len, _ref, _result;
|
|
|
|
_ref = this.variables;
|
2010-10-06 23:24:32 -04:00
|
|
|
_result = [];
|
2010-11-02 00:31:42 -04:00
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
|
|
|
v = _ref[_i];
|
2010-10-19 19:36:50 -04:00
|
|
|
if (v.type.assigned) {
|
2010-10-21 19:50:36 -04:00
|
|
|
_result.push("" + v.name + " = " + v.type.value);
|
2010-08-14 11:42:19 -04:00
|
|
|
}
|
2010-07-16 22:31:36 -04:00
|
|
|
}
|
2010-09-19 08:29:15 -04:00
|
|
|
return _result;
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.compiledDeclarations = function() {
|
|
|
|
return this.declaredVariables().join(', ');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
2010-06-12 19:05:13 -04:00
|
|
|
Scope.prototype.compiledAssignments = function() {
|
|
|
|
return this.assignedVariables().join(', ');
|
2010-02-27 19:46:45 -05:00
|
|
|
};
|
|
|
|
return Scope;
|
|
|
|
}).call(this);
|
2010-09-21 03:53:58 -04:00
|
|
|
}).call(this);
|