Struct TuringMachine

Source
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

Source

pub fn new(code: &str) -> Result<(Self, Vec<CompilerWarning>), CompilerError>

Create a new Turing machine from a string of code

Source

pub fn none() -> Self

Create a new empty Turing machine

Source

pub fn handle_error(error: CompilerError)

Parse a Turing machine code syntax error and print it to the console

Source

pub fn get_current_instruction(&self) -> Option<TuringInstruction>

Gets the current instruction

Source

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

Source

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

Source

pub fn is_infinite_loop(&self, threshold: usize) -> bool

Returns true if the current state has been reached more times than the given threshold

Source

pub fn reset_frequencies(&mut self)

Resets the frequencies of the states

Source

pub fn finished(&self) -> bool

Returns true if the current state is a final state and the motion is to Halt

Source

pub fn values(&self) -> Vec<u32>

Returns the values of the tape (i.e. the number of 1s between each 0)

Source

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)

Source

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)

Source

pub fn get(&self, i: usize) -> Option<bool>

Returns the value of the tape at the given index, or None if the index is out of bounds

Trait Implementations§

Source§

impl Clone for TuringMachine

Source§

fn clone(&self) -> TuringMachine

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TuringMachine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for TuringMachine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.