2010-07-24 15:31:43 +00:00
|
|
|
(function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
var Scope, _ref, extend, helpers;
|
2010-03-30 20:48:43 +00:00
|
|
|
var __hasProp = Object.prototype.hasOwnProperty;
|
2010-09-21 01:56:18 +00:00
|
|
|
if (typeof process !== "undefined" && process !== null) {
|
|
|
|
helpers = require('./helpers').helpers;
|
|
|
|
} else {
|
2010-02-18 04:22:05 +00:00
|
|
|
this.exports = this;
|
2010-02-13 20:25:04 +00:00
|
|
|
}
|
2010-09-21 01:56:18 +00:00
|
|
|
_ref = helpers;
|
|
|
|
extend = _ref.extend;
|
2010-02-28 00:46:45 +00:00
|
|
|
exports.Scope = (function() {
|
2010-05-15 03:40:04 +00:00
|
|
|
Scope = function(parent, expressions, method) {
|
2010-09-21 01:56:18 +00:00
|
|
|
var _ref2;
|
|
|
|
_ref2 = [parent, expressions, method];
|
|
|
|
this.parent = _ref2[0];
|
|
|
|
this.expressions = _ref2[1];
|
|
|
|
this.method = _ref2[2];
|
2010-02-28 00:46:45 +00:00
|
|
|
this.variables = {};
|
2010-03-30 23:21:14 +00:00
|
|
|
if (this.parent) {
|
2010-09-19 16:34:27 +00:00
|
|
|
this.garbage = this.parent.garbage;
|
2010-03-30 23:21:14 +00:00
|
|
|
} else {
|
2010-09-19 16:34:27 +00:00
|
|
|
this.garbage = [];
|
2010-03-30 23:21:14 +00:00
|
|
|
Scope.root = this;
|
|
|
|
}
|
2010-02-28 00:46:45 +00:00
|
|
|
return this;
|
2010-02-09 02:10:48 +00:00
|
|
|
};
|
2010-03-30 23:21:14 +00:00
|
|
|
Scope.root = null;
|
2010-09-19 16:34:27 +00:00
|
|
|
Scope.prototype.startLevel = function() {
|
|
|
|
return this.garbage.push([]);
|
|
|
|
};
|
|
|
|
Scope.prototype.endLevel = function() {
|
2010-09-21 02:13:08 +00:00
|
|
|
var _i, _len, _ref2, _result, name;
|
|
|
|
_result = []; _ref2 = this.garbage.pop();
|
|
|
|
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
|
|
|
|
name = _ref2[_i];
|
2010-09-19 16:34:27 +00:00
|
|
|
if (this.variables[name] === 'var') {
|
|
|
|
_result.push(this.variables[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-09-21 01:56:18 +00:00
|
|
|
var _ref2, k, v;
|
|
|
|
_ref2 = this.variables;
|
|
|
|
for (v in _ref2) {
|
|
|
|
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) {
|
|
|
|
var immediate;
|
|
|
|
immediate = Object.prototype.hasOwnProperty.call(this.variables, name);
|
|
|
|
if (immediate || (options && options.immediate)) {
|
|
|
|
return immediate;
|
2010-02-28 00:46:45 +00:00
|
|
|
}
|
|
|
|
return !!(this.parent && this.parent.check(name));
|
|
|
|
};
|
2010-09-19 13:18:01 +00:00
|
|
|
Scope.prototype.temporary = function(type, index) {
|
2010-09-21 01:56:18 +00:00
|
|
|
return '_' + type + (index > 1 ? index : '');
|
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-09-21 02:13:08 +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-09-19 16:34:27 +00:00
|
|
|
if (this.garbage.length) {
|
|
|
|
this.garbage[this.garbage.length - 1].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-09-19 14:47:26 +00:00
|
|
|
return val === 'var' || val === 'reuse';
|
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-09-21 01:56:18 +00:00
|
|
|
var _ref2, _result, key, val;
|
2010-02-28 00:46:45 +00:00
|
|
|
return (function() {
|
2010-09-21 01:56:18 +00:00
|
|
|
_result = []; _ref2 = this.variables;
|
|
|
|
for (key in _ref2) {
|
|
|
|
if (!__hasProp.call(_ref2, key)) continue;
|
|
|
|
val = _ref2[key];
|
2010-09-19 14:47:26 +00:00
|
|
|
if (val === 'var' || val === 'reuse') {
|
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-09-21 01:56:18 +00:00
|
|
|
var _ref2, _result, key, val;
|
|
|
|
_result = []; _ref2 = this.variables;
|
|
|
|
for (key in _ref2) {
|
|
|
|
if (!__hasProp.call(_ref2, key)) continue;
|
|
|
|
val = _ref2[key];
|
2010-08-11 04:40:15 +00:00
|
|
|
if (val.assigned) {
|
2010-09-19 12:29:15 +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-02-24 23:56:32 +00:00
|
|
|
})();
|