Interpreter

Struct Interpreter 

Source
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

Source

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}
Source

pub fn intern_atom(&mut self, text: &str) -> Rc<str>

Source

pub fn set_pending_doc_target(&mut self, atom: Rc<str>)

Source

pub fn take_pending_doc_target(&mut self) -> Option<Rc<str>>

Source

pub fn attach_doc( &mut self, atom: &Rc<str>, doc: Rc<str>, ) -> Result<(), RuntimeError>

Source

pub fn push(&mut self, value: Value)

Source

pub fn pop(&mut self) -> Result<Value, RuntimeError>

Source

pub fn pop_number(&mut self) -> Result<f64, RuntimeError>

Source

pub fn pop_integer(&mut self) -> Result<usize, RuntimeError>

Source

pub fn make_list(&self, items: Vec<Value>) -> Value

Source

pub fn make_array(&self, items: Vec<Value>) -> Value

Source

pub fn is_null(&self, value: &Value) -> bool

Source

pub fn is_truthy(&self, value: &Value) -> bool

Source

pub fn push_return(&mut self, value: Value)

Source

pub fn pop_return(&mut self) -> Result<Value, RuntimeError>

Source

pub fn peek_return(&self) -> Result<&Value, RuntimeError>

Source

pub fn pop_with_context(&mut self, context: &str) -> Result<Value, RuntimeError>

Source

pub fn set_output(&mut self, output: Box<dyn Output>)

Source

pub fn has_output(&self) -> bool

Source

pub fn set_time_source(&mut self, time_source: Box<dyn TimeSource>)

Source

pub fn has_time_source(&self) -> bool

Source

pub fn writeln(&mut self, text: &str) -> Result<(), ()>

Source

pub fn write_str(&mut self, text: &str) -> Result<(), ()>

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.