1
 2
 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

//! Holds support for the `Instruction` enum

use std::fmt;


/// Provides an interface for different instructions.
/// Essentially a front end enum for this mapping: 
/// - IncPtr  => '>'
/// - DecPtr  => '<'
/// - IncVal  => '+'
/// - DecVal  => '-'
/// - Output  => '.'
/// - Input   => ','
/// - SetJump => '['
/// - Jump    => ']'
/// - EOF     => ';'
/// - Debug   => '#'
/// - NOP     => Any other character
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Instruction {
    IncPtr,
    DecPtr,
    IncVal,
    DecVal,
    Output,
    Input,
    SetJump,
    Jump,
    EOF,
    NOP,
    Debug,
} impl Instruction {

    /// Gets an `Instruction` from a `char`
    pub fn from_char(instruction: char) -> Self {
        match instruction {
            '>' => Instruction::IncPtr,
            '<' => Instruction::DecPtr,
            '+' => Instruction::IncVal,
            '-' => Instruction::DecVal,
            '.' => Instruction::Output,
            ',' => Instruction::Input,
            '[' => Instruction::SetJump,
            ']' => Instruction::Jump,
            ';' => Instruction::EOF,
            '#' => Instruction::Debug,
            _ => Instruction::NOP,
        }
    }

    /// Gets a `char` from an `Instruction`
    pub fn as_char(&self) -> char {
        match self {
            Instruction::IncPtr => '>',
            Instruction::DecPtr => '<',
            Instruction::IncVal => '+',
            Instruction::DecVal => '-',
            Instruction::Output => '.',
            Instruction::Input => ',',
            Instruction::SetJump => '[',
            Instruction::Jump => ']',
            Instruction::EOF => ';',
            Instruction::NOP => 'N',
            Instruction::Debug => '#',
        }
    }
} impl fmt::Display for Instruction {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.as_char())
    }
}