Expand description
General-purpose Lisp codec for the SIM runtime: the s-expression surface
that round-trips every expression through the shared Expr graph.
A decoder lexes and reads parenthesized s-expression text into checked
Expr forms; an encoder serializes any Expr back to Lisp text aware of
its output position (eval, quote, data, pattern). Because the codec covers
the full expression graph rather than a single domain, it can faithfully
represent any value the kernel can hold.
§Module map
The crate’s behavior lives behind the private implementation module, whose
public items are re-exported at the crate root. Internally that module
aggregates: lex (tokenizing Lisp source into tokens and trivia), tree
(reading a token stream into a located expression tree), decode
(the Decoder/TreeDecoder/LocatedDecoder entry points and surface
lowering), forms (parsing individual atoms, literals, symbols, logic
variables, and quote forms), encode (the Encoder/TreeEncoder rendering
of Expr back to text), and runtime (the Lib registration wiring the
codec into the runtime). RECIPES exposes the embedded cookbook recipes.
§Examples
Register the codec, decode s-expression text into an Expr, then encode an
Expr back to Lisp text:
use std::sync::Arc;
use sim_codec::{Input, decode_with_codec, encode_with_codec};
use sim_codec_lisp::LispCodecLib;
use sim_kernel::{
Cx, DefaultFactory, EagerPolicy, Expr, ReadPolicy, Symbol,
};
let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
sim_test_support::register_core_classes(&mut cx);
sim_test_support::register_f64_number_domain(&mut cx);
let lib = LispCodecLib::new(cx.registry_mut().fresh_codec_id())?;
cx.load_lib(&lib)?;
let lisp = Symbol::qualified("codec", "lisp");
// Decode text into a checked `Expr` form.
let expr = decode_with_codec(
&mut cx,
&lisp,
Input::Text("(quote [1 2])".to_owned()),
ReadPolicy::default(),
)?;
assert!(matches!(expr, Expr::Quote { .. }));
// Encode the `Expr` back to Lisp text (a semantic round-trip).
let text = encode_with_codec(&mut cx, &lisp, &expr, Default::default())?
.into_text()
.unwrap();
assert_eq!(text, "(quote [1 2])");The loadable codec lib also exports cli/main/codec-lisp. That entrypoint
accepts the standard CLI envelope table, evaluates exactly one source from
eval, script, or stdin through the active context eval policy, and
returns a cli/repl marker for a bare handoff.
Structs§
- Lisp
Codec Lib Libthat registers the Lisp codec with the runtime.- Lisp
Proc Macro Decoder - Lisp decoder built on
proc-macro2tokenization. - Lisp
Proc Macro Encoder - Lisp encoder that renders expressions back to s-expression text.
Statics§
- RECIPES
- Cookbook recipes for the Lisp codec, embedded at build time from the crate’s
recipes/directory and exposed for help and browse surfaces.
Functions§
- decode_
lisp_ located - Decodes Lisp source into a
LocatedExpr, interning the source text undersource_idand attaching span origins so diagnostics can point back at it. - decode_
lisp_ tree - Decodes Lisp source into a
LocatedExprTree, preserving trivia and span information for every node so the tree can be re-encoded losslessly. - encode_
object_ lisp - Encodes a runtime
Valueto Lisp text via its object encoder, rendering constructor, tagged-data, and opaque object encodings as s-expression forms. - token_
stream_ type_ name - Returns the type name of the
proc-macro2token stream this codec accepts as pre-lexed input, used by the runtime to recognize token-stream decode inputs.