Add scope command to repl.
This commit is contained in:
parent
d5eb2887ff
commit
ab60c2e1d2
@ -48,6 +48,7 @@ fn print_error(input: &str, err: EvalAltResult) {
|
|||||||
fn print_help() {
|
fn print_help() {
|
||||||
println!("help => print this help");
|
println!("help => print this help");
|
||||||
println!("quit, exit => quit");
|
println!("quit, exit => quit");
|
||||||
|
println!("scope => print all variables in the scope");
|
||||||
println!("ast => print the last AST");
|
println!("ast => print the last AST");
|
||||||
println!("astu => print the last raw, un-optimized AST");
|
println!("astu => print the last raw, un-optimized AST");
|
||||||
println!(r"end a line with '\' to continue to the next line.");
|
println!(r"end a line with '\' to continue to the next line.");
|
||||||
@ -110,6 +111,13 @@ fn main() {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
"exit" | "quit" => break, // quit
|
"exit" | "quit" => break, // quit
|
||||||
|
"scope" => {
|
||||||
|
scope
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.for_each(|(i, (name, value))| println!("[{}] {} = {:?}", i + 1, name, value));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
"astu" => {
|
"astu" => {
|
||||||
// print the last un-optimized AST
|
// print the last un-optimized AST
|
||||||
println!("{:#?}", &ast_u);
|
println!("{:#?}", &ast_u);
|
||||||
|
30
src/scope.rs
30
src/scope.rs
@ -444,9 +444,37 @@ impl<'a> Scope<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Get an iterator to entries in the Scope.
|
/// Get an iterator to entries in the Scope.
|
||||||
pub(crate) fn iter(&self) -> impl Iterator<Item = &Entry> {
|
pub(crate) fn to_iter(&self) -> impl Iterator<Item = &Entry> {
|
||||||
self.0.iter().rev() // Always search a Scope in reverse order
|
self.0.iter().rev() // Always search a Scope in reverse order
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get an iterator to entries in the Scope.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use rhai::{Dynamic, Scope};
|
||||||
|
///
|
||||||
|
/// let mut my_scope = Scope::new();
|
||||||
|
///
|
||||||
|
/// my_scope.push("x", 42_i64);
|
||||||
|
/// my_scope.push("foo", "hello".to_string());
|
||||||
|
///
|
||||||
|
/// let mut iter = my_scope.iter();
|
||||||
|
///
|
||||||
|
/// let (name, value) = iter.next().unwrap();
|
||||||
|
/// assert_eq!(name, "x");
|
||||||
|
/// assert_eq!(value.clone().cast::<i64>(), 42);
|
||||||
|
///
|
||||||
|
/// let (name, value) = iter.next().unwrap();
|
||||||
|
/// assert_eq!(name, "foo");
|
||||||
|
/// assert_eq!(value.clone().cast::<String>(), "hello");
|
||||||
|
/// ```
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
|
||||||
|
self.0
|
||||||
|
.iter()
|
||||||
|
.map(|Entry { name, value, .. }| (name.as_ref(), value))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, K: Into<Cow<'a, str>>> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a> {
|
impl<'a, K: Into<Cow<'a, str>>> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a> {
|
||||||
|
Loading…
Reference in New Issue
Block a user