scope.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Generated by CoffeeScript 1.12.6
  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, type) {
  45. if (type == null) {
  46. type = 'var';
  47. }
  48. if (this.check(name)) {
  49. return true;
  50. }
  51. this.add(name, type);
  52. return false;
  53. };
  54. Scope.prototype.parameter = function(name) {
  55. if (this.shared && this.parent.check(name, true)) {
  56. return;
  57. }
  58. return this.add(name, 'param');
  59. };
  60. Scope.prototype.check = function(name) {
  61. var ref;
  62. return !!(this.type(name) || ((ref = this.parent) != null ? ref.check(name) : void 0));
  63. };
  64. Scope.prototype.temporary = function(name, index, single) {
  65. var diff, endCode, letter, newCode, num, startCode;
  66. if (single == null) {
  67. single = false;
  68. }
  69. if (single) {
  70. startCode = name.charCodeAt(0);
  71. endCode = 'z'.charCodeAt(0);
  72. diff = endCode - startCode;
  73. newCode = startCode + index % (diff + 1);
  74. letter = String.fromCharCode(newCode);
  75. num = Math.floor(index / (diff + 1));
  76. return "" + letter + (num || '');
  77. } else {
  78. return "" + name + (index || '');
  79. }
  80. };
  81. Scope.prototype.type = function(name) {
  82. var i, len, ref, v;
  83. ref = this.variables;
  84. for (i = 0, len = ref.length; i < len; i++) {
  85. v = ref[i];
  86. if (v.name === name) {
  87. return v.type;
  88. }
  89. }
  90. return null;
  91. };
  92. Scope.prototype.freeVariable = function(name, options) {
  93. var index, ref, temp;
  94. if (options == null) {
  95. options = {};
  96. }
  97. index = 0;
  98. while (true) {
  99. temp = this.temporary(name, index, options.single);
  100. if (!(this.check(temp) || indexOf.call(this.root.referencedVars, temp) >= 0)) {
  101. break;
  102. }
  103. index++;
  104. }
  105. if ((ref = options.reserve) != null ? ref : true) {
  106. this.add(temp, 'var', true);
  107. }
  108. return temp;
  109. };
  110. Scope.prototype.assign = function(name, value) {
  111. this.add(name, {
  112. value: value,
  113. assigned: true
  114. }, true);
  115. return this.hasAssignments = true;
  116. };
  117. Scope.prototype.hasDeclarations = function() {
  118. return !!this.declaredVariables().length;
  119. };
  120. Scope.prototype.declaredVariables = function() {
  121. var v;
  122. return ((function() {
  123. var i, len, ref, results;
  124. ref = this.variables;
  125. results = [];
  126. for (i = 0, len = ref.length; i < len; i++) {
  127. v = ref[i];
  128. if (v.type === 'var') {
  129. results.push(v.name);
  130. }
  131. }
  132. return results;
  133. }).call(this)).sort();
  134. };
  135. Scope.prototype.assignedVariables = function() {
  136. var i, len, ref, results, v;
  137. ref = this.variables;
  138. results = [];
  139. for (i = 0, len = ref.length; i < len; i++) {
  140. v = ref[i];
  141. if (v.type.assigned) {
  142. results.push(v.name + " = " + v.type.value);
  143. }
  144. }
  145. return results;
  146. };
  147. return Scope;
  148. })();
  149. }).call(this);