Crate stackr_rs

Source
Expand description

A stack-based interpreter written in Rust. Meant to be embedded in your application. Visit the repository for more information.

Basic usage:

use stackr_rs::*;

let code = "1 1 +";
let mut interpreter = Interpreter::new(());
interpreter.evaluate(code, None).unwrap();
println!("1 1 + ={}", interpreter.pop_number().unwrap());

See more built-in words.

Example with custom built-in words:

use stackr_rs::*;

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);

Example of a custom word:

: squared
"n -- n^2"
"Squares the top of the stack"
"2 squared"
dup *
;

2 squared
print-stack

See the examples directory for more examples or the README for more information.

Structs§

Address
Address in RAM.
Interpreter
The core interpreter object. Takes in a custom defined state that can be modified by built-ins.
Location
Location of a token/word in the source code.

Enums§

StackValue
Value
A value that is stored in RAM or on the stack.

Type Aliases§

BuiltIn
A type alias for the operation a built-in word performs.
Err
A type alias for an error.
Number
A type alias for a number.