Expand description
§value_lang
Compact runtime value representation for interpreted languages, by NaN-boxing.
A dynamically-typed interpreter needs one type to stand for every runtime
value — nil, a boolean, a number, an interned name — and it copies that type on
nearly every instruction. value-lang packs all of those into a single 64-bit
Value: eight bytes, Copy, and free of any discriminant word, because the
kind is folded into the bit pattern itself. The technique is NaN-boxing — real
floats are stored as themselves, and every other kind hides inside the quiet-NaN
encodings that no genuine float produces.
The whole representation is safe Rust: it is built from f64::to_bits and
f64::from_bits and integer arithmetic, with no pointers and no unsafe
(unsafe is forbidden crate-wide). It owns value representation and nothing
else — strings and identifiers travel as Symbol handles from
intern-lang, resolved against the interner that issued them.
§At a glance
Value— the eight-byte NaN-boxed handle. Build one withnil,bool,int,float, orsym; test it with theis_*predicates; read it back with theas_*accessors.Unpacked— the tagged-union view. CallValue::unpacktomatchon every kind at once, andValue::fromto pack one back.Symbol— re-exported fromintern-lang; the compact string handle aValuecan carry.
§Example
use value_lang::{Unpacked, Value};
// One eight-byte type for every runtime value.
let stack = [Value::int(2), Value::float(0.5), Value::bool(true), Value::nil()];
// Dispatch on the kind by unpacking.
let ints: i32 = stack
.iter()
.filter_map(|v| v.as_int())
.sum();
assert_eq!(ints, 2);
assert_eq!(stack[1].unpack(), Unpacked::Float(0.5));
assert_eq!(core::mem::size_of::<Value>(), 8);§no_std
The crate is no_std-compatible and does not even require alloc: the
representation is pure integer and float arithmetic. The default std feature
is additive and only forwards std to intern-lang. Disable default features
to build for a bare target. The optional serde feature adds Serialize /
Deserialize for Value.
Structs§
- Symbol
- A small, copyable handle to a string held by an
Interner. - Value
- A runtime value, packed into a single 64-bit word by NaN-boxing.