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: StateCustom state for each application. Allows users to register built-ins that modify this state.
Implementations§
Source§impl<State> Interpreter<State>
impl<State> Interpreter<State>
Sourcepub fn reset_program(&mut self)
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>
impl<State> Interpreter<State>
Source§impl<State> Interpreter<State>
impl<State> Interpreter<State>
Sourcepub fn new(state: State) -> Self
pub fn new(state: State) -> Self
Create a new interpreter. Pass in the initial custom state for the interpreter.
Examples found in repository?
More examples
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/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);
}examples/if.rs (line 29)
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);
}Additional examples can be found in:
Sourcepub fn evaluate(&mut self, code: &str, path: Option<PathBuf>) -> Result<(), Err>
pub fn evaluate(&mut self, code: &str, path: Option<PathBuf>) -> Result<(), Err>
Evaluate a program.
Examples found in repository?
More examples
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);
}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);
}examples/custom_builtin.rs (line 38)
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);
}Sourcepub fn pop_number(&mut self) -> Result<f32, Err>
pub fn pop_number(&mut self) -> Result<f32, Err>
Pop a number from the stack.
Sourcepub fn push_number(&mut self, number: f32)
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);
}Sourcepub fn push_address(&mut self, address: Address)
pub fn push_address(&mut self, address: Address)
Push an address onto the stack.
Sourcepub fn pop_address(&mut self) -> Result<Address, Err>
pub fn pop_address(&mut self) -> Result<Address, Err>
Pop an address from the stack.
Sourcepub fn push_string(&mut self, string: String)
pub fn push_string(&mut self, string: String)
Push a string onto the stack.
Sourcepub fn pop_string(&mut self) -> Result<String, Err>
pub fn pop_string(&mut self) -> Result<String, Err>
Pop a string from the stack.
Sourcepub fn register_builtin(
&mut self,
name: &str,
stack_modification: &str,
documentation: &str,
example: &str,
func: BuiltIn<State>,
)
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_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);
}Sourcepub fn print_documentation(&self)
pub fn print_documentation(&self)
Print the documentation for all words.
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> 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