Skip to main content

ReplSession

Struct ReplSession 

Source
pub struct ReplSession { /* private fields */ }
Expand description

Persistent state for an interactive REPL session. Unlike the one-shot run / [Evaluator::run] pair, a ReplSession carries its scopes, fns, user types, method tables, and import cache across calls, so session.eval("let x = 5") followed by session.eval("print(x)") sees the same x. Persistent state for an interactive REPL session. Build once with Self::new, then call Self::eval for each fresh line / block the user types. State carried across calls: every let / const / fn / struct / enum / method declaration, every use’d module’s bindings and aliases, the global rng seed, and the import cache.

A ReplSession is not a sandboxing boundary — embedders that want a fresh identity between inputs should construct a new session. Resource limits (steps, memory) are supplied per eval call since they reset each time.

Implementations§

Source§

impl ReplSession

Source

pub fn new() -> Self

Build a fresh session. Type tables are pre-seeded with the engine builtins (Result, RuntimeError) so Result::Ok(...) resolves without an explicit use.

Source

pub fn get(&self, name: &str) -> Option<Value>

Look up a binding by name. Convenience for tests and embedders that want to peek at the session’s state between eval calls — e.g. checking that let x = 5 actually stuck.

Source

pub fn binding_names(&self) -> Vec<String>

Every currently-bound name in the root scope, sorted. Handy for REPL introspection commands (:vars) and for tab-completers that want to stay honest about what’s actually in scope rather than guessing from observed tokens.

Source

pub fn eval<H: BopHost>( &mut self, source: &str, host: &mut H, limits: &BopLimits, ) -> Result<Option<Value>, BopError>

Parse source and run its statements against this session’s accumulated state.

If the last statement is a bare expression (ExprStmt), the session evaluates it and returns its value as Ok(Some(v)) so the REPL can echo the result. Every other shape — let x = ..., fn foo, struct, use, loops — returns Ok(None).

Partial failure semantics: if an earlier statement errors, the session’s state reflects whatever ran before the failure. That matches what a user would expect from an interactive prompt.

Source

pub fn run_stmts<H: BopHost>( &mut self, stmts: &[Stmt], host: &mut H, limits: &BopLimits, ) -> Result<Option<Value>, BopError>

Like Self::eval but takes an already-parsed AST. Useful when the caller has already run a parse_with_warnings pass and wants to surface warnings before executing.

Trait Implementations§

Source§

impl Default for ReplSession

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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.