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