Struct Interpreter

Source
pub struct Interpreter<State> {
    pub state: State,
    /* private fields */
}
Expand description

The core interpreter object. Takes in a custom defined state that can be modified by built-ins.

Fields§

§state: State

Custom state for each application. Allows users to register built-ins that modify this state.

Implementations§

Source§

impl<State> Interpreter<State>

Source

pub fn reset_program(&mut self)

Resets the program counter and clears the program and program debug locations but preserves RAM and stack.

Source§

impl<State> Interpreter<State>

Source

pub fn format_code(code: &str, path: Option<PathBuf>) -> Result<String, Err>

Format a code file.

Examples found in repository?
examples/format_code.rs (line 36)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
fn main() {
    // This is just some ugly code to showcase the formatting.
    // Typically '.' is used as a noop op and helps with formatting.
    let code = r#"
    var 
    stuff 
    .

    1 stuff 
    set .

    stuff
    get 
    .


    : debug "" "" "" print-stack drop ;
    0 begin 1 + dup 2 == if 
            "hello" .  stuff get drop .  break
        end

        dup
        2 == if 
            "hello"
        else
            "world"
            drop
        end
    loop

    "world"
    
    "#;
    let code = Interpreter::<()>::format_code(code, None).unwrap();

    println!("{}", code);
}
Source

pub fn stringify_program(&self) -> String

Returns the program as a formatted string.

Source§

impl<State> Interpreter<State>

Source

pub fn new(state: State) -> Self

Create a new interpreter. Pass in the initial custom state for the interpreter.

Examples found in repository?
examples/add.rs (line 5)
3
4
5
6
7
8
9
fn main() {
    let code = "1 1 +";
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();

    println!("1 1 + ={}", interpreter.pop_number().unwrap());
}
More examples
Hide additional examples
examples/print_documentation.rs (line 8)
3
4
5
6
7
8
9
10
fn main() {
    println!("This is how you print documentation");
    let code = r#"
    documentation
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
examples/execute_address.rs (line 7)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    let code = r#"
    1 2 [ + ] @
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();

    println!(
        "1 2 [ + ] @  should return {}",
        interpreter.pop_number().unwrap()
    );
}
examples/custom_word.rs (line 15)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    let code = r#"
    : squared
        "n -- n^2"
        "Squares the top of the stack"
        "2 squared"
        dup *
    ;

    2 squared
    print-stack
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
examples/repl.rs (line 6)
3
4
5
6
7
8
9
10
11
fn main() {
    // TODO: figure out way to start repl in interpreter
    // let code = "repl \"example of repl\" print-stack";
    let mut interpreter = Interpreter::new(());
    // interpreter.evaluate(code, None).unwrap();

    // Alternative way to start repl in host
    interpreter.start_repl().unwrap();
}
examples/loop.rs (line 21)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    let code = r#"

    0
    begin
        print-stack
    
        dup 10 <=
        if
            drop
            "loop finished"
            print-stack
            break
        end
        1 + 
    loop
    
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
Source

pub fn evaluate(&mut self, code: &str, path: Option<PathBuf>) -> Result<(), Err>

Evaluate a program.

Examples found in repository?
examples/add.rs (line 6)
3
4
5
6
7
8
9
fn main() {
    let code = "1 1 +";
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();

    println!("1 1 + ={}", interpreter.pop_number().unwrap());
}
More examples
Hide additional examples
examples/print_documentation.rs (line 9)
3
4
5
6
7
8
9
10
fn main() {
    println!("This is how you print documentation");
    let code = r#"
    documentation
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
examples/execute_address.rs (line 8)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    let code = r#"
    1 2 [ + ] @
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();

    println!(
        "1 2 [ + ] @  should return {}",
        interpreter.pop_number().unwrap()
    );
}
examples/custom_word.rs (line 16)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
fn main() {
    let code = r#"
    : squared
        "n -- n^2"
        "Squares the top of the stack"
        "2 squared"
        dup *
    ;

    2 squared
    print-stack
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
examples/loop.rs (line 22)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    let code = r#"

    0
    begin
        print-stack
    
        dup 10 <=
        if
            drop
            "loop finished"
            print-stack
            break
        end
        1 + 
    loop
    
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
examples/if.rs (line 30)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    let code = r#"
    "Example of if"
    print-stack
    drop

    1 if 
        "evaluated when true!"
        print-stack
        drop
    end

    "Now we do an example of an else"
    print-stack
    drop

    0 if 
        "Not ran"
        print-stack
        drop
    else
        "else is ran"
        print-stack
        drop
    end
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();
}
Source

pub fn get_name(&self, address: Address) -> String

Get the name of an address if present.

Source

pub fn pop(&mut self) -> Result<StackValue, Err>

Pop a value from the stack.

Source

pub fn pop_bool(&mut self) -> Result<bool, Err>

Pop a boolean from the stack.

Source

pub fn pop_number(&mut self) -> Result<f32, Err>

Pop a number from the stack.

Examples found in repository?
examples/add.rs (line 8)
3
4
5
6
7
8
9
fn main() {
    let code = "1 1 +";
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();

    println!("1 1 + ={}", interpreter.pop_number().unwrap());
}
More examples
Hide additional examples
examples/execute_address.rs (line 12)
3
4
5
6
7
8
9
10
11
12
13
14
fn main() {
    let code = r#"
    1 2 [ + ] @
    "#;
    let mut interpreter = Interpreter::new(());
    interpreter.evaluate(code, None).unwrap();

    println!(
        "1 2 [ + ] @  should return {}",
        interpreter.pop_number().unwrap()
    );
}
Source

pub fn push_number(&mut self, number: f32)

Push a number onto the stack.

Examples found in repository?
examples/custom_builtin.rs (line 24)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
fn main() {
    let state: u32 = 0;
    let mut interpreter = Interpreter::new(state);

    interpreter.register_builtin(
        "increment-state",
        "--",
        "Increments the state.",
        "increment-state",
        |interpreter| {
            interpreter.state += 1;
            Ok(())
        },
    );

    interpreter.register_builtin(
        "get-state",
        "-- n",
        "Gets the state.",
        "get-state",
        |interpreter| {
            interpreter.push_number(interpreter.state as f32);
            Ok(())
        },
    );

    let code = r#"
    print-stack
    increment-state
    get-state
    "The state has been modified!"
    print-stack
    "#;

    println!("State before execution: {:?}", interpreter.state);
    interpreter.evaluate(code, None).unwrap();
    println!("State after execution: {:?}", interpreter.state);
}
Source

pub fn push_address(&mut self, address: Address)

Push an address onto the stack.

Source

pub fn pop_address(&mut self) -> Result<Address, Err>

Pop an address from the stack.

Source

pub fn push_string(&mut self, string: String)

Push a string onto the stack.

Source

pub fn pop_string(&mut self) -> Result<String, Err>

Pop a string from the stack.

Source

pub fn start_repl(&mut self) -> Result<(), Err>

Start a REPL.

Examples found in repository?
examples/repl.rs (line 10)
3
4
5
6
7
8
9
10
11
fn main() {
    // TODO: figure out way to start repl in interpreter
    // let code = "repl \"example of repl\" print-stack";
    let mut interpreter = Interpreter::new(());
    // interpreter.evaluate(code, None).unwrap();

    // Alternative way to start repl in host
    interpreter.start_repl().unwrap();
}
Source

pub fn register_builtin( &mut self, name: &str, stack_modification: &str, documentation: &str, example: &str, func: BuiltIn<State>, )

Register a built-in function to be used in the interpreter.

Examples found in repository?
examples/custom_error.rs (lines 7-16)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let state: u32 = 0;
    let mut interpreter = Interpreter::new(state);

    interpreter.register_builtin(
        "example-error",
        "-- n",
        "returns an error",
        "get-state",
        |interpreter| {
            //
            Err(("This is an error".to_string(), interpreter.location()))
        },
    );

    let code = r#"
    example-error
    "#;

    let result = interpreter.evaluate(code, None);
    println!("Result: {:?}", result);
}
More examples
Hide additional examples
examples/custom_builtin.rs (lines 7-16)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
fn main() {
    let state: u32 = 0;
    let mut interpreter = Interpreter::new(state);

    interpreter.register_builtin(
        "increment-state",
        "--",
        "Increments the state.",
        "increment-state",
        |interpreter| {
            interpreter.state += 1;
            Ok(())
        },
    );

    interpreter.register_builtin(
        "get-state",
        "-- n",
        "Gets the state.",
        "get-state",
        |interpreter| {
            interpreter.push_number(interpreter.state as f32);
            Ok(())
        },
    );

    let code = r#"
    print-stack
    increment-state
    get-state
    "The state has been modified!"
    print-stack
    "#;

    println!("State before execution: {:?}", interpreter.state);
    interpreter.evaluate(code, None).unwrap();
    println!("State after execution: {:?}", interpreter.state);
}
Source

pub fn print_documentation(&self)

Print the documentation for all words.

Source

pub fn location(&self) -> Location

Returns the location of the current instruction.

Examples found in repository?
examples/custom_error.rs (line 14)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    let state: u32 = 0;
    let mut interpreter = Interpreter::new(state);

    interpreter.register_builtin(
        "example-error",
        "-- n",
        "returns an error",
        "get-state",
        |interpreter| {
            //
            Err(("This is an error".to_string(), interpreter.location()))
        },
    );

    let code = r#"
    example-error
    "#;

    let result = interpreter.evaluate(code, None);
    println!("Result: {:?}", result);
}

Auto Trait Implementations§

§

impl<State> Freeze for Interpreter<State>
where State: Freeze,

§

impl<State> RefUnwindSafe for Interpreter<State>
where State: RefUnwindSafe,

§

impl<State> Send for Interpreter<State>
where State: Send,

§

impl<State> Sync for Interpreter<State>
where State: Sync,

§

impl<State> Unpin for Interpreter<State>
where State: Unpin,

§

impl<State> UnwindSafe for Interpreter<State>
where State: UnwindSafe,

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.