Expand description
§Grift
A minimal no_std, no_alloc R7RS-compliant Scheme implementation built on
arena-based garbage collection. Perfect for embedded systems, WebAssembly, or any
environment where heap allocation is unavailable or undesirable.
§Features
no_std,no_allocby default — Runs on bare metal- Arena-based allocation — Fixed memory footprint, no heap
- Mark-and-sweep GC — Controllable from Scheme code
- Proper tail-call optimization — Via trampolining
- Lexical closures — First-class functions with captured environments
- R7RS-compliant — Scheme semantics following the Revised⁷ Report
- Pluggable I/O — Trait-based I/O boundary (
IoProvider) keeps the evaluatorno_stdwhile allowing real I/O on hosted platforms - Native FFI — Register Rust functions callable from Scheme
- Embedded support — Optional hardware natives for GPIO, memory peek/poke, and bit manipulation
§Quick Start
use grift::{Lisp, Evaluator, Value};
// Create a Lisp interpreter with a 20,000-cell arena
let lisp: Lisp<20000> = Lisp::new();
let mut eval = Evaluator::new(&lisp).unwrap();
// Evaluate expressions
let result = eval.eval_str("(+ 1 2 3)").unwrap();
assert!(matches!(lisp.get(result), Ok(Value::Number(6))));§Optional std Feature
Enable the std feature for an interactive REPL and real I/O:
[dependencies]
grift = { version = "1.3", features = ["std"] }The std feature brings in:
grift_repl— Interactive REPL with line editing, GC commands, and helpgrift_std—StdIoProvider, anIoProviderimplementation usingstd::iofor stdin/stdout/stderr and dynamic string ports
Then run:
cargo install grift --features std
grift§I/O Architecture
The evaluator is fully no_std and performs no I/O itself. Instead, an
IoProvider trait (defined in grift_core) abstracts all port
operations:
NullIoProvider— No-op implementation that silently discards output and rejects reads. Used inno_std/embedded contexts where there is no real I/O.StdIoProvider(behindstdfeature) — Full implementation backed bystd::io, providing stdin/stdout/stderr and dynamic string ports.
Pass any IoProvider to the evaluator at runtime:
ⓘ
use grift::{Lisp, Evaluator, NullIoProvider};
let lisp: Lisp<20000> = Lisp::new();
let mut eval = Evaluator::new(&lisp).unwrap();
let mut io = NullIoProvider;
eval.set_io_provider(&mut io);§Crate Organization
This crate re-exports the complete Grift stack:
grift_arena— Fixed-size arena allocator with mark-and-sweep GCgrift_core— Core types (Value,Builtin,StdLib,Lisp) and theIoProvidertrait boundarygrift_parser— Lexer and parsergrift_eval— Fully-trampolined evaluator with native FFIgrift_repl— Interactive REPL (behindstdfeature)grift_std—StdIoProviderfor hosted I/O (behindstdfeature)- [
grift_macros] — Procedural macros (include_stdlib!) - [
grift_util] — Shared utilities (Scheme-name ↔ Rust-name conversion) - [
grift_arena_embedded] — Embedded hardware natives (GPIO, peek/poke, bit manipulation)
Modules§
- arena
- Arena allocator and garbage collection primitives.
- core_
types - Core value types and Lisp context.
- eval
- Evaluator, error handling, and native function interop.
- parser
- Parser, lexer, value types, and built-in definitions.
- repl
std - REPL and formatting utilities (requires
stdfeature). - std_io
std - Standard library I/O provider (requires
stdfeature).
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. - Eval
Error - Evaluation error with context - optimized for minimal size.
- Evaluator
- The Lisp evaluator with full trampolined TCO.
- GcStats
- Statistics returned by garbage collection.
- LexError
- Lexer error with location
- Lexer
- A standalone lexer that produces tokens on demand without heap allocation.
- Lisp
- A Lisp execution context wrapping an arena
- Native
Entry - A registered native function with its name.
- Native
Registry - Registry for native functions.
- 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.
- Repl
std - The REPL structure (for API convenience)
- Source
Loc - Source location for error reporting
- Spanned
Token - A token with its source location
- Stack
Frame - A stack frame for error reporting
- StdIo
Provider std - An
IoProviderimplementation backed by Rust’s standard I/O.
Enums§
- Arena
Error - Errors that can occur during arena operations.
- Builtin
- Built-in functions (optimization to avoid symbol lookup)
- Error
Kind - Error kind enumeration
- IoError
Kind - Error kinds for I/O operations in
no_stdenvironments. - LexError
Kind - Lexer error kinds
- Parse
Error Kind - StdLib
- Standard library functions (stored in static memory, not arena)
- Token
- A token produced by the lexer.
- Value
- A Lisp value
Constants§
- MAX_
NATIVE_ FUNCTIONS - Maximum number of native functions that can be registered.
Traits§
- From
Lisp - Trait for converting Lisp values to Rust types.
- IoProvider
- Trait for providing I/O operations to the evaluator.
- ToLisp
- Trait for converting Rust types to Lisp values.
- Trace
- Trait for types that can be traced by the garbage collector.
Functions§
- args_
empty - Check if the argument list is empty (nil).
- count_
args - Count the number of arguments in a list.
- eval_
to_ string std - Evaluate a string and return the result as a string
- extract_
arg - Extract a single argument from a Lisp argument list.
- format_
error std - Format an evaluation error with full context
- format_
value std - Format a Lisp value as a string
- parse
- Parse a string into a Lisp expression
- parse_
all - Parse multiple expressions
- run_
repl std - Run a REPL session
- value_
to_ string std - Convert a Lisp value to a string
Type Aliases§
- Arena
Result - Result type for arena operations.
- Eval
Result - Result type for evaluation
- IoResult
- Result type for I/O operations.
- Native
Fn - A native function that can be called from Lisp.