Skip to main content

Value

Enum Value 

Source
pub enum Value {
Show 14 variants Nil, True, False, Number(isize), Float(f64), Char(char), Cons { car: ArenaIndex, cdr: ArenaIndex, }, Symbol { chars: ArenaIndex, }, Lambda { data: ArenaIndex, }, Builtin(Builtin), StdLib { func: StdLib, cache: ArenaIndex, }, Array { data: ArenaIndex, len: usize, }, String { data: ArenaIndex, len: usize, }, Native { id: usize, name_hash: usize, },
}
Expand description

A Lisp value

Variants§

§

Nil

The empty list (NOT false - use False for that)

§

True

Boolean true (#t)

§

False

Boolean false (#f) - the ONLY false value

§

Number(isize)

Integer number (exact)

§

Float(f64)

Floating-point number (inexact)

§

Char(char)

Single character (used in strings and symbol storage)

§

Cons

Cons cell (pair)

Fields

§

Symbol

Symbol (contains a contiguous string) The chars field points to a Value::String which contains the symbol name. The length is obtained from the String value, providing a single source of truth.

Fields

§

Lambda

Lambda / closure (memory-optimized)

To minimize enum size, lambda data is stored as a linked list in the arena: data points to a cons cell (params . (body . env)) where:

  • params: List of parameter symbols
  • body: Expression to evaluate
  • env: Captured environment (alist)

Use Lisp::lambda() to create and Lisp::lambda_parts() to extract.

Fields

§

Builtin(Builtin)

Built-in function (optimized)

§

StdLib

Standard library function (stored in static memory with caching)

Unlike Lambda which stores code in the arena, StdLib references static function definitions. The function body is parsed on first call and cached, providing good performance while minimizing initial arena cost.

§Memory Efficiency

  • Function definitions are in static memory (const strings)
  • Parsed body and params are cached on first call
  • Subsequent calls reuse the cached parsed AST

§Cache Field

  • cache: NULL until first call, then points to (body . params) cons cell

Use Lisp::stdlib() to create and Lisp::stdlib_cache() to access cache.

Fields

§func: StdLib
§

Array

Array (contiguous storage of values in the arena)

Arrays store values contiguously in the arena, similar to how symbols store characters. This provides O(1) indexed access and mutation.

§Memory Layout

  • data points to the first element in contiguous storage
  • Elements are stored at data+0, data+1, …, data+(len-1)

§Example

(define arr (make-array 3 0))  ; Create array of 3 zeros
(array-set! arr 1 42)          ; Set index 1 to 42
(array-ref arr 1)              ; => 42
(array-length arr)             ; => 3

Fields

§len: usize
§

String

String (contiguous storage of Char values in the arena)

Strings store characters contiguously in the arena, similar to arrays. This provides O(1) indexed access and O(1) length lookup.

§Memory Layout

  • data points to the first character in contiguous storage
  • Characters are stored at data+0, data+1, …, data+(len-1)

§Example

(string-length "hello")   ; => 5
(string-ref "hello" 0)    ; => #\h

Fields

§len: usize
§

Native

Native function (Rust function callable from Lisp)

Native functions are registered at runtime and identified by their ID. The actual function pointer is stored in the evaluator’s NativeRegistry.

§Fields

  • id: Index into the NativeRegistry’s entries array
  • name_hash: Hash of the function name for quick comparison

Fields

§name_hash: usize

Implementations§

Source§

impl Value

Source

pub const fn is_nil(&self) -> bool

Check if this value is nil (empty list)

Source

pub const fn is_false(&self) -> bool

Check if this value is false (#f) This is the ONLY way to be false in this Lisp

Source

pub const fn is_true(&self) -> bool

Check if this value is true (#t)

Source

pub const fn is_boolean(&self) -> bool

Check if this value is a boolean (#t or #f)

Source

pub const fn is_atom(&self) -> bool

Check if this value is an atom (not a cons cell)

Source

pub const fn is_number(&self) -> bool

Check if this value is a number (integer or float)

Source

pub const fn is_integer(&self) -> bool

Check if this value is an integer

Source

pub const fn is_float(&self) -> bool

Check if this value is a float

Source

pub const fn is_symbol(&self) -> bool

Check if this value is a symbol

Source

pub const fn is_cons(&self) -> bool

Check if this value is a cons cell (pair)

Source

pub const fn is_lambda(&self) -> bool

Check if this value is a lambda

Source

pub const fn is_builtin(&self) -> bool

Check if this value is a builtin

Source

pub const fn is_stdlib(&self) -> bool

Check if this value is a stdlib function

Source

pub const fn is_native(&self) -> bool

Check if this value is a native (Rust) function

Source

pub const fn is_procedure(&self) -> bool

Check if this value is a procedure (lambda, builtin, stdlib, or native function)

Source

pub const fn is_array(&self) -> bool

Check if this value is an array

Source

pub const fn is_string(&self) -> bool

Check if this value is a string

Source

pub const fn as_number(&self) -> Option<isize>

Get the number value if this is an integer

Source

pub const fn as_float(&self) -> Option<f64>

Get the float value if this is a float

Source

pub const fn as_f64(&self) -> Option<f64>

Get any numeric value as f64

Source

pub const fn as_char(&self) -> Option<char>

Get the char value if this is a char

Source

pub const fn type_name(&self) -> &'static str

Get a human-readable type name

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize> Trace<Value, N> for Value

Implement Trace for GC support

Source§

fn trace<F>(&self, tracer: F)
where F: FnMut(ArenaIndex),

Trace all ArenaIndex references contained in this value. Read more
Source§

impl Copy for Value

Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.