mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
LibJS: Add support for hex, octal & binary big integer literals
This commit is contained in:
parent
2ad2e055e2
commit
690eb3bb8a
3 changed files with 34 additions and 0 deletions
|
@ -1830,6 +1830,16 @@ Value NumericLiteral::execute(Interpreter& interpreter, GlobalObject&) const
|
|||
Value BigIntLiteral::execute(Interpreter& interpreter, GlobalObject&) const
|
||||
{
|
||||
InterpreterNodeScope node_scope { interpreter, *this };
|
||||
Crypto::SignedBigInteger integer;
|
||||
if (m_value[0] == '0' && m_value.length() >= 3) {
|
||||
if (m_value[1] == 'x' || m_value[1] == 'X') {
|
||||
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base16(m_value.substring(2, m_value.length() - 3)));
|
||||
} else if (m_value[1] == 'o' || m_value[1] == 'O') {
|
||||
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base8(m_value.substring(2, m_value.length() - 3)));
|
||||
} else if (m_value[1] == 'b' || m_value[1] == 'B') {
|
||||
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base2(m_value.substring(2, m_value.length() - 3)));
|
||||
}
|
||||
}
|
||||
return js_bigint(interpreter.heap(), Crypto::SignedBigInteger::from_base10(m_value.substring(0, m_value.length() - 1)));
|
||||
}
|
||||
|
||||
|
|
|
@ -466,12 +466,24 @@ Token Lexer::next()
|
|||
} else if (m_current_char == 'o' || m_current_char == 'O') {
|
||||
// octal
|
||||
is_invalid_numeric_literal = !consume_octal_number();
|
||||
if (m_current_char == 'n') {
|
||||
consume();
|
||||
token_type = TokenType::BigIntLiteral;
|
||||
}
|
||||
} else if (m_current_char == 'b' || m_current_char == 'B') {
|
||||
// binary
|
||||
is_invalid_numeric_literal = !consume_binary_number();
|
||||
if (m_current_char == 'n') {
|
||||
consume();
|
||||
token_type = TokenType::BigIntLiteral;
|
||||
}
|
||||
} else if (m_current_char == 'x' || m_current_char == 'X') {
|
||||
// hexadecimal
|
||||
is_invalid_numeric_literal = !consume_hexadecimal_number();
|
||||
if (m_current_char == 'n') {
|
||||
consume();
|
||||
token_type = TokenType::BigIntLiteral;
|
||||
}
|
||||
} else if (m_current_char == 'n') {
|
||||
consume();
|
||||
token_type = TokenType::BigIntLiteral;
|
||||
|
|
|
@ -7,6 +7,18 @@ describe("correct behavior", () => {
|
|||
expect("" + 123n).toBe("123");
|
||||
});
|
||||
|
||||
test("hex literals", () => {
|
||||
expect(0xffn).toBe(255n);
|
||||
});
|
||||
|
||||
test("octal literals", () => {
|
||||
expect(0o10n).toBe(8n);
|
||||
});
|
||||
|
||||
test("binary literals", () => {
|
||||
expect(0b10n).toBe(2n);
|
||||
});
|
||||
|
||||
test("arithmetic operators", () => {
|
||||
let bigint = 123n;
|
||||
expect(-bigint).toBe(-123n);
|
||||
|
|
Loading…
Add table
Reference in a new issue