Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80677 views
1
// Generated by CoffeeScript 1.9.3
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
6
exports.Scope = Scope = (function() {
7
function Scope(parent, expressions, method, referencedVars) {
8
var ref, ref1;
9
this.parent = parent;
10
this.expressions = expressions;
11
this.method = method;
12
this.referencedVars = referencedVars;
13
this.variables = [
14
{
15
name: 'arguments',
16
type: 'arguments'
17
}
18
];
19
this.positions = {};
20
if (!this.parent) {
21
this.utilities = {};
22
}
23
this.root = (ref = (ref1 = this.parent) != null ? ref1.root : void 0) != null ? ref : this;
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 ref;
42
if (((ref = this.method) != null ? ref.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 ref;
65
return !!(this.type(name) || ((ref = this.parent) != null ? ref.check(name) : void 0));
66
};
67
68
Scope.prototype.temporary = function(name, index, single) {
69
if (single == null) {
70
single = false;
71
}
72
if (single) {
73
return (index + parseInt(name, 36)).toString(36).replace(/\d/g, 'a');
74
} else {
75
return name + (index || '');
76
}
77
};
78
79
Scope.prototype.type = function(name) {
80
var i, len, ref, v;
81
ref = this.variables;
82
for (i = 0, len = ref.length; i < len; i++) {
83
v = ref[i];
84
if (v.name === name) {
85
return v.type;
86
}
87
}
88
return null;
89
};
90
91
Scope.prototype.freeVariable = function(name, options) {
92
var index, ref, temp;
93
if (options == null) {
94
options = {};
95
}
96
index = 0;
97
while (true) {
98
temp = this.temporary(name, index, options.single);
99
if (!(this.check(temp) || indexOf.call(this.root.referencedVars, temp) >= 0)) {
100
break;
101
}
102
index++;
103
}
104
if ((ref = options.reserve) != null ? ref : true) {
105
this.add(temp, 'var', true);
106
}
107
return temp;
108
};
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
118
Scope.prototype.hasDeclarations = function() {
119
return !!this.declaredVariables().length;
120
};
121
122
Scope.prototype.declaredVariables = function() {
123
var v;
124
return ((function() {
125
var i, len, ref, results;
126
ref = this.variables;
127
results = [];
128
for (i = 0, len = ref.length; i < len; i++) {
129
v = ref[i];
130
if (v.type === 'var') {
131
results.push(v.name);
132
}
133
}
134
return results;
135
}).call(this)).sort();
136
};
137
138
Scope.prototype.assignedVariables = function() {
139
var i, len, ref, results, v;
140
ref = this.variables;
141
results = [];
142
for (i = 0, len = ref.length; i < len; i++) {
143
v = ref[i];
144
if (v.type.assigned) {
145
results.push(v.name + " = " + v.type.value);
146
}
147
}
148
return results;
149
};
150
151
return Scope;
152
153
})();
154
155
}).call(this);
156
157