pub struct Interpreter {
pub stack: Vec<Value>,
pub return_stack: Vec<Value>,
pub dictionary: HashMap<Rc<str>, DictEntry>,
pub atoms: HashMap<String, Rc<str>>,
pub local_frames: Vec<HashMap<Rc<str>, Value>>,
pub current_pos: Option<SourcePos>,
pub time_source: Option<Box<dyn TimeSource>>,
/* private fields */
}Fields§
§stack: Vec<Value>§return_stack: Vec<Value>§dictionary: HashMap<Rc<str>, DictEntry>§atoms: HashMap<String, Rc<str>>§local_frames: Vec<HashMap<Rc<str>, Value>>§current_pos: Option<SourcePos>§time_source: Option<Box<dyn TimeSource>>Implementations§
Source§impl Interpreter
impl Interpreter
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/simple_calculator.rs (line 10)
8fn main() {
9 // Create a new interpreter instance
10 let mut interp = Interpreter::new();
11
12 // Define some calculations to perform
13 let expressions = vec![
14 ("5 3 +", "Addition"),
15 ("10 4 -", "Subtraction"),
16 ("7 6 *", "Multiplication"),
17 ("20 4 /", "Division"),
18 ("3 dup *", "3 squared (3 * 3)"),
19 ("[1 2 3 4 5] length", "List length"),
20 ];
21
22 println!("Uni Calculator Example\n");
23
24 for (expr, description) in expressions {
25 // Execute the expression
26 match execute_string(expr, &mut interp) {
27 Ok(()) => {
28 // Get the result from the stack
29 if let Some(result) = interp.stack.last() {
30 println!("{}: {} = {}", description, expr, result);
31 }
32 }
33 Err(e) => {
34 eprintln!("Error evaluating '{}': {:?}", expr, e);
35 }
36 }
37
38 // Clear the stack for next calculation
39 interp.stack.clear();
40 }
41
42 println!("\nDefining and using a custom function:");
43
44 // Define a square function and use it
45 match execute_string("'square [dup *] def 5 square", &mut interp) {
46 Ok(()) => {
47 if let Some(result) = interp.stack.last() {
48 println!("5 squared = {}", result);
49 }
50 }
51 Err(e) => {
52 eprintln!("Error: {:?}", e);
53 }
54 }
55}pub fn intern_atom(&mut self, text: &str) -> Rc<str>
pub fn set_pending_doc_target(&mut self, atom: Rc<str>)
pub fn take_pending_doc_target(&mut self) -> Option<Rc<str>>
pub fn attach_doc( &mut self, atom: &Rc<str>, doc: Rc<str>, ) -> Result<(), RuntimeError>
pub fn push(&mut self, value: Value)
pub fn pop(&mut self) -> Result<Value, RuntimeError>
pub fn pop_number(&mut self) -> Result<f64, RuntimeError>
pub fn pop_integer(&mut self) -> Result<usize, RuntimeError>
pub fn make_list(&self, items: Vec<Value>) -> Value
pub fn make_array(&self, items: Vec<Value>) -> Value
pub fn is_null(&self, value: &Value) -> bool
pub fn is_truthy(&self, value: &Value) -> bool
pub fn push_return(&mut self, value: Value)
pub fn pop_return(&mut self) -> Result<Value, RuntimeError>
pub fn peek_return(&self) -> Result<&Value, RuntimeError>
pub fn pop_with_context(&mut self, context: &str) -> Result<Value, RuntimeError>
pub fn set_output(&mut self, output: Box<dyn Output>)
pub fn has_output(&self) -> bool
pub fn set_time_source(&mut self, time_source: Box<dyn TimeSource>)
pub fn has_time_source(&self) -> bool
pub fn writeln(&mut self, text: &str) -> Result<(), ()>
pub fn write_str(&mut self, text: &str) -> Result<(), ()>
Auto Trait Implementations§
impl Freeze for Interpreter
impl !RefUnwindSafe for Interpreter
impl !Send for Interpreter
impl !Sync for Interpreter
impl Unpin for Interpreter
impl !UnwindSafe for Interpreter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more