Expand description
§Scheme Parser
An R7RS-compliant Scheme parser with arena-allocated values.
§Design
- Symbols use contiguous string storage for memory efficiency
- Symbol interning ensures the same symbol name returns the same index
- All values are stored in a
grift_arenaarena - Supports garbage collection via the
Tracetrait - Explicit boolean values (#t, #f) separate from nil/empty list
- Strict evaluation - All arguments are evaluated before function application (call-by-value)
§Core Types
Core types (Value, Builtin, StdLib, Lisp) are defined in the
grift_core crate and re-exported here for backward compatibility.
§Value Representation
Nil- The empty list (NOT false!)True- Boolean true (#t)False- Boolean false (#f)Number(isize)- Integer numbersChar(char)- Single characterCons { car, cdr }- Pair/list cell with inline indicesSymbol(ArenaIndex)- Symbol pointing to interned stringLambda { params, body_env }- Closure with inline indicesBuiltin(Builtin)- Optimized built-in functionStdLib(StdLib)- Standard library function (static code, parsed on-demand)Array { len, data }- Vector with inline lengthString { len, data }- String with inline lengthNative { id }- Native Rust function reference
§Reserved Slots
The Lisp singleton values (nil, true, false) are pre-allocated in reserved slots at initialization time.
The first 4 slots of the arena are reserved:
- Slot 0:
Value::Nil- empty list singleton - Slot 1:
Value::True- boolean true singleton - Slot 2:
Value::False- boolean false singleton - Slot 3:
Value::Cons- intern table reference cell
§Pitfalls and Gotchas
§Truthiness
- Only
#fis false! Everything else is truthy, including:nil/'()(the empty list)0(the number zero)- Empty strings
§Garbage Collection
- The intern table is always a GC root - interned symbols are never collected
- Reserved slots (nil, true, false) are implicitly preserved
- Run
gc()with appropriate roots to reclaim memory
§StdLib Functions
- Body is parsed on each call (minor overhead, but keeps code out of arena)
- Recursive stdlib functions work via the global environment
- Errors in static source strings are only caught at runtime
Re-exports§
pub use lexer::Lexer;pub use lexer::Token;pub use lexer::SpannedToken;pub use lexer::LexError;pub use lexer::LexErrorKind;pub use parser::SourceLoc;
Modules§
Macros§
- define_
builtins - Macro for defining built-in functions.
- define_
stdlib - Macro for defining standard library functions.
Structs§
- Arena
- Fixed-size arena allocator with O(1) allocation.
- Arena
Index - Index into the arena.
- Display
Port - Write a
Valuethrough anIoProviderport. - Display
Value - A wrapper that enables
core::fmt::Displayfor Lisp values. - GcStats
- Statistics returned by garbage collection.
- Lisp
- A Lisp execution context wrapping an arena
- Null
IoProvider - A no-op I/O provider that silently discards all output and returns
IoErrorKind::Unsupportedfor reads. - Parse
Error - Parser error with location
- Parser
- Parser state — wraps a
Lexerand builds arena-allocated AST nodes. - PortId
- Identifies an I/O port.
Enums§
- Arena
Error - Errors that can occur during arena operations.
- Builtin
- Built-in functions (optimization to avoid symbol lookup)
- IoError
Kind - Error kinds for I/O operations in
no_stdenvironments. - Parse
Error Kind - StdLib
- Standard library functions (stored in static memory, not arena)
- Value
- A Lisp value
Constants§
- PRELUDE_
SOURCE - The combined prelude source containing all macro and function definitions.
- RESERVED_
SLOTS - Number of reserved slots in the arena:
Traits§
- IoProvider
- Trait for providing I/O operations to the evaluator.
- Trace
- Trait for types that can be traced by the garbage collector.
Functions§
Type Aliases§
- Arena
Result - Result type for arena operations.
- IoResult
- Result type for I/O operations.
- fsize
- Platform-dependent floating-point type, matching the width of
isize/usize.