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