Env

Struct Env 

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

An instance of the interpreter, the “Tcl machine.”

To run a program, you need one of these. You can create one containing the standard set of commands using Env::default(), and then call eval as many times as required.

Dropping it will deallocate all associated state.

Implementations§

Source§

impl Env

Source

pub fn empty() -> Self

Creates an environment with no commands registered.

You can load commands by calling register.

Source

pub fn register( &mut self, name: &Value, arity: usize, function: impl Fn(&mut Env, &mut [OwnedValue]) -> Result<OwnedValue, FlowChange> + 'static, )

Adds a command to self, under the name name, expecting arity arguments (including its own name!), and implemented by the Rust function function.

If a command can handle various numbers of arguments, the arity here should be 0, and the command is responsible for checking argument count itself.

Source

pub fn eval(&mut self, s: &Value) -> Result<OwnedValue, FlowChange>

Evaluates the source code s in terms of this interpreter.

On normal completion, returns the result. Otherwise, returns the change in flow control.

Source

pub fn set_or_create_var(&mut self, name: OwnedValue, value: OwnedValue)

Sets a variable named name to value in the current innermost scope, creating it if it doesn’t exist.

If the variable exists, name winds up being freed. This might seem like a good reason for it to be borrowed (&Value) instead. But in practice, the only case where it would be borrowed from a long-lived allocation is also the only case where we always create new bindings: in cmd_proc. So this keeps the code simpler at no performance cost.

Source

pub fn get_existing_var(&mut self, name: &Value) -> Option<OwnedValue>

Gets a copy of the contents of an existing variable, or returns None if it doesn’t exist.

Trait Implementations§

Source§

impl Default for Env

Source§

fn default() -> Self

Creates a new Env environment and initializes it with the standard bundled command set before returning it.

Auto Trait Implementations§

§

impl Freeze for Env

§

impl !RefUnwindSafe for Env

§

impl !Send for Env

§

impl !Sync for Env

§

impl Unpin for Env

§

impl !UnwindSafe for Env

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.