haskell-brainfuck-0.1.0.0: BrainFuck interpreter

Copyright(c) Sebastian Galkin, 2014
LicenseMIT
Maintainerparaseba@gmail.com
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

HaskBF.Eval

Description

This module exports functions that allow to evaluate a BrainFuck program. Evaluation supports two types of error, parsing and execution, by returning instances of EvalResult

This module should be all library users need to import.

Synopsis

Documentation

eval

Arguments

:: Monad m 
=> Machine m

The machine used to do I/O

-> Program

The program to evaluate

-> m (Either BFExError BFTape)

Resulting tape after evaluation or execution error

Evaluate a parsed BrainFuck program using I/O provided by the given - Machine - - The result is either an execution error or the BFTape representing the - resulting state of the tape after the last instruction was executed.

evalBS

Arguments

:: Monad m 
=> Machine m

The machine used to do I/O

-> ByteString

The code to evaluate

-> m EvalResult

Parsing/evaluation result

Evaluate an unparsed BrainFuck program using I/O provided by the given - Machine - - The result is returned as an EvalResult

evalStr

Arguments

:: Monad m 
=> Machine m

The machine used to do I/O

-> String

The code to evaluate

-> m EvalResult

Parsing/evaluation result

Evaluate an unparsed BrainFuck program using I/O provided by the given - Machine - - The result is returned as an EvalResult

data EvalResult

Evaluation result of an unparsed BrainFuck program

Constructors

EvalSuccess BFTape

Parsing and evaluation were successful. The resulting - state of the tape after the last instruction - was executed.

EvalExecError BFExError

The program was parsed successfully but evaluation - failed. - The reason for failure is overflowing a limit of the tape. - The state of the tape before the error is included

EvalParseError ParseError

The program can not be parsed. Parsing error message is - included

data Machine m

Underlying input output for the evaluation machine. Changing the monad m - achives different results. For instance using the IO monad an evaluator can - be created that does inputoutput to stdinstdout. If the monad is State, - for instance, input/output can happen in memory. - - We offer two implementations of Machine: - - * defaultIOMachine: under the IO monad, does input/output using the - standard streams - * simulatorMachine: under the State monad, does input/output on lists - - It's easy to create other Machines by using different monads and - functions.

Constructors

Machine 

Fields

putByte :: Int8 -> m ()

Write a byte to the output, under monad m

getByte :: m Int8

Get a byte under the m monad

defaultIOMachine :: Machine IO

A Machine that can evaluate code under the IO monad by doing I/O - to stdin/stout. - - Bytes are read by comnverting them from the ASCII code

simulatorMachine :: Machine (State SimState)

A Machine that can evaluate program doing in-memory I/O under the State - monad. It stores state as SimState

data SimState

State used by simulatorMachine to evaluate code under the State monad - - It maintains input and output bytes inside lists

Constructors

SimState [Int8] [Int8] 

simStateOutput :: SimState -> [Int8]

Extract the output stream from a simulatorMachine state