forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple.js
265 lines (233 loc) · 6.97 KB
/
tuple.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @constructor
* @param {Array.<Object>|Object} L
*/
Sk.builtin.tuple = function (L) {
var it, i;
if (!(this instanceof Sk.builtin.tuple)) {
return new Sk.builtin.tuple(L);
}
if (L === undefined) {
L = [];
}
if (Object.prototype.toString.apply(L) === "[object Array]") {
this.v = L;
}
else {
if (L.tp$iter) {
this.v = [];
for (it = L.tp$iter(), i = it.tp$iternext(); i !== undefined; i = it.tp$iternext()) {
this.v.push(i);
}
}
else {
throw new Sk.builtin.ValueError("expecting Array or iterable");
}
}
this.__class__ = Sk.builtin.tuple;
this["v"] = this.v;
return this;
};
Sk.builtin.tuple.prototype.tp$name = "tuple";
Sk.builtin.tuple.prototype["$r"] = function () {
var ret;
var i;
var bits;
if (this.v.length === 0) {
return new Sk.builtin.str("()");
}
bits = [];
for (i = 0; i < this.v.length; ++i) {
bits[i] = Sk.misceval.objectRepr(this.v[i]).v;
}
ret = bits.join(", ");
if (this.v.length === 1) {
ret += ",";
}
return new Sk.builtin.str("(" + ret + ")");
};
Sk.builtin.tuple.prototype.mp$subscript = function (index) {
var ret;
var i;
if (Sk.misceval.isIndex(index)) {
i = Sk.misceval.asIndex(index);
if (i !== undefined) {
if (i < 0) {
i = this.v.length + i;
}
if (i < 0 || i >= this.v.length) {
throw new Sk.builtin.IndexError("tuple index out of range");
}
return this.v[i];
}
}
else if (index instanceof Sk.builtin.slice) {
ret = [];
index.sssiter$(this, function (i, wrt) {
ret.push(wrt.v[i]);
});
return new Sk.builtin.tuple(ret);
}
throw new Sk.builtin.TypeError("tuple indices must be integers, not " + Sk.abstr.typeName(index));
};
// todo; the numbers and order are taken from python, but the answer's
// obviously not the same because there's no int wrapping. shouldn't matter,
// but would be nice to make the hash() values the same if it's not too
// expensive to simplify tests.
Sk.builtin.tuple.prototype.tp$hash = function () {
var y;
var i;
var mult = 1000003;
var x = 0x345678;
var len = this.v.length;
for (i = 0; i < len; ++i) {
y = Sk.builtin.hash(this.v[i]).v;
if (y === -1) {
return new Sk.builtin.nmber(-1, Sk.builtin.nmber.int$);
}
x = (x ^ y) * mult;
mult += 82520 + len + len;
}
x += 97531;
if (x === -1) {
x = -2;
}
return new Sk.builtin.nmber(x | 0, Sk.builtin.nmber.int$);
};
Sk.builtin.tuple.prototype.sq$repeat = function (n) {
var j;
var i;
var ret;
n = Sk.builtin.asnum$(n);
ret = [];
for (i = 0; i < n; ++i) {
for (j = 0; j < this.v.length; ++j) {
ret.push(this.v[j]);
}
}
return new Sk.builtin.tuple(ret);
};
Sk.builtin.tuple.prototype.nb$multiply = Sk.builtin.tuple.prototype.sq$repeat;
Sk.builtin.tuple.prototype.nb$inplace_multiply = Sk.builtin.tuple.prototype.sq$repeat;
Sk.builtin.tuple.prototype.ob$type = Sk.builtin.type.makeIntoTypeObj("tuple", Sk.builtin.tuple);
Sk.builtin.tuple.prototype.tp$iter = function () {
var ret =
{
tp$iter : function () {
return ret;
},
$obj : this,
$index : 0,
tp$iternext: function () {
// todo; StopIteration
if (ret.$index >= ret.$obj.v.length) {
return undefined;
}
return ret.$obj.v[ret.$index++];
}
};
return ret;
};
Sk.builtin.tuple.prototype["__iter__"] = new Sk.builtin.func(function (self) {
Sk.builtin.pyCheckArgs("__iter__", arguments, 1, 1);
return self.tp$iter();
});
Sk.builtin.tuple.prototype.tp$getattr = Sk.builtin.object.prototype.GenericGetAttr;
Sk.builtin.tuple.prototype.tp$richcompare = function (w, op) {
//print(" tup rc", JSON.stringify(this.v), JSON.stringify(w), op);
// w not a tuple
var k;
var i;
var wl;
var vl;
var v;
if (!w.__class__ || w.__class__ != Sk.builtin.tuple) {
// shortcuts for eq/not
if (op === "Eq") {
return false;
}
if (op === "NotEq") {
return true;
}
// todo; other types should have an arbitrary order
return false;
}
v = this.v;
w = w.v;
vl = v.length;
wl = w.length;
for (i = 0; i < vl && i < wl; ++i) {
k = Sk.misceval.richCompareBool(v[i], w[i], "Eq");
if (!k) {
break;
}
}
if (i >= vl || i >= wl) {
// no more items to compare, compare sizes
switch (op) {
case "Lt":
return vl < wl;
case "LtE":
return vl <= wl;
case "Eq":
return vl === wl;
case "NotEq":
return vl !== wl;
case "Gt":
return vl > wl;
case "GtE":
return vl >= wl;
default:
goog.asserts.fail();
}
}
// we have an item that's different
// shortcuts for eq/not
if (op === "Eq") {
return false;
}
if (op === "NotEq") {
return true;
}
// or, compare the differing element using the proper operator
//print(" tup rcb end", i, v[i] instanceof Sk.builtin.str, JSON.stringify(v[i]), w[i] instanceof Sk.builtin.str, JSON.stringify(w[i]), op);
return Sk.misceval.richCompareBool(v[i], w[i], op);
};
Sk.builtin.tuple.prototype.sq$concat = function (other) {
var msg;
if (other.__class__ != Sk.builtin.tuple) {
msg = "can only concatenate tuple (not \"";
msg += Sk.abstr.typeName(other) + "\") to tuple";
throw new Sk.builtin.TypeError(msg);
}
return new Sk.builtin.tuple(this.v.concat(other.v));
};
Sk.builtin.tuple.prototype.nb$add = Sk.builtin.tuple.prototype.sq$concat;
Sk.builtin.tuple.prototype.nb$inplace_add = Sk.builtin.tuple.prototype.sq$concat;
Sk.builtin.tuple.prototype.sq$length = function () {
return this.v.length;
};
Sk.builtin.tuple.prototype["index"] = new Sk.builtin.func(function (self, item) {
var i;
var len = self.v.length;
var obj = self.v;
for (i = 0; i < len; ++i) {
if (Sk.misceval.richCompareBool(obj[i], item, "Eq")) {
return Sk.builtin.assk$(i, Sk.builtin.nmber.int$);
}
}
throw new Sk.builtin.ValueError("tuple.index(x): x not in tuple");
});
Sk.builtin.tuple.prototype["count"] = new Sk.builtin.func(function (self, item) {
var i;
var len = self.v.length;
var obj = self.v;
var count = 0;
for (i = 0; i < len; ++i) {
if (Sk.misceval.richCompareBool(obj[i], item, "Eq")) {
count += 1;
}
}
return new Sk.builtin.nmber(count, Sk.builtin.nmber.int$);
});
goog.exportSymbol("Sk.builtin.tuple", Sk.builtin.tuple);