[][src]Struct brainfrick::Brainfuck

pub struct Brainfuck {
    pub max_steps: usize,
    // some fields omitted
}

A struct that parses and runs brainfuck.

Example

use brainfrick::Brainfuck;

let purpzie_sucks = Brainfuck::execute("
    ++++++++[>++++++++++<-]>.<++[>++++++++++<-]+++[>+++++<-]>+
    +.---.--.++++++++++.<++[>----------<-]>+++.----.<+++++++[>
    ----------<-]>+.<++++++++[>++++++++++<-]>+++.++.<+++[>----
    --<-]>.++++++++.++++++++.<++++++++[>----------<-]>--.
")?;

assert_eq!(purpzie_sucks, "Purpzie sucks!");

The debug character

To aid whoever is crazy enough to write in brainfuck, the question mark ? will output the current cell number and value.

let where_am_i = Brainfuck::execute(">>+++++?")?;
assert_eq!(where_am_i, "[2,5]");

Memory details

Memory is infinite in both directions. In order to prevent malicious brainfuck from running forever, there is a configurable, maximum number of 'steps' you can allow to be executed before stopping.

let mut infinite = Brainfuck::parse("+[>+]")?;
infinite.max_steps = 100;
let result = infinite.run();

assert!(result.is_err());

Testing multiple inputs

If you'd like to quickly test the same brainfuck with many different inputs, you can parse it beforehand to speed up the process. See Brainfuck::input for information.

Fields

max_steps: usize

The maximum number of 'steps' that will be run before stopping. Defaults to Brainfuck::MAX_STEPS.

Implementations

impl Brainfuck[src]

pub const MAX_STEPS: usize[src]

The default maximum number of 'steps' that will be run before stopping.

pub fn execute<S: Into<String>>(code: S) -> Result<String, Error>[src]

Run some brainfuck.

Errors

May return an Error of kind UnmatchedBracket or MaxSteps.

Example

let purpzie_sucks = Brainfuck::execute("
    ++++++++[>++++++++++<-]>.<++[>++++++++++<-]+++[>+++++<-]>+
    +.---.--.++++++++++.<++[>----------<-]>+++.----.<+++++++[>
    ----------<-]>+.<++++++++[>++++++++++<-]>+++.++.<+++[>----
    --<-]>.++++++++.++++++++.<++++++++[>----------<-]>--.
")?;

assert_eq!(purpzie_sucks, "Purpzie sucks!");

pub fn execute_with_input<O: Into<String>, R: AsRef<str>>(
    code: O,
    input: R
) -> Result<String, Error>
[src]

Run some brainfuck with input.

Errors

May return an Error of kind UnmatchedBracket or MaxSteps.

Example

let loud = Brainfuck::execute_with_input(
    ",[>++++[<-------->-]<.,]",
    "foobar",
)?;

assert_eq!(loud, "FOOBAR");

pub fn parse<S: Into<String>>(code: S) -> Result<Brainfuck, Error>[src]

Parse some brainfuck for later use.

This essentially just creates a Brainfuck struct.

Errors

May return an UnmatchedBracket error.

Example

let purpzie = Brainfuck::parse("
    ++++++++[>++++++++++<-]>.<++[>++++++++++<-]+++[>+++++<-]>+
    +.---.--.++++++++++.<++[>----------<-]>+++.----.<+++++++[>
    ----------<-]>+.<++++++++[>++++++++++<-]>+++.++.<+++[>----
    --<-]>.++++++++.++++++++.<++++++++[>----------<-]>--.
")?;

// ...later

let sucks = purpzie.run()?;

assert_eq!(sucks, "Purpzie sucks!");

pub fn run(&self) -> Result<String, Error>[src]

Run the brainfuck.

Note that, for a single Brainfuck, this will always output the same result.

Errors

May return a MaxSteps error.

Example

let purpzie = Brainfuck::parse("
    ++++++++[>++++++++++<-]>.<++[>++++++++++<-]+++[>+++++<-]>+
    +.---.--.++++++++++.<++[>----------<-]>+++.----.<+++++++[>
    ----------<-]>+.<++++++++[>++++++++++<-]>+++.++.<+++[>----
    --<-]>.++++++++.++++++++.<++++++++[>----------<-]>--.
")?;

// ...later

let sucks = purpzie.run()?;

assert_eq!(sucks, "Purpzie sucks!");

pub fn input<S: AsRef<str>>(&self, input: S) -> Result<String, Error>[src]

Run the brainfuck with input.

Errors

May return a MaxSteps error.

Example

let loud = Brainfuck::parse(",[>++++[<-------->-]<.,]")?;

assert_eq!(loud.input("foobar")?, "FOOBAR");
assert_eq!(loud.input("heck")?, "HECK");
assert_eq!(loud.input("aaaaa")?, "AAAAA");

pub fn code(&self) -> &str[src]

The original, brainfuck 'source'.

Example

let code = ",[>++++[<-------->-]<.,]";
let example = Brainfuck::parse(code)?;
assert_eq!(code, example.code());

Trait Implementations

impl Clone for Brainfuck[src]

impl Debug for Brainfuck[src]

impl Eq for Brainfuck[src]

impl PartialEq<Brainfuck> for Brainfuck[src]

impl StructuralEq for Brainfuck[src]

impl StructuralPartialEq for Brainfuck[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.