bfc_rs/
lib.rs

1/// Module containing the parser code.
2mod parser;
3/// Module containing the compiler code.
4mod compiler;
5/// Type representing a standard or virtual Brainfuck instruction.
6#[derive(Clone, Debug, PartialEq)] // this is for tests
7pub enum BrainfuckInstr {
8    /// Move the data pointer back one cell.
9    PointerDec,
10    /// Move the data pointer forward one cell.
11    PointerInc,
12    /// Decrement the value of the current memory cell.
13    DataDec,
14    /// Increment the value of the current memory cell.
15    DataInc,
16    /// Get a byte from the standard input and store it in the current memory cell.
17    GetByte,
18    /// Write the current memory cell's value to the standard output.
19    PutByte,
20    /// Begin a while loop conditional on the current value not being zero.
21    WhileNonzero,
22    /// Close the while loop.
23    EndWhile,
24    /* The instructions below are our own virtual instructions that exist for optimization purposes.
25    They DO NOT occur naturally in Brainfuck source code and have no corresponding text characters. */
26    /// Subtract from the pointer.
27    PointerSub(u16),
28    /// Add to the pointer.
29    PointerAdd(u16),
30    /// Subtract from the current numer.
31    DataSub(u8),
32    /// Add to the current number.
33    DataAdd(u8),
34    /// Print a number of bytes to the standard output **at once**.
35    Print(u16)
36}
37
38/// The syntax errors possible.
39#[derive(Debug, PartialEq)]
40pub enum SyntaxError {
41    /// A closing square bracket was found at the contained line:index position, but there was no opening square bracket before it.
42    PrematureEndWhile(usize, usize),
43    /// The last while loop opened, at the contained line:index position, has no closing bracket.
44    UnclosedWhile(usize, usize)
45}
46
47pub use compiler::compile;
48pub use parser::{
49    cto::optimize,
50    Parser
51};