pub struct TuringMachine {
pub instructions: HashMap<(String, bool), TuringInstruction>,
pub final_states: Vec<String>,
pub current_state: String,
pub previous_state: Option<String>,
pub tape_position: usize,
pub tape: Vec<bool>,
pub frequencies: HashMap<String, usize>,
pub description: Option<String>,
pub composed_libs: Vec<Library>,
pub code: String,
}
Expand description
A Turing machine
Fields§
§instructions: HashMap<(String, bool), TuringInstruction>
The dictionary of instructions for the machine.
final_states: Vec<String>
The final states of the machine. If the machine reaches one of these states, it will stop.
current_state: String
The current state of the machine.
previous_state: Option<String>
The previous state of the machine.
tape_position: usize
The position of the head on the tape.
tape: Vec<bool>
The binary tape of the machine.
frequencies: HashMap<String, usize>
The frequencies of the states. Used to detect infinite loops.
description: Option<String>
The description of the machine. Found in the ///
comments at the top of the file.
composed_libs: Vec<Library>
The composed libraries that the machine uses. Used only as information, since their instructions are already compiled into the machine.
code: String
The actual code of the machine. Used for resetting the machine and debugging.
Implementations§
Source§impl TuringMachine
impl TuringMachine
Sourcepub fn new(code: &str) -> Result<(Self, Vec<CompilerWarning>), CompilerError>
pub fn new(code: &str) -> Result<(Self, Vec<CompilerWarning>), CompilerError>
Create a new Turing machine from a string of code
Sourcepub fn handle_error(error: CompilerError)
pub fn handle_error(error: CompilerError)
Parse a Turing machine code syntax error and print it to the console
Sourcepub fn get_current_instruction(&self) -> Option<TuringInstruction>
pub fn get_current_instruction(&self) -> Option<TuringInstruction>
Gets the current instruction
Sourcepub fn is_undefined(&self) -> bool
pub fn is_undefined(&self) -> bool
Returns true if the current state is undefined (i.e. there is no instruction for the current state and value) except if the current state is a final state
Sourcepub fn step(&mut self) -> bool
pub fn step(&mut self) -> bool
Calculates the next step of the Turing machine and returns true if the current state is a final state
Sourcepub fn is_infinite_loop(&self, threshold: usize) -> bool
pub fn is_infinite_loop(&self, threshold: usize) -> bool
Returns true if the current state has been reached more times than the given threshold
Sourcepub fn reset_frequencies(&mut self)
pub fn reset_frequencies(&mut self)
Resets the frequencies of the states
Sourcepub fn finished(&self) -> bool
pub fn finished(&self) -> bool
Returns true if the current state is a final state and the motion is to Halt
Sourcepub fn values(&self) -> Vec<u32>
pub fn values(&self) -> Vec<u32>
Returns the values of the tape (i.e. the number of 1s between each 0)
Sourcepub fn tape_value(&self) -> TuringOutput
pub fn tape_value(&self) -> TuringOutput
Returns the current output of the Turing machine (i.e. the number of steps and the number of 1s on the tape, or undefined if the Turing machine is in an undefined state)
Sourcepub fn final_result(&mut self) -> TuringOutput
pub fn final_result(&mut self) -> TuringOutput
Returns the final output of the Turing machine directly (i.e. keeps calculating the next step until the current state is a final state)
Trait Implementations§
Source§impl Clone for TuringMachine
impl Clone for TuringMachine
Source§fn clone(&self) -> TuringMachine
fn clone(&self) -> TuringMachine
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more