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
impl Env
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates an environment with no commands registered.
You can load commands by calling register.
Sourcepub fn register(
&mut self,
name: &Value,
arity: usize,
function: impl Fn(&mut Env, &mut [OwnedValue]) -> Result<OwnedValue, FlowChange> + 'static,
)
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.
Sourcepub fn eval(&mut self, s: &Value) -> Result<OwnedValue, FlowChange>
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.
Sourcepub fn set_or_create_var(&mut self, name: OwnedValue, value: OwnedValue)
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.
Sourcepub fn get_existing_var(&mut self, name: &Value) -> Option<OwnedValue>
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.