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
impl Instruction
Sourcepub const fn from_char(c: char) -> Self
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.
Symbol | Effect |
---|---|
+ | 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 theBrainFuck
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 NoOp
s
Trait Implementations§
Source§impl Clone for Instruction
impl Clone for Instruction
Source§fn clone(&self) -> Instruction
fn clone(&self) -> Instruction
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Debug for Instruction
impl Debug for Instruction
Source§impl Display for Instruction
Convert an instruction to a String
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.