js-slang
Version:
Javascript-based implementations of Source, written in Typescript
418 lines • 13.6 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.math_trunc = exports.math_tanh = exports.math_tan = exports.math_sqrt = exports.math_sinh = exports.math_sin = exports.math_sign = exports.math_round = exports.math_random = exports.math_pow = exports.math_min = exports.math_max = exports.math_log10 = exports.math_log2 = exports.math_log1p = exports.math_log = exports.math_imul = exports.math_hypot = exports.math_fround = exports.math_floor = exports.math_expm1 = exports.math_exp = exports.math_cosh = exports.math_cos = exports.math_clz32 = exports.math_ceil = exports.math_cbrt = exports.math_atanh = exports.math_atan2 = exports.math_atan = exports.math_asinh = exports.math_asin = exports.math_acosh = exports.math_acos = exports.math_abs = exports.__py_unary_plus = exports.__py_floorer = exports.__py_powerer = exports.__py_modder = exports.__py_divider = exports.__py_multiplier = exports.__py_minuser = exports.__py_adder = exports.is_int = exports.is_float = void 0;
function is_float(v) {
return typeof v === 'number';
}
exports.is_float = is_float;
function is_int(v) {
return typeof v === 'bigint';
}
exports.is_int = is_int;
function __is_numeric(v) {
return is_int(v) || is_float(v);
}
function __is_string(v) {
// Retrieved from https://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string-in-javascript
return typeof v === 'string' || v instanceof String;
}
function __py_adder(x, y) {
if (typeof x === typeof y) {
return x + y;
}
if (typeof x === 'bigint') {
return Number(x) + y;
}
if (typeof y === 'bigint') {
return x + Number(y);
}
if (__is_numeric(x) && __is_numeric(y)) {
return x + y;
}
throw new Error(`Invalid types for addition operation: ${typeof x}, ${typeof y}`);
}
exports.__py_adder = __py_adder;
function __py_minuser(x, y) {
if (!__is_numeric(x)) {
throw new Error('Expected number on left hand side of operation, got ' + typeof x + '.');
}
if (!__is_numeric(y)) {
throw new Error('Expected number on right hand side of operation, got ' + typeof y + '.');
}
if (typeof x === 'bigint' && typeof y === 'bigint') {
return x - y;
}
if (typeof x === 'number' && typeof y === 'number') {
return x - y;
}
if (typeof x === 'bigint' && typeof y === 'number') {
return Number(x) - y;
}
if (typeof x === 'number' && typeof y === 'bigint') {
return x - Number(y);
}
throw new Error(`Invalid types for subtraction operation: ${typeof x}, ${typeof y}`);
}
exports.__py_minuser = __py_minuser;
function __py_multiplier(x, y) {
if (!((__is_numeric(x) && __is_numeric(y)) ||
(__is_string(x) && __is_numeric(y)) ||
(__is_string(y) && __is_numeric(x)))) {
throw new Error(`Invalid types for multiply operation: ${typeof x}, ${typeof y}`);
}
if (typeof x === 'bigint' && typeof y === 'bigint') {
return x * y;
}
if (typeof x === 'number' && typeof y === 'number') {
return x * y;
}
if (typeof x === 'bigint' && typeof y === 'number') {
return Number(x) * y;
}
if (typeof x === 'number' && typeof y === 'bigint') {
return x * Number(y);
}
if (typeof x == 'number' && __is_string(y)) {
return y.repeat(x);
}
if (typeof y == 'number' && __is_string(x)) {
return x.repeat(y);
}
throw new Error(`Invalid types for multiply operation: ${typeof x}, ${typeof y}`);
}
exports.__py_multiplier = __py_multiplier;
function __py_divider(x, y) {
if (!__is_numeric(x)) {
throw new Error('Expected number on left hand side of operation, got ' + typeof x + '.');
}
if (!__is_numeric(y)) {
throw new Error('Expected number on right hand side of operation, got ' + typeof y + '.');
}
if (typeof x === 'bigint' && typeof y === 'bigint') {
return Number(x) / Number(y);
}
if (typeof x === 'number' && typeof y === 'number') {
return x / y;
}
if (typeof x === 'bigint' && typeof y === 'number') {
return Number(x) / y;
}
if (typeof x === 'number' && typeof y === 'bigint') {
return x / Number(y);
}
throw new Error(`Invalid types for divide operation: ${typeof x}, ${typeof y}`);
}
exports.__py_divider = __py_divider;
function __py_modder(x, y) {
if (!__is_numeric(x)) {
throw new Error('Expected number on left hand side of operation, got ' + typeof x + '.');
}
if (!__is_numeric(y)) {
throw new Error('Expected number on right hand side of operation, got ' + typeof y + '.');
}
if (typeof x === 'bigint' && typeof y === 'bigint') {
return x % y;
}
if (typeof x === 'number' && typeof y === 'number') {
return x % y;
}
if (typeof x === 'bigint' && typeof y === 'number') {
return Number(x) % y;
}
if (typeof x === 'number' && typeof y === 'bigint') {
return x % Number(y);
}
throw new Error(`Invalid types for modulo operation: ${typeof x}, ${typeof y}`);
}
exports.__py_modder = __py_modder;
function __py_powerer(x, y) {
if (!__is_numeric(x)) {
throw new Error('Expected number on left hand side of operation, got ' + typeof x + '.');
}
if (!__is_numeric(y)) {
throw new Error('Expected number on right hand side of operation, got ' + typeof y + '.');
}
if (typeof x === 'bigint' && typeof y === 'bigint') {
let res = BigInt(1);
for (let i = 0; i < y; i++) {
res = res * x;
}
return res;
}
if (typeof x === 'number' && typeof y === 'number') {
return Math.pow(x, y);
}
if (typeof x === 'bigint' && typeof y === 'number') {
return Math.pow(Number(x), y);
}
if (typeof x === 'number' && typeof y === 'bigint') {
return Math.pow(x, Number(y));
}
throw new Error(`Invalid types for power operation: ${typeof x}, ${typeof y}`);
}
exports.__py_powerer = __py_powerer;
function __py_floorer(x, y) {
return BigInt(Math.floor(__py_divider(x, y)));
}
exports.__py_floorer = __py_floorer;
function __py_unary_plus(x) {
if (__is_numeric(x)) {
return +Number(x);
}
throw new Error(`Invalid type for unary plus operation: ${typeof x}`);
}
exports.__py_unary_plus = __py_unary_plus;
function math_abs(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.abs(Number(x));
}
exports.math_abs = math_abs;
function math_acos(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.acos(Number(x));
}
exports.math_acos = math_acos;
function math_acosh(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.acosh(Number(x));
}
exports.math_acosh = math_acosh;
function math_asin(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.asin(Number(x));
}
exports.math_asin = math_asin;
function math_asinh(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.asinh(Number(x));
}
exports.math_asinh = math_asinh;
function math_atan(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.atan(Number(x));
}
exports.math_atan = math_atan;
function math_atan2(y, x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.atan2(Number(y), Number(x));
}
exports.math_atan2 = math_atan2;
function math_atanh(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.atanh(Number(x));
}
exports.math_atanh = math_atanh;
function math_cbrt(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.cbrt(Number(x));
}
exports.math_cbrt = math_cbrt;
function math_ceil(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.ceil(Number(x));
}
exports.math_ceil = math_ceil;
function math_clz32(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.clz32(Number(x));
}
exports.math_clz32 = math_clz32;
function math_cos(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.cos(Number(x));
}
exports.math_cos = math_cos;
function math_cosh(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.cosh(Number(x));
}
exports.math_cosh = math_cosh;
function math_exp(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.exp(Number(x));
}
exports.math_exp = math_exp;
function math_expm1(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.expm1(Number(x));
}
exports.math_expm1 = math_expm1;
function math_floor(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.floor(Number(x));
}
exports.math_floor = math_floor;
function math_fround(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.fround(Number(x));
}
exports.math_fround = math_fround;
function math_hypot(...elements) {
const coercedElements = elements.map(el => {
if (!__is_numeric(el)) {
throw new Error(`Invalid type for operation: ${typeof el}`);
}
return Number(el);
});
return Math.hypot(...coercedElements);
}
exports.math_hypot = math_hypot;
function math_imul(x, y) {
if (!__is_numeric(x) || !__is_numeric(y)) {
throw new Error(`Invalid types for power operation: ${typeof x}, ${typeof y}`);
}
return Math.imul(Number(x), Number(y));
}
exports.math_imul = math_imul;
function math_log(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.log(Number(x));
}
exports.math_log = math_log;
function math_log1p(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.log1p(Number(x));
}
exports.math_log1p = math_log1p;
function math_log2(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.log2(Number(x));
}
exports.math_log2 = math_log2;
function math_log10(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.log10(Number(x));
}
exports.math_log10 = math_log10;
function math_max(...elements) {
// TODO: Python max also supports strings!
const coercedElements = elements.map(el => {
if (!__is_numeric(el)) {
throw new Error(`Invalid type for operation: ${typeof el}`);
}
return Number(el);
});
return Math.max(...coercedElements);
}
exports.math_max = math_max;
function math_min(...elements) {
// TODO: Python min also supports strings!
const coercedElements = elements.map(el => {
if (!__is_numeric(el)) {
throw new Error(`Invalid type for operation: ${typeof el}`);
}
return Number(el);
});
return Math.min(...coercedElements);
}
exports.math_min = math_min;
function math_pow(x, y) {
if (!__is_numeric(x) || !__is_numeric(y)) {
throw new Error(`Invalid types for power operation: ${typeof x}, ${typeof y}`);
}
return Math.pow(Number(x), Number(y));
}
exports.math_pow = math_pow;
function math_random() {
return Math.random();
}
exports.math_random = math_random;
function math_round(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.round(Number(x));
}
exports.math_round = math_round;
function math_sign(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.sign(Number(x));
}
exports.math_sign = math_sign;
function math_sin(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.sin(Number(x));
}
exports.math_sin = math_sin;
function math_sinh(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.sinh(Number(x));
}
exports.math_sinh = math_sinh;
function math_sqrt(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.sqrt(Number(x));
}
exports.math_sqrt = math_sqrt;
function math_tan(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.tan(Number(x));
}
exports.math_tan = math_tan;
function math_tanh(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.tanh(Number(x));
}
exports.math_tanh = math_tanh;
function math_trunc(x) {
if (!__is_numeric(x)) {
throw new Error(`Invalid type for operation: ${typeof x}`);
}
return Math.trunc(Number(x));
}
exports.math_trunc = math_trunc;
//# sourceMappingURL=pylib.js.map
;