UNPKG

@abaplint/runtime

Version:
107 lines 3.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Integer = exports.MIN_INTEGER = exports.MAX_INTEGER = exports.DIGITS = void 0; exports.toInteger = toInteger; const throw_error_1 = require("../throw_error"); const float_1 = require("./float"); const hex_1 = require("./hex"); const xstring_1 = require("./xstring"); const integer8_1 = require("./integer8"); const hex_uint8_1 = require("./hex_uint8"); const character_1 = require("./character"); const string_1 = require("./string"); exports.DIGITS = new RegExp(/^\s*-?\+?\d+\.?\d* *$/i); exports.MAX_INTEGER = 2147483647; exports.MIN_INTEGER = -2147483648; function toInteger(value, exception = true) { if (value.endsWith("-")) { value = "-" + value.substring(0, value.length - 1); } if (value.trim().length === 0) { value = "0"; } else if (exports.DIGITS.test(value) === false) { if (exception === true) { (0, throw_error_1.throwError)("CX_SY_CONVERSION_NO_NUMBER"); } else { throw new Error("CONVT_NO_NUMBER"); } } return Math.round(parseFloat(value)); } class Integer { constructor(input) { this.constant = false; this.value = 0; this.qualifiedName = input?.qualifiedName; } getQualifiedName() { return this.qualifiedName; } clone() { const n = new Integer({ qualifiedName: this.qualifiedName }); // set without trigger checks and padding n.value = this.value; return n; } setConstant() { this.constant = true; return this; } set(value) { if (this.constant === true) { throw new Error("Changing constant"); } if (typeof value === "number") { this.value = Math.round(value); } else if (value instanceof Integer) { this.set(value.get()); } else if (value instanceof character_1.Character) { this.value = toInteger(value.get()); } else if (value instanceof string_1.String) { this.value = toInteger(value.get()); } else if (value instanceof integer8_1.Integer8) { this.set(Number(value.get())); } else if (value instanceof float_1.Float) { this.set(Math.round(value.getRaw())); } else if (value instanceof hex_1.Hex || value instanceof xstring_1.XString || value instanceof hex_uint8_1.HexUInt8) { let num = parseInt(value.get(), 16); // handle two complement, if ((value instanceof hex_1.Hex || value instanceof hex_uint8_1.HexUInt8) && value.getLength() >= 4) { const maxVal = Math.pow(2, value.get().length / 2 * 8); if (num > maxVal / 2 - 1) { num = num - maxVal; } } this.set(num); } else if (typeof value === "string") { this.value = toInteger(value); } else { this.set(value.get()); } /* if (this.value > 2147483647 || this.value < -2147483648) { throwError("CX_SY_ARITHMETIC_OVERFLOW"); } */ return this; } clear() { this.value = 0; } get() { return this.value; } } exports.Integer = Integer; //# sourceMappingURL=integer.js.map