scope.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Generated by CoffeeScript 1.10.0
  2. (function() {
  3. var Scope,
  4. 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; };
  5. exports.Scope = Scope = (function() {
  6. function Scope(parent, expressions, method, referencedVars) {
  7. var ref, ref1;
  8. this.parent = parent;
  9. this.expressions = expressions;
  10. this.method = method;
  11. this.referencedVars = referencedVars;
  12. this.variables = [
  13. {
  14. name: 'arguments',
  15. type: 'arguments'
  16. }
  17. ];
  18. this.positions = {};
  19. if (!this.parent) {
  20. this.utilities = {};
  21. }
  22. this.root = (ref = (ref1 = this.parent) != null ? ref1.root : void 0) != null ? ref : this;
  23. }
  24. Scope.prototype.add = function(name, type, immediate) {
  25. if (this.shared && !immediate) {
  26. return this.parent.add(name, type, immediate);
  27. }
  28. if (Object.prototype.hasOwnProperty.call(this.positions, name)) {
  29. return this.variables[this.positions[name]].type = type;
  30. } else {
  31. return this.positions[name] = this.variables.push({
  32. name: name,
  33. type: type
  34. }) - 1;
  35. }
  36. };
  37. Scope.prototype.namedMethod = function() {
  38. var ref;
  39. if (((ref = this.method) != null ? ref.name : void 0) || !this.parent) {
  40. return this.method;
  41. }
  42. return this.parent.namedMethod();
  43. };
  44. Scope.prototype.find = function(name) {
  45. if (this.check(name)) {
  46. return true;
  47. }
  48. this.add(name, 'var');
  49. return false;
  50. };
  51. Scope.prototype.parameter = function(name) {
  52. if (this.shared && this.parent.check(name, true)) {
  53. return;
  54. }
  55. return this.add(name, 'param');
  56. };
  57. Scope.prototype.check = function(name) {
  58. var ref;
  59. return !!(this.type(name) || ((ref = this.parent) != null ? ref.check(name) : void 0));
  60. };
  61. Scope.prototype.temporary = function(name, index, single) {
  62. if (single == null) {
  63. single = false;
  64. }
  65. if (single) {
  66. return (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
  67. } else {
  68. return name + (index || '');
  69. }
  70. };
  71. Scope.prototype.type = function(name) {
  72. var i, len, ref, v;
  73. ref = this.variables;
  74. for (i = 0, len = ref.length; i < len; i++) {
  75. v = ref[i];
  76. if (v.name === name) {
  77. return v.type;
  78. }
  79. }
  80. return null;
  81. };
  82. Scope.prototype.freeVariable = function(name, options) {
  83. var index, ref, temp;
  84. if (options == null) {
  85. options = {};
  86. }
  87. index = 0;
  88. while (true) {
  89. temp = this.temporary(name, index, options.single);
  90. if (!(this.check(temp) || indexOf.call(this.root.referencedVars, temp) >= 0)) {
  91. break;
  92. }
  93. index++;
  94. }
  95. if ((ref = options.reserve) != null ? ref : true) {
  96. this.add(temp, 'var', true);
  97. }
  98. return temp;
  99. };
  100. Scope.prototype.assign = function(name, value) {
  101. this.add(name, {
  102. value: value,
  103. assigned: true
  104. }, true);
  105. return this.hasAssignments = true;
  106. };
  107. Scope.prototype.hasDeclarations = function() {
  108. return !!this.declaredVariables().length;
  109. };
  110. Scope.prototype.declaredVariables = function() {
  111. var v;
  112. return ((function() {
  113. var i, len, ref, results;
  114. ref = this.variables;
  115. results = [];
  116. for (i = 0, len = ref.length; i < len; i++) {
  117. v = ref[i];
  118. if (v.type === 'var') {
  119. results.push(v.name);
  120. }
  121. }
  122. return results;
  123. }).call(this)).sort();
  124. };
  125. Scope.prototype.assignedVariables = function() {
  126. var i, len, ref, results, v;
  127. ref = this.variables;
  128. results = [];
  129. for (i = 0, len = ref.length; i < len; i++) {
  130. v = ref[i];
  131. if (v.type.assigned) {
  132. results.push(v.name + " = " + v.type.value);
  133. }
  134. }
  135. return results;
  136. };
  137. return Scope;
  138. })();
  139. }).call(this);