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>
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>
Sourcepub fn format_code(code: &str, path: Option<PathBuf>) -> Result<String, Err>
pub fn format_code(code: &str, path: Option<PathBuf>) -> Result<String, Err>
Format a code file.
Examples found in repository?
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);
}
Sourcepub fn stringify_program(&self) -> String
pub fn stringify_program(&self) -> String
Returns the program as a formatted string.
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
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()
);
}
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();
}
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();
}
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
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()
);
}
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();
}
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();
}
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();
}
Sourcepub fn pop(&mut self) -> Result<StackValue, Err>
pub fn pop(&mut self) -> Result<StackValue, Err>
Pop a value from the stack.
Sourcepub fn pop_number(&mut self) -> Result<f32, Err>
pub fn pop_number(&mut self) -> Result<f32, Err>
Pop a number from the stack.
Examples found in repository?
More examples
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()
);
}
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?
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?
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
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.
Sourcepub fn location(&self) -> Location
pub fn location(&self) -> Location
Returns the location of the current instruction.
Examples found in repository?
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);
}