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
/// Module containing the parser code.
mod parser;
/// Module containing the compiler code.
mod compiler;
/// Type representing a standard or virtual Brainfuck instruction.
#[derive(Clone, Debug, PartialEq)] // this is for tests
pub enum BrainfuckInstr {
    /// Move the data pointer back one cell.
    PointerDec,
    /// Move the data pointer forward one cell.
    PointerInc,
    /// Decrement the value of the current memory cell.
    DataDec,
    /// Increment the value of the current memory cell.
    DataInc,
    /// Get a byte from the standard input and store it in the current memory cell.
    GetByte,
    /// Write the current memory cell's value to the standard output.
    PutByte,
    /// Begin a while loop conditional on the current value not being zero.
    WhileNonzero,
    /// Close the while loop.
    EndWhile,
    /* The instructions below are our own virtual instructions that exist for optimization purposes.
    They DO NOT occur naturally in Brainfuck source code and have no corresponding text characters. */
    /// Subtract from the pointer.
    PointerSub(u16),
    /// Add to the pointer.
    PointerAdd(u16),
    /// Subtract from the current numer.
    DataSub(u8),
    /// Add to the current number.
    DataAdd(u8),
    /// Print a number of bytes to the standard output **at once**.
    Print(u16)
}

/// The syntax errors possible.
#[derive(Debug, PartialEq)]
pub enum SyntaxError {
    /// A closing square bracket was found at the contained line:index position, but there was no opening square bracket before it.
    PrematureEndWhile(usize, usize),
    /// The last while loop opened, at the contained line:index position, has no closing bracket.
    UnclosedWhile(usize, usize)
}

pub use compiler::compile;
pub use parser::{
    cto::optimize,
    Parser
};