Enum brainfoamkit_lib::Instruction

source ·
pub enum Instruction {
    IncrementPointer,
    DecrementPointer,
    IncrementValue,
    DecrementValue,
    OutputValue,
    InputValue,
    JumpForward,
    JumpBackward,
    NoOp,
}
Expand description

All possible instructions that can be understood by the interpreter

This enum is at the heart of the interpreter. This enumerates all eight instructions that a brainfuck program can be composed of in addition to a ninth No-Op instruction

§Examples

use brainfoamkit_lib::Instruction;

let incrptr = Instruction::from_char('>');
let decrptr = Instruction::from_char('<');

assert_eq!(incrptr, Instruction::IncrementPointer);
assert_eq!(decrptr, Instruction::DecrementPointer);

Variants§

§

IncrementPointer

Instruction to Increment the Pointer

Internal representation of the > instruction.

§

DecrementPointer

Instruction to Decrement the Pointer

Internal representation of the < instruction.

§

IncrementValue

Instruction to Increment the Value stored in memory location

Internal representation of the + instruction.

§

DecrementValue

Instruction to Increment the Value stored in memory location

Internal representation of the - instruction.

§

OutputValue

Instruction to Output the currently stored value to the external interface

Internal representation of the . instruction.

§

InputValue

Instruction to Input the currently available value at the external interface

Internal representation of the , instruction.

§

JumpForward

Instruction to Start a loop if the current value is non-zero

Internal representation of the [ instruction.

§

JumpBackward

Instruction to Restart a loop if the current value is non-zero

Internal representation of the ] instruction.

§

NoOp

Instruction to do nothing

This does not have a corresponding instruction in BrainFuck

Implementations§

source§

impl Instruction

source

pub const fn from_char(c: char) -> Self

Convert a char to an Instruction

This method takes in a a single instruction (character and converts that into the instruction.

This ignores any instructions not in the standard alphabet of BrainFuck and counts them as No-Ops

The following table and associated github repository show the entire syntax.

SymbolEffect
+Increment the value in the current memory cell
-Decrement the value in the current memory cell
>Move the memory pointer to the right
<Move the memory pointer to the left
[Begin a loop: continue if value in memory cell is nonzero
]End a loop: jump back to corresponding [ if value in memory cell is nonzero
.Output the ASCII value in the current memory cell
,Read a single ASCII character and store it in the current memory cell
_Everything else is a No-Up
§Argument
  • c - A single character from the BrainFuck list of command characters.
§Examples
use brainfoamkit_lib::Instruction;

let instruction = Instruction::from_char('+');

assert_eq!(instruction, Instruction::IncrementValue)
§Returns

The appropriate variant of the Instruction enum

§Notes

This version of Instruction treats every character other than the eight specific characters as NoOps

Trait Implementations§

source§

impl Clone for Instruction

source§

fn clone(&self) -> Instruction

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 Instruction

source§

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

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

impl Display for Instruction

Convert an instruction to a String

This method converts a given instruction to a human-readable instruction.

§Examples

use brainfoamkit_lib::{
    Instruction,
    Nybble,
};

let instruction = Instruction::from_char('>');

assert_eq!(instruction.to_string(), "INCPTR");

§Returns

The instruction as a string.

§See Also

  • from_char(): Creates a new Instruction from a string.
source§

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

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

impl PartialEq for Instruction

source§

fn eq(&self, other: &Instruction) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Instruction

source§

impl Eq for Instruction

source§

impl StructuralPartialEq for Instruction

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: Copy,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

§

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§

default 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>,

§

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>,

§

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.