brainfuck-monad-0.2.0: BrainFuck monad

Safe HaskellSafe-Inferred




newtype BrainFuck a Source


BrainFuck (DataPointer -> ([Char], DataPointer, a)) 


func :: BrainFuck f -> DataPointer -> ([Char], DataPointer, f) Source

Retrieve the inner function

brainfuck :: BrainFuck f -> String Source

Evaluate the monad and get a brainfuck program

next :: BrainFuck () Source

move data pointer right

close :: BrainFuck () Source

if byte at data pointer is nonzero, jump to optoce after matching open

open :: BrainFuck () Source

if byte at data pointer is zero, jump to opcode after close

input :: BrainFuck () Source

input byte, storing at data pointer

output :: BrainFuck () Source

output byte at data pointer

decr :: BrainFuck () Source

decrement data

incr :: BrainFuck () Source

increment data

prev :: BrainFuck () Source

move data pointer left

opcode :: Char -> BrainFuck () Source

Adds an arbitrary character to the program. Should not be used directly.

opcode' :: (DataPointer -> DataPointer) -> Char -> BrainFuck () Source

Adds an arbitrary character to the program, and updates the data pointer. Should not be used directly.

loopUnless0 :: BrainFuck a -> BrainFuck a Source

The loop is only run if the data pointer doesn't point to 0.

On entry, the loop body is run, and then it loops, until the data pointer points to 0.

addr :: BrainFuck DataPointer Source

Gets the current address of the data pointer.

setAddr :: Integer -> BrainFuck () Source

Moves the data pointer to a specific address.

multi :: BrainFuck () -> Int -> BrainFuck () Source

Run an action multiple times.

zero :: BrainFuck () Source

Zeros the current data cell.

set :: Int -> BrainFuck () Source

Changes the current data cell to contain a specific value. (It can start at any value).

alloc :: BrainFuck a -> BrainFuck a Source

For higher-level programming in brainfuck, it's useful to have a way to run a function, while allocating a memory cell, which is initialized to contain 0.

This and many of the functions below assume that cells to the left are in use, while cells to the right are unused and may contain any data. Higher-level functions should generally avoid changing the current cell, and should instead alloc a new one to use.

withChar :: Char -> BrainFuck a -> BrainFuck a Source

Allocates a new memory cell, populates it with a Char, and runs the action.

loopFrom :: Int -> (DataPointer -> BrainFuck ()) -> BrainFuck () Source

Allocates a cell and uses it as the loop counter, starting from the provided value. The action will continue running in a loop until it decrements the counter to 0.

forever :: BrainFuck a -> BrainFuck () Source

Runs an action in an infinite loop. The action should avoid touching the current memory cell.

unless0 :: BrainFuck () -> BrainFuck () Source

Runs the action unless the data pointer points to 0.

sum :: BrainFuck () Source

Adds the current and next data cells. The next cell is zeroed and the sum is left in the current cell.

mult :: Int -> BrainFuck () Source

Multiplies the current data cell by some value. Uses and zeros some of the following cells.

display :: String -> BrainFuck () Source

Displays a string. Tries to generate a fairly small brainfuck program, using a few encoding tricks. The current cell is modified, and not cleaned up at the end, so run using alloc if necessary.

demo :: String Source

Prints out the alphabet, repeatedly.

cat :: String Source

Copy input to output.

helloworld :: String Source

Simple hello world.

helloworld' :: String Source

Optimized to use less space.

optimize :: String -> String Source

Simple optimiser for brainfuck code.