pub struct Ctx { /* private fields */ }Expand description
The long-lived state of an evaluation, the equivalent of a JS realm: the
unique-id source for closure identities, the builtin module cache, the
environment free list, and the module loader — a resolver plus the cache of
modules loaded through it (load).
A host creates one per program run, per Python realm object or per
playground session, and passes &Ctx to every call. Imports are still
resolved before evaluation, and loaded modules are Rc-held values. See
docs/done/2026-10-02_elly-host.md.
Implementations§
Source§impl Ctx
impl Ctx
Sourcepub fn new() -> Self
pub fn new() -> Self
A context with a fresh id source and no module resolver, so every import fails.
Sourcepub fn with_resolver(resolver: Box<dyn ModuleResolver>) -> Self
pub fn with_resolver(resolver: Box<dyn ModuleResolver>) -> Self
A context whose imports are served by resolver.
Source§impl Ctx
impl Ctx
Sourcepub fn chunk(&self, id: ChunkId) -> Ref<'_, StaticChunk>
pub fn chunk(&self, id: ChunkId) -> Ref<'_, StaticChunk>
The chunk id of this realm.
Do not hold the reference across anything that may add a chunk: an evaluation, an application or a load.
Source§impl Ctx
impl Ctx
Sourcepub fn parse(&self, src: &str) -> Result<ChunkId, Error>
pub fn parse(&self, src: &str) -> Result<ChunkId, Error>
Parse Elly source into a new chunk named __eval_N, resolving its
names. Free variables cause ParseError::UnboundName.
Sourcepub fn parse_open(&self, src: &str) -> Result<ChunkId, Error>
pub fn parse_open(&self, src: &str) -> Result<ChunkId, Error>
Parse Elly source as an open term: a free name is bound into the root
frame, as if given. Used by tooling and the parse golden suite; for
evaluation, prefer parse.
Sourcepub fn parse_preluded(
&self,
src: &str,
prelude: &[Text],
) -> Result<ChunkId, Error>
pub fn parse_preluded( &self, src: &str, prelude: &[Text], ) -> Result<ChunkId, Error>
Parse Elly source resolved against a fixed outer frame (prelude):
prelude[i] is index i of the root frame. Unknown names cause
ParseError::UnboundName.
Source§impl Ctx
impl Ctx
Sourcepub fn take_trace(&self, raised: &Raised) -> Trace
pub fn take_trace(&self, raised: &Raised) -> Trace
The trace of raised, which reached the top of an evaluation of this
realm, taken out of the realm, and for a .no_match, what refuted
which value.
Sourcepub fn report_raised(
&self,
raised: &Raised,
trace: &Trace,
doc: &dyn Fn(&DocSource, &str) -> DocText,
style: ReportStyle,
) -> String
pub fn report_raised( &self, raised: &Raised, trace: &Trace, doc: &dyn Fn(&DocSource, &str) -> DocText, style: ReportStyle, ) -> String
Reports raised, which reached the top with trace: an uncaught
line, then what refuted which value and the calls it unwound through,
each shown in style. doc gives the name and, if it has it, the
text of a document, by its source and the name of its chunk; an
anonymous document is known by the chunk’s.
Source§impl Ctx
impl Ctx
Sourcepub fn compile_module(&self, src: &str) -> Result<ChunkId, Error>
pub fn compile_module(&self, src: &str) -> Result<ChunkId, Error>
Compile Elly module source into a new chunk, whose top module is the
module (see docs/done/2026-08-07_elly-modules-v0.md).
This is purely syntactic: bodies are resolved against the module’s
members and imports are mapped to leading frame slots. No bodies are
evaluated; instantiate makes an instance.
Sourcepub fn compile_module_framed(
&self,
src: &str,
frame: &[Text],
) -> Result<ChunkId, Error>
pub fn compile_module_framed( &self, src: &str, frame: &[Text], ) -> Result<ChunkId, Error>
Compile Elly module source against a frame of ordered names.
References to frame names resolve to de Bruijn locals. Lookup order: enclosing binders $\to$ imports $\to$ module items $\to$ frame. Module imports occupy the leading frame slots.
Sourcepub fn compile_module_named(
&self,
src: &str,
frame: &[Text],
name: Option<ModuleName>,
) -> Result<ChunkId, Error>
pub fn compile_module_named( &self, src: &str, frame: &[Text], name: Option<ModuleName>, ) -> Result<ChunkId, Error>
Same as compile_module_framed, but records
the canonical source name used by __Mod.name and for instance
deduplication. The chunk takes the name, or __module_N if there is none.
Source§impl Ctx
impl Ctx
Sourcepub fn load(&self, spec: &str) -> Result<Value, LoadError>
pub fn load(&self, spec: &str) -> Result<Value, LoadError>
Loads a module and its import graph through this context’s resolver,
returning the instance for spec. A module already in the cache is
reused, so two loads that reach one module share its instance.
Without a resolver, every spec is LoadError::NotFound.
Loading is not re-entrant: nothing evaluates while a graph is instantiated, so no host callback can run a nested load.
Source§impl Ctx
impl Ctx
Sourcepub fn chunk_bases(&self) -> Vec<Loc>
pub fn chunk_bases(&self) -> Vec<Loc>
Each chunk’s base in the realm’s offset space, in chunk order. Bases do not decrease; a chunk with no documents shares its base with the next.
Sourcepub fn ctx_span(&self, chunk: ChunkId, span: ChunkSpan) -> Option<CtxSpan>
pub fn ctx_span(&self, chunk: ChunkId, span: ChunkSpan) -> Option<CtxSpan>
Chunk chunk’s span span in the realm’s offset space.
Sourcepub fn node_span(&self, chunk: ChunkId, id: NodeId) -> Option<CtxSpan>
pub fn node_span(&self, chunk: ChunkId, id: NodeId) -> Option<CtxSpan>
The span of node id of chunk chunk, if it was parsed.
Sourcepub fn pat_span(&self, chunk: ChunkId, id: PatId) -> Option<CtxSpan>
pub fn pat_span(&self, chunk: ChunkId, id: PatId) -> Option<CtxSpan>
The span of pattern id of chunk chunk, if it was parsed.
Sourcepub fn lambda_span(&self, chunk: ChunkId, id: LambdaId) -> Option<CtxSpan>
pub fn lambda_span(&self, chunk: ChunkId, id: LambdaId) -> Option<CtxSpan>
The span of lambda id of chunk chunk, if it was parsed.
Sourcepub fn member_span(
&self,
chunk: ChunkId,
m: ModId,
index: u32,
) -> Option<CtxSpan>
pub fn member_span( &self, chunk: ChunkId, m: ModId, index: u32, ) -> Option<CtxSpan>
The span of member index of module m of chunk chunk, if it was
parsed (see StaticChunk::member_span).