forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.js
494 lines (456 loc) · 10.2 KB
/
stream.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
'use strict';
var List = require("./list.js");
var Curry = require("./curry.js");
var Caml_bytes = require("./caml_bytes.js");
var Caml_option = require("./caml_option.js");
var Caml_string = require("./caml_string.js");
var Caml_exceptions = require("./caml_exceptions.js");
var CamlinternalLazy = require("./camlinternalLazy.js");
var Failure = /* @__PURE__ */Caml_exceptions.create("Stream.Failure");
var $$Error = /* @__PURE__ */Caml_exceptions.create("Stream.Error");
function count(param) {
if (param !== undefined) {
return param.count;
} else {
return 0;
}
}
function data(param) {
if (param !== undefined) {
return param.data;
} else {
return "Sempty";
}
}
function get_data(count, _d) {
while(true) {
var d = _d;
if (typeof d !== "object") {
return d;
}
switch (d.TAG) {
case "Scons" :
return d;
case "Sapp" :
var d2 = d._1;
var match = get_data(count, d._0);
if (typeof match !== "object") {
_d = d2;
continue ;
}
if (match.TAG === "Scons") {
return {
TAG: "Scons",
_0: match._0,
_1: {
TAG: "Sapp",
_0: match._1,
_1: d2
}
};
}
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"stream.ml",
48,
12
],
Error: new Error()
};
case "Slazy" :
_d = CamlinternalLazy.force(d._0);
continue ;
case "Sgen" :
var g = d._0;
var match$1 = g.curr;
if (match$1 !== undefined) {
var a = Caml_option.valFromOption(match$1);
if (a !== undefined) {
g.curr = undefined;
return {
TAG: "Scons",
_0: Caml_option.valFromOption(a),
_1: d
};
} else {
return "Sempty";
}
}
var a$1 = Curry._1(g.func, count);
if (a$1 !== undefined) {
return {
TAG: "Scons",
_0: Caml_option.valFromOption(a$1),
_1: d
};
} else {
g.curr = Caml_option.some(undefined);
return "Sempty";
}
}
};
}
function peek_data(s) {
while(true) {
var f = s.data;
if (typeof f !== "object") {
return ;
}
switch (f.TAG) {
case "Scons" :
return Caml_option.some(f._0);
case "Sapp" :
var d = get_data(s.count, s.data);
if (typeof d !== "object") {
return ;
}
if (d.TAG === "Scons") {
s.data = d;
return Caml_option.some(d._0);
}
throw {
RE_EXN_ID: "Assert_failure",
_1: [
"stream.ml",
71,
12
],
Error: new Error()
};
case "Slazy" :
s.data = CamlinternalLazy.force(f._0);
continue ;
case "Sgen" :
var g = f._0;
var a = g.curr;
if (a !== undefined) {
return Caml_option.valFromOption(a);
}
var x = Curry._1(g.func, s.count);
g.curr = Caml_option.some(x);
return x;
}
};
}
function peek(s) {
if (s !== undefined) {
return peek_data(s);
}
}
function junk_data(s) {
while(true) {
var g = s.data;
if (typeof g === "object") {
switch (g.TAG) {
case "Scons" :
s.count = s.count + 1 | 0;
s.data = g._1;
return ;
case "Sgen" :
var g$1 = g._0;
var match = g$1.curr;
if (match !== undefined) {
s.count = s.count + 1 | 0;
g$1.curr = undefined;
return ;
}
break;
default:
}
}
var match$1 = peek_data(s);
if (match$1 === undefined) {
return ;
}
continue ;
};
}
function junk(data) {
if (data !== undefined) {
return junk_data(data);
}
}
function nget_data(n, s) {
if (n <= 0) {
return [
/* [] */0,
s.data,
0
];
}
var a = peek_data(s);
if (a === undefined) {
return [
/* [] */0,
s.data,
0
];
}
var a$1 = Caml_option.valFromOption(a);
junk_data(s);
var match = nget_data(n - 1 | 0, s);
return [
{
hd: a$1,
tl: match[0]
},
{
TAG: "Scons",
_0: a$1,
_1: match[1]
},
match[2] + 1 | 0
];
}
function npeek(n, d) {
if (d !== undefined) {
var match = nget_data(n, d);
d.count = d.count - match[2] | 0;
d.data = match[1];
return match[0];
} else {
return /* [] */0;
}
}
function next(s) {
var a = peek(s);
if (a !== undefined) {
junk(s);
return Caml_option.valFromOption(a);
}
throw {
RE_EXN_ID: Failure,
Error: new Error()
};
}
function empty(s) {
var match = peek(s);
if (match === undefined) {
return ;
}
throw {
RE_EXN_ID: Failure,
Error: new Error()
};
}
function iter(f, strm) {
var _param;
while(true) {
var a = peek(strm);
if (a === undefined) {
return ;
}
junk(strm);
Curry._1(f, Caml_option.valFromOption(a));
_param = undefined;
continue ;
};
}
function from(f) {
return {
count: 0,
data: {
TAG: "Sgen",
_0: {
curr: undefined,
func: f
}
}
};
}
function of_list(l) {
return {
count: 0,
data: List.fold_right((function (x, l) {
return {
TAG: "Scons",
_0: x,
_1: l
};
}), l, "Sempty")
};
}
function of_string(s) {
var count = {
contents: 0
};
return from(function (param) {
var c = count.contents;
if (c < s.length) {
count.contents = count.contents + 1 | 0;
return Caml_string.get(s, c);
}
});
}
function of_bytes(s) {
var count = {
contents: 0
};
return from(function (param) {
var c = count.contents;
if (c < s.length) {
count.contents = count.contents + 1 | 0;
return Caml_bytes.get(s, c);
}
});
}
function iapp(i, s) {
return {
count: 0,
data: {
TAG: "Sapp",
_0: data(i),
_1: data(s)
}
};
}
function icons(i, s) {
return {
count: 0,
data: {
TAG: "Scons",
_0: i,
_1: data(s)
}
};
}
function ising(i) {
return {
count: 0,
data: {
TAG: "Scons",
_0: i,
_1: "Sempty"
}
};
}
function lapp(f, s) {
return {
count: 0,
data: {
TAG: "Slazy",
_0: {
LAZY_DONE: false,
VAL: (function () {
return {
TAG: "Sapp",
_0: data(Curry._1(f, undefined)),
_1: data(s)
};
})
}
}
};
}
function lcons(f, s) {
return {
count: 0,
data: {
TAG: "Slazy",
_0: {
LAZY_DONE: false,
VAL: (function () {
return {
TAG: "Scons",
_0: Curry._1(f, undefined),
_1: data(s)
};
})
}
}
};
}
function lsing(f) {
return {
count: 0,
data: {
TAG: "Slazy",
_0: {
LAZY_DONE: false,
VAL: (function () {
return {
TAG: "Scons",
_0: Curry._1(f, undefined),
_1: "Sempty"
};
})
}
}
};
}
function slazy(f) {
return {
count: 0,
data: {
TAG: "Slazy",
_0: {
LAZY_DONE: false,
VAL: (function () {
return data(Curry._1(f, undefined));
})
}
}
};
}
function dump_data(f, param) {
if (typeof param !== "object") {
console.log("Sempty");
return ;
}
switch (param.TAG) {
case "Scons" :
console.log("Scons (");
Curry._1(f, param._0);
console.log(", ");
dump_data(f, param._1);
console.log(")");
return ;
case "Sapp" :
console.log("Sapp (");
dump_data(f, param._0);
console.log(", ");
dump_data(f, param._1);
console.log(")");
return ;
case "Slazy" :
console.log("Slazy");
return ;
case "Sgen" :
console.log("Sgen");
return ;
}
}
function dump(f, s) {
console.log("{count = ");
var i = count(s);
console.log(String(i));
console.log("; data = ");
dump_data(f, data(s));
console.log("}");
console.log("");
}
var sempty;
exports.Failure = Failure;
exports.$$Error = $$Error;
exports.from = from;
exports.of_list = of_list;
exports.of_string = of_string;
exports.of_bytes = of_bytes;
exports.iter = iter;
exports.next = next;
exports.empty = empty;
exports.peek = peek;
exports.junk = junk;
exports.count = count;
exports.npeek = npeek;
exports.iapp = iapp;
exports.icons = icons;
exports.ising = ising;
exports.lapp = lapp;
exports.lcons = lcons;
exports.lsing = lsing;
exports.sempty = sempty;
exports.slazy = slazy;
exports.dump = dump;
/* No side effect */