UNPKG

bs-platform

Version:

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

817 lines (775 loc) 21.5 kB
'use strict'; var Caml_int32 = require("./caml_int32.js"); var Caml_int64 = require("./caml_int64.js"); var Caml_utils = require("./caml_utils.js"); var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js"); function parse_digit(c) { if (c >= 65) { if (c >= 97) { if (c >= 123) { return -1; } else { return c - 87 | 0; } } else if (c >= 91) { return -1; } else { return c - 55 | 0; } } else if (c > 57 || c < 48) { return -1; } else { return c - /* "0" */48 | 0; } } function int_of_string_base(param) { switch (param) { case 0 : return 8; case 1 : return 16; case 2 : return 10; case 3 : return 2; } } function parse_sign_and_base(s) { var sign = 1; var base = /* Dec */2; var i = 0; var match = s.charCodeAt(i); switch (match) { case 43 : i = i + 1 | 0; break; case 44 : break; case 45 : sign = -1; i = i + 1 | 0; break; default: } if (s[i] === "0") { var match$1 = s.charCodeAt(i + 1 | 0); if (match$1 >= 89) { if (match$1 >= 111) { if (match$1 < 121) { switch (match$1 - 111 | 0) { case 0 : base = /* Oct */0; i = i + 2 | 0; break; case 6 : i = i + 2 | 0; break; case 1 : case 2 : case 3 : case 4 : case 5 : case 7 : case 8 : break; case 9 : base = /* Hex */1; i = i + 2 | 0; break; } } } else if (match$1 === 98) { base = /* Bin */3; i = i + 2 | 0; } } else if (match$1 !== 66) { if (match$1 >= 79) { switch (match$1 - 79 | 0) { case 0 : base = /* Oct */0; i = i + 2 | 0; break; case 6 : i = i + 2 | 0; break; case 1 : case 2 : case 3 : case 4 : case 5 : case 7 : case 8 : break; case 9 : base = /* Hex */1; i = i + 2 | 0; break; } } } else { base = /* Bin */3; i = i + 2 | 0; } } return /* tuple */[ i, sign, base ]; } function caml_int_of_string(s) { var match = parse_sign_and_base(s); var i = match[0]; var base = int_of_string_base(match[2]); var threshold = 4294967295; var len = s.length; var c = i < len ? s.charCodeAt(i) : /* "\000" */0; var d = parse_digit(c); if (d < 0 || d >= base) { throw [ Caml_builtin_exceptions.failure, "int_of_string" ]; } var aux = function (_acc, _k) { while(true) { var k = _k; var acc = _acc; if (k === len) { return acc; } else { var a = s.charCodeAt(k); if (a === /* "_" */95) { _k = k + 1 | 0; continue ; } else { var v = parse_digit(a); if (v < 0 || v >= base) { throw [ Caml_builtin_exceptions.failure, "int_of_string" ]; } else { var acc$1 = base * acc + v; if (acc$1 > threshold) { throw [ Caml_builtin_exceptions.failure, "int_of_string" ]; } else { _k = k + 1 | 0; _acc = acc$1; continue ; } } } } }; }; var res = match[1] * aux(d, i + 1 | 0); var or_res = res | 0; if (base === 10 && res !== or_res) { throw [ Caml_builtin_exceptions.failure, "int_of_string" ]; } return or_res; } function caml_int64_of_string(s) { var match = parse_sign_and_base(s); var hbase = match[2]; var i = match[0]; var base = Caml_int64.of_int32(int_of_string_base(hbase)); var sign = Caml_int64.of_int32(match[1]); var threshold; switch (hbase) { case 0 : threshold = /* int64 */[ /* hi */536870911, /* lo */4294967295 ]; break; case 1 : threshold = /* int64 */[ /* hi */268435455, /* lo */4294967295 ]; break; case 2 : threshold = /* int64 */[ /* hi */429496729, /* lo */2576980377 ]; break; case 3 : threshold = /* int64 */[ /* hi */2147483647, /* lo */4294967295 ]; break; } var len = s.length; var c = i < len ? s.charCodeAt(i) : /* "\000" */0; var d = Caml_int64.of_int32(parse_digit(c)); if (Caml_int64.lt(d, /* int64 */[ /* hi */0, /* lo */0 ]) || Caml_int64.ge(d, base)) { throw [ Caml_builtin_exceptions.failure, "int64_of_string" ]; } var aux = function (_acc, _k) { while(true) { var k = _k; var acc = _acc; if (k === len) { return acc; } else { var a = s.charCodeAt(k); if (a === /* "_" */95) { _k = k + 1 | 0; continue ; } else { var v = Caml_int64.of_int32(parse_digit(a)); if (Caml_int64.lt(v, /* int64 */[ /* hi */0, /* lo */0 ]) || Caml_int64.ge(v, base) || Caml_int64.gt(acc, threshold)) { throw [ Caml_builtin_exceptions.failure, "int64_of_string" ]; } else { var acc$1 = Caml_int64.add(Caml_int64.mul(base, acc), v); _k = k + 1 | 0; _acc = acc$1; continue ; } } } }; }; var res = Caml_int64.mul(sign, aux(d, i + 1 | 0)); var or_res = Caml_int64.or_(res, /* int64 */[ /* hi */0, /* lo */0 ]); if (Caml_int64.eq(base, /* int64 */[ /* hi */0, /* lo */10 ]) && Caml_int64.neq(res, or_res)) { throw [ Caml_builtin_exceptions.failure, "int64_of_string" ]; } return or_res; } function int_of_base(param) { switch (param) { case 0 : return 8; case 1 : return 16; case 2 : return 10; } } function lowercase(c) { if (c >= /* "A" */65 && c <= /* "Z" */90 || c >= /* "\192" */192 && c <= /* "\214" */214 || c >= /* "\216" */216 && c <= /* "\222" */222) { return c + 32 | 0; } else { return c; } } function parse_format(fmt) { var len = fmt.length; if (len > 31) { throw [ Caml_builtin_exceptions.invalid_argument, "format_int: format too long" ]; } var f = /* record */[ /* justify */"+", /* signstyle */"-", /* filter */" ", /* alternate */false, /* base : Dec */2, /* signedconv */false, /* width */0, /* uppercase */false, /* sign */1, /* prec */-1, /* conv */"f" ]; var _i = 0; while(true) { var i = _i; if (i >= len) { return f; } else { var c = fmt.charCodeAt(i); var exit = 0; if (c >= 69) { if (c >= 88) { if (c >= 121) { exit = 1; } else { switch (c - 88 | 0) { case 0 : f[/* base */4] = /* Hex */1; f[/* uppercase */7] = true; _i = i + 1 | 0; continue ; case 13 : case 14 : case 15 : exit = 5; break; case 12 : case 17 : exit = 4; break; case 23 : f[/* base */4] = /* Oct */0; _i = i + 1 | 0; continue ; case 29 : f[/* base */4] = /* Dec */2; _i = i + 1 | 0; continue ; case 1 : case 2 : case 3 : case 4 : case 5 : case 6 : case 7 : case 8 : case 9 : case 10 : case 11 : case 16 : case 18 : case 19 : case 20 : case 21 : case 22 : case 24 : case 25 : case 26 : case 27 : case 28 : case 30 : case 31 : exit = 1; break; case 32 : f[/* base */4] = /* Hex */1; _i = i + 1 | 0; continue ; } } } else if (c >= 72) { exit = 1; } else { f[/* signedconv */5] = true; f[/* uppercase */7] = true; f[/* conv */10] = String.fromCharCode(lowercase(c)); _i = i + 1 | 0; continue ; } } else { switch (c) { case 35 : f[/* alternate */3] = true; _i = i + 1 | 0; continue ; case 32 : case 43 : exit = 2; break; case 45 : f[/* justify */0] = "-"; _i = i + 1 | 0; continue ; case 46 : f[/* prec */9] = 0; var j = i + 1 | 0; while((function(j){ return function () { var w = fmt.charCodeAt(j) - /* "0" */48 | 0; return w >= 0 && w <= 9; } }(j))()) { f[/* prec */9] = (Caml_int32.imul(f[/* prec */9], 10) + fmt.charCodeAt(j) | 0) - /* "0" */48 | 0; j = j + 1 | 0; }; _i = j; continue ; case 33 : case 34 : case 36 : case 37 : case 38 : case 39 : case 40 : case 41 : case 42 : case 44 : case 47 : exit = 1; break; case 48 : f[/* filter */2] = "0"; _i = i + 1 | 0; continue ; case 49 : case 50 : case 51 : case 52 : case 53 : case 54 : case 55 : case 56 : case 57 : exit = 3; break; default: exit = 1; } } switch (exit) { case 1 : _i = i + 1 | 0; continue ; case 2 : f[/* signstyle */1] = String.fromCharCode(c); _i = i + 1 | 0; continue ; case 3 : f[/* width */6] = 0; var j$1 = i; while((function(j$1){ return function () { var w = fmt.charCodeAt(j$1) - /* "0" */48 | 0; return w >= 0 && w <= 9; } }(j$1))()) { f[/* width */6] = (Caml_int32.imul(f[/* width */6], 10) + fmt.charCodeAt(j$1) | 0) - /* "0" */48 | 0; j$1 = j$1 + 1 | 0; }; _i = j$1; continue ; case 4 : f[/* signedconv */5] = true; f[/* base */4] = /* Dec */2; _i = i + 1 | 0; continue ; case 5 : f[/* signedconv */5] = true; f[/* conv */10] = String.fromCharCode(c); _i = i + 1 | 0; continue ; } } }; } function finish_formatting(config, rawbuffer) { var justify = config[/* justify */0]; var signstyle = config[/* signstyle */1]; var filter = config[/* filter */2]; var alternate = config[/* alternate */3]; var base = config[/* base */4]; var signedconv = config[/* signedconv */5]; var width = config[/* width */6]; var uppercase = config[/* uppercase */7]; var sign = config[/* sign */8]; var len = rawbuffer.length; if (signedconv && (sign < 0 || signstyle !== "-")) { len = len + 1 | 0; } if (alternate) { if (base === /* Oct */0) { len = len + 1 | 0; } else if (base === /* Hex */1) { len = len + 2 | 0; } } var buffer = ""; if (justify === "+" && filter === " ") { for(var i = len ,i_finish = width - 1 | 0; i <= i_finish; ++i){ buffer = buffer + filter; } } if (signedconv) { if (sign < 0) { buffer = buffer + "-"; } else if (signstyle !== "-") { buffer = buffer + signstyle; } } if (alternate && base === /* Oct */0) { buffer = buffer + "0"; } if (alternate && base === /* Hex */1) { buffer = buffer + "0x"; } if (justify === "+" && filter === "0") { for(var i$1 = len ,i_finish$1 = width - 1 | 0; i$1 <= i_finish$1; ++i$1){ buffer = buffer + filter; } } buffer = uppercase ? buffer + rawbuffer.toUpperCase() : buffer + rawbuffer; if (justify === "-") { for(var i$2 = len ,i_finish$2 = width - 1 | 0; i$2 <= i_finish$2; ++i$2){ buffer = buffer + " "; } } return buffer; } function caml_format_int(fmt, i) { if (fmt === "%d") { return String(i); } else { var f = parse_format(fmt); var f$1 = f; var i$1 = i; var i$2 = i$1 < 0 ? ( f$1[/* signedconv */5] ? (f$1[/* sign */8] = -1, -i$1) : (i$1 >>> 0) ) : i$1; var s = i$2.toString(int_of_base(f$1[/* base */4])); if (f$1[/* prec */9] >= 0) { f$1[/* filter */2] = " "; var n = f$1[/* prec */9] - s.length | 0; if (n > 0) { s = Caml_utils.repeat(n, "0") + s; } } return finish_formatting(f$1, s); } } function caml_int64_format(fmt, x) { var f = parse_format(fmt); var x$1 = f[/* signedconv */5] && Caml_int64.lt(x, /* int64 */[ /* hi */0, /* lo */0 ]) ? (f[/* sign */8] = -1, Caml_int64.neg(x)) : x; var s = ""; var match = f[/* base */4]; switch (match) { case 0 : var wbase = /* int64 */[ /* hi */0, /* lo */8 ]; var cvtbl = "01234567"; if (Caml_int64.lt(x$1, /* int64 */[ /* hi */0, /* lo */0 ])) { var y = Caml_int64.discard_sign(x$1); var match$1 = Caml_int64.div_mod(y, wbase); var quotient = Caml_int64.add(/* int64 */[ /* hi */268435456, /* lo */0 ], match$1[0]); var modulus = match$1[1]; s = String.fromCharCode(cvtbl.charCodeAt(modulus[1] | 0)) + s; while(Caml_int64.neq(quotient, /* int64 */[ /* hi */0, /* lo */0 ])) { var match$2 = Caml_int64.div_mod(quotient, wbase); quotient = match$2[0]; modulus = match$2[1]; s = String.fromCharCode(cvtbl.charCodeAt(modulus[1] | 0)) + s; }; } else { var match$3 = Caml_int64.div_mod(x$1, wbase); var quotient$1 = match$3[0]; var modulus$1 = match$3[1]; s = String.fromCharCode(cvtbl.charCodeAt(modulus$1[1] | 0)) + s; while(Caml_int64.neq(quotient$1, /* int64 */[ /* hi */0, /* lo */0 ])) { var match$4 = Caml_int64.div_mod(quotient$1, wbase); quotient$1 = match$4[0]; modulus$1 = match$4[1]; s = String.fromCharCode(cvtbl.charCodeAt(modulus$1[1] | 0)) + s; }; } break; case 1 : s = Caml_int64.to_hex(x$1) + s; break; case 2 : var wbase$1 = /* int64 */[ /* hi */0, /* lo */10 ]; var cvtbl$1 = "0123456789"; if (Caml_int64.lt(x$1, /* int64 */[ /* hi */0, /* lo */0 ])) { var y$1 = Caml_int64.discard_sign(x$1); var match$5 = Caml_int64.div_mod(y$1, wbase$1); var match$6 = Caml_int64.div_mod(Caml_int64.add(/* int64 */[ /* hi */0, /* lo */8 ], match$5[1]), wbase$1); var quotient$2 = Caml_int64.add(Caml_int64.add(/* int64 */[ /* hi */214748364, /* lo */3435973836 ], match$5[0]), match$6[0]); var modulus$2 = match$6[1]; s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$2[1] | 0)) + s; while(Caml_int64.neq(quotient$2, /* int64 */[ /* hi */0, /* lo */0 ])) { var match$7 = Caml_int64.div_mod(quotient$2, wbase$1); quotient$2 = match$7[0]; modulus$2 = match$7[1]; s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$2[1] | 0)) + s; }; } else { var match$8 = Caml_int64.div_mod(x$1, wbase$1); var quotient$3 = match$8[0]; var modulus$3 = match$8[1]; s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$3[1] | 0)) + s; while(Caml_int64.neq(quotient$3, /* int64 */[ /* hi */0, /* lo */0 ])) { var match$9 = Caml_int64.div_mod(quotient$3, wbase$1); quotient$3 = match$9[0]; modulus$3 = match$9[1]; s = String.fromCharCode(cvtbl$1.charCodeAt(modulus$3[1] | 0)) + s; }; } break; } if (f[/* prec */9] >= 0) { f[/* filter */2] = " "; var n = f[/* prec */9] - s.length | 0; if (n > 0) { s = Caml_utils.repeat(n, "0") + s; } } return finish_formatting(f, s); } function caml_format_float(fmt, x) { var f = parse_format(fmt); var prec = f[/* prec */9] < 0 ? 6 : f[/* prec */9]; var x$1 = x < 0 ? (f[/* sign */8] = -1, -x) : x; var s = ""; if (isNaN(x$1)) { s = "nan"; f[/* filter */2] = " "; } else if (isFinite(x$1)) { var match = f[/* conv */10]; switch (match) { case "e" : s = x$1.toExponential(prec); var i = s.length; if (s[i - 3 | 0] === "e") { s = s.slice(0, i - 1 | 0) + ("0" + s.slice(i - 1 | 0)); } break; case "f" : s = x$1.toFixed(prec); break; case "g" : var prec$1 = prec !== 0 ? prec : 1; s = x$1.toExponential(prec$1 - 1 | 0); var j = s.indexOf("e"); var exp = Number(s.slice(j + 1 | 0)) | 0; if (exp < -4 || x$1 >= 1e21 || x$1.toFixed().length > prec$1) { var i$1 = j - 1 | 0; while(s[i$1] === "0") { i$1 = i$1 - 1 | 0; }; if (s[i$1] === ".") { i$1 = i$1 - 1 | 0; } s = s.slice(0, i$1 + 1 | 0) + s.slice(j); var i$2 = s.length; if (s[i$2 - 3 | 0] === "e") { s = s.slice(0, i$2 - 1 | 0) + ("0" + s.slice(i$2 - 1 | 0)); } } else { var p = prec$1; if (exp < 0) { p = p - (exp + 1 | 0) | 0; s = x$1.toFixed(p); } else { while((function () { s = x$1.toFixed(p); return s.length > (prec$1 + 1 | 0); })()) { p = p - 1 | 0; }; } if (p !== 0) { var k = s.length - 1 | 0; while(s[k] === "0") { k = k - 1 | 0; }; if (s[k] === ".") { k = k - 1 | 0; } s = s.slice(0, k + 1 | 0); } } break; default: } } else { s = "inf"; f[/* filter */2] = " "; } return finish_formatting(f, s); } function float_of_string (s,exn){ var res = +s; if ((s.length > 0) && (res === res)) return res; s = s.replace(/_/g, ""); res = +s; if (((s.length > 0) && (res === res)) || /^[+-]?nan$/i.test(s)) { return res; }; if (/^\+?inf(inity)?$/i.test(s)) return Infinity; if (/^-inf(inity)?$/i.test(s)) return -Infinity; throw exn; }; function caml_float_of_string(s) { return float_of_string(s, [ Caml_builtin_exceptions.failure, "float_of_string" ]); } var caml_nativeint_format = caml_format_int; var caml_int32_format = caml_format_int; var caml_int32_of_string = caml_int_of_string; var caml_nativeint_of_string = caml_int_of_string; exports.caml_format_float = caml_format_float; exports.caml_format_int = caml_format_int; exports.caml_nativeint_format = caml_nativeint_format; exports.caml_int32_format = caml_int32_format; exports.caml_float_of_string = caml_float_of_string; exports.caml_int64_format = caml_int64_format; exports.caml_int_of_string = caml_int_of_string; exports.caml_int32_of_string = caml_int32_of_string; exports.caml_int64_of_string = caml_int64_of_string; exports.caml_nativeint_of_string = caml_nativeint_of_string; /* No side effect */