Expand description
Lua numeral conversion core (stone candidate: pure functions, no runtime
types). Two consumers: the lexer (literal tokens, shape pre-validated by
scanning) and the VM/stdlib (str2num — luaO_str2num semantics with
whitespace and sign). Versioning is expressed as capability flags so this
module stays dialect-agnostic.
Enums§
- Num
- Result of parsing a Lua numeric literal — either an integer or a float (Lua 5.1 collapses everything to float at this layer).
Functions§
- dec_
literal - Decimal numeral (no sign, no surrounding space).
int_ok = falseforces float results (Lua 5.1: numbers are doubles).negis whether a leading ‘-’ was stripped by the caller: it widens the integer range by one unit (PUC l_str2int’s+ neg) so that the magnitude 2^63 parses as an integer — lettingtonumber("-9223372036854775808")recover minint. The caller still applies the actual negation. - hex_
digit - Decode a single ASCII hex digit (
0-9,a-f,A-F) into its numeric value, or returnNonefor a non-hex byte. - hex_
literal - Hex numeral after the
0xprefix (no sign, no surrounding space). - num_
to_ string - Lua number → text. Integers print as integers. Floats print with
shortest round-trip digits (the 5.5 “read back correctly” rule) in
C
%g-style presentation: scientific form when the decimal exponent falls outside [-4, 14), two-digit signed exponent, and.0appended to integral-looking decimals (PUC lua_number2str). Exact boundary alignment against PUC 5.5 output is rechecked by the P04 gate (strings/math suites). - num_
to_ string_ for - Variant for ≤5.2: those dialects only had
lua_Number(a double), so PUC’s%.14gformatter trims any trailing.0(an integer-valued float renders as plain2, not2.0). 5.3+ introduced the integer subtype and the renderer started appending.0to distinguish floats — pm.lua’s pattern transformations build"%" .. (s+1)and need"%2"on 5.1/5.2. - str2num
- luaO_str2num: optional surrounding whitespace and sign, decimal or hex.
Used by VM string→number coercion and
tonumber. - write_
i64_ dec - Write i64 decimal into a stack buffer; returns the slice of valid
bytes inside
buf. 20 chars covers i64::MIN..=i64::MAX (the longest is “-9223372036854775808” at 20 bytes). Hot in tostring(int) on numeric-heavy workloads (string_concat builds 5000 of these): skips the String allocation thati.to_string()does.