UNPKG

bs-platform

Version:

bucklescript compiler, ocaml standard libary by bucklescript and its required runtime support

508 lines (468 loc) 13.2 kB
'use strict'; var List = require("./list.js"); var Block = require("./block.js"); var Curry = require("./curry.js"); var Caml_obj = require("./caml_obj.js"); var Caml_bytes = require("./caml_bytes.js"); var Pervasives = require("./pervasives.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 Caml_builtin_exceptions = require("./caml_builtin_exceptions.js"); var Failure = Caml_exceptions.create("Stream.Failure"); var $$Error = Caml_exceptions.create("Stream.Error"); function count(param) { if (param !== undefined) { return param[/* count */0]; } else { return 0; } } function data(param) { if (param !== undefined) { return param[/* data */1]; } else { return /* Sempty */0; } } function fill_buff(b) { b[/* len */2] = Pervasives.input(b[/* ic */0], b[/* buff */1], 0, b[/* buff */1].length); b[/* ind */3] = 0; return /* () */0; } function get_data(count, _d) { while(true) { var d = _d; if (typeof d === "number") { return d; } else { switch (d.tag | 0) { case /* Scons */0 : return d; case /* Sapp */1 : var d2 = d[1]; var match = get_data(count, d[0]); if (typeof match === "number") { _d = d2; continue ; } else if (match.tag) { throw [ Caml_builtin_exceptions.assert_failure, /* tuple */[ "stream.ml", 53, 12 ] ]; } else { return /* Scons */Block.__(0, [ match[0], /* Sapp */Block.__(1, [ match[1], d2 ]) ]); } case /* Slazy */2 : _d = CamlinternalLazy.force(d[0]); continue ; case /* Sgen */3 : var g = d[0]; var match$1 = g[/* curr */0]; if (match$1 !== undefined) { var match$2 = Caml_option.valFromOption(match$1); if (match$2 !== undefined) { g[/* curr */0] = undefined; return /* Scons */Block.__(0, [ Caml_option.valFromOption(match$2), d ]); } else { return /* Sempty */0; } } else { var match$3 = Curry._1(g[/* func */1], count); if (match$3 !== undefined) { return /* Scons */Block.__(0, [ Caml_option.valFromOption(match$3), d ]); } else { g[/* curr */0] = Caml_option.some(undefined); return /* Sempty */0; } } case /* Sbuffio */4 : var b = d[0]; if (b[/* ind */3] >= b[/* len */2]) { fill_buff(b); } if (b[/* len */2] === 0) { return /* Sempty */0; } else { var r = b[/* buff */1][b[/* ind */3]]; b[/* ind */3] = b[/* ind */3] + 1 | 0; return /* Scons */Block.__(0, [ r, d ]); } } } }; } function peek_data(s) { while(true) { var match = s[/* data */1]; if (typeof match === "number") { return ; } else { switch (match.tag | 0) { case /* Scons */0 : return Caml_option.some(match[0]); case /* Sapp */1 : var d = get_data(s[/* count */0], s[/* data */1]); if (typeof d === "number") { return ; } else if (d.tag) { throw [ Caml_builtin_exceptions.assert_failure, /* tuple */[ "stream.ml", 82, 12 ] ]; } else { s[/* data */1] = d; return Caml_option.some(d[0]); } case /* Slazy */2 : s[/* data */1] = CamlinternalLazy.force(match[0]); continue ; case /* Sgen */3 : var g = match[0]; var match$1 = g[/* curr */0]; if (match$1 !== undefined) { return Caml_option.valFromOption(match$1); } else { var x = Curry._1(g[/* func */1], s[/* count */0]); g[/* curr */0] = Caml_option.some(x); return x; } case /* Sbuffio */4 : var b = match[0]; if (b[/* ind */3] >= b[/* len */2]) { fill_buff(b); } if (b[/* len */2] === 0) { s[/* data */1] = /* Sempty */0; return ; } else { return b[/* buff */1][b[/* ind */3]]; } } } }; } function peek(param) { if (param !== undefined) { return peek_data(param); } } function junk_data(s) { while(true) { var match = s[/* data */1]; if (typeof match !== "number") { switch (match.tag | 0) { case /* Scons */0 : s[/* count */0] = s[/* count */0] + 1 | 0; s[/* data */1] = match[1]; return /* () */0; case /* Sgen */3 : var g = match[0]; var match$1 = g[/* curr */0]; if (match$1 !== undefined) { s[/* count */0] = s[/* count */0] + 1 | 0; g[/* curr */0] = undefined; return /* () */0; } break; case /* Sbuffio */4 : var b = match[0]; s[/* count */0] = s[/* count */0] + 1 | 0; b[/* ind */3] = b[/* ind */3] + 1 | 0; return /* () */0; default: } } var match$2 = peek_data(s); if (match$2 !== undefined) { continue ; } else { return /* () */0; } }; } function junk(param) { if (param !== undefined) { return junk_data(param); } else { return /* () */0; } } function nget_data(n, s) { if (n <= 0) { return /* tuple */[ /* [] */0, s[/* data */1], 0 ]; } else { var match = peek_data(s); if (match !== undefined) { var a = Caml_option.valFromOption(match); junk_data(s); var match$1 = nget_data(n - 1 | 0, s); return /* tuple */[ /* :: */[ a, match$1[0] ], /* Scons */Block.__(0, [ a, match$1[1] ]), match$1[2] + 1 | 0 ]; } else { return /* tuple */[ /* [] */0, s[/* data */1], 0 ]; } } } function npeek(n, param) { if (param !== undefined) { var n$1 = n; var s = param; var match = nget_data(n$1, s); s[/* count */0] = s[/* count */0] - match[2] | 0; s[/* data */1] = match[1]; return match[0]; } else { return /* [] */0; } } function next(s) { var match = peek(s); if (match !== undefined) { junk(s); return Caml_option.valFromOption(match); } else { throw Failure; } } function empty(s) { var match = peek(s); if (match !== undefined) { throw Failure; } else { return /* () */0; } } function iter(f, strm) { var _param = /* () */0; while(true) { var match = peek(strm); if (match !== undefined) { junk(strm); Curry._1(f, Caml_option.valFromOption(match)); _param = /* () */0; continue ; } else { return /* () */0; } }; } function from(f) { return /* record */[ /* count */0, /* data : Sgen */Block.__(3, [/* record */[ /* curr */undefined, /* func */f ]]) ]; } function of_list(l) { return /* record */[ /* count */0, /* data */List.fold_right((function (x, l) { return /* Scons */Block.__(0, [ x, l ]); }), l, /* Sempty */0) ]; } function of_string(s) { var count = /* record */[/* contents */0]; return from((function (param) { var c = count[0]; if (c < s.length) { count[0] = count[0] + 1 | 0; return Caml_string.get(s, c); } })); } function of_bytes(s) { var count = /* record */[/* contents */0]; return from((function (param) { var c = count[0]; if (c < s.length) { count[0] = count[0] + 1 | 0; return Caml_bytes.get(s, c); } })); } function of_channel(ic) { return /* record */[ /* count */0, /* data : Sbuffio */Block.__(4, [/* record */[ /* ic */ic, /* buff */Caml_bytes.caml_create_bytes(4096), /* len */0, /* ind */0 ]]) ]; } function iapp(i, s) { return /* record */[ /* count */0, /* data : Sapp */Block.__(1, [ data(i), data(s) ]) ]; } function icons(i, s) { return /* record */[ /* count */0, /* data : Scons */Block.__(0, [ i, data(s) ]) ]; } function ising(i) { return /* record */[ /* count */0, /* data : Scons */Block.__(0, [ i, /* Sempty */0 ]) ]; } function lapp(f, s) { return /* record */[ /* count */0, /* data : Slazy */Block.__(2, [Caml_obj.caml_lazy_make((function (param) { return /* Sapp */Block.__(1, [ data(Curry._1(f, /* () */0)), data(s) ]); }))]) ]; } function lcons(f, s) { return /* record */[ /* count */0, /* data : Slazy */Block.__(2, [Caml_obj.caml_lazy_make((function (param) { return /* Scons */Block.__(0, [ Curry._1(f, /* () */0), data(s) ]); }))]) ]; } function lsing(f) { return /* record */[ /* count */0, /* data : Slazy */Block.__(2, [Caml_obj.caml_lazy_make((function (param) { return /* Scons */Block.__(0, [ Curry._1(f, /* () */0), /* Sempty */0 ]); }))]) ]; } function slazy(f) { return /* record */[ /* count */0, /* data : Slazy */Block.__(2, [Caml_obj.caml_lazy_make((function (param) { return data(Curry._1(f, /* () */0)); }))]) ]; } function dump_data(f, param) { if (typeof param === "number") { return Pervasives.print_string("Sempty"); } else { switch (param.tag | 0) { case /* Scons */0 : Pervasives.print_string("Scons ("); Curry._1(f, param[0]); Pervasives.print_string(", "); dump_data(f, param[1]); return Pervasives.print_string(")"); case /* Sapp */1 : Pervasives.print_string("Sapp ("); dump_data(f, param[0]); Pervasives.print_string(", "); dump_data(f, param[1]); return Pervasives.print_string(")"); case /* Slazy */2 : return Pervasives.print_string("Slazy"); case /* Sgen */3 : return Pervasives.print_string("Sgen"); case /* Sbuffio */4 : return Pervasives.print_string("Sbuffio"); } } } function dump(f, s) { Pervasives.print_string("{count = "); Pervasives.print_int(count(s)); Pervasives.print_string("; data = "); dump_data(f, data(s)); Pervasives.print_string("}"); return Pervasives.print_newline(/* () */0); } var sempty = undefined; 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.of_channel = of_channel; 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 */