[−][src]Struct brainfrick::Brainfuck
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]
code: O,
input: R
) -> Result<String, Error>
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 parsed 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 parsed 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
impl RefUnwindSafe for Brainfuck
impl Send for Brainfuck
impl Sync for Brainfuck
impl Unpin for Brainfuck
impl UnwindSafe for Brainfuck
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,