Copyright | (c) Sebastian Galkin 2018 |
---|---|
License | GPL-3 |
Safe Haskell | None |
Language | Haskell2010 |
- data Op
- newtype MemOffset = MemOffset Int
- newtype MulFactor = MulFactor Int
- data Direction
- data Optimized
- data Unoptimized
- newtype Program opt = Program {
- instructions :: [Op]
- flattened :: Program o -> [Op]
- data Machine v = Machine {}
- class MachineIO m where
- data MockIO = MockIO {
- machineIn :: [Int8]
- machineOut :: [Int8]
- mkMockIO :: [Int8] -> MockIO
- mkMockIOS :: String -> MockIO
- mockOutput :: MockIO -> [Int8]
- mockOutputS :: MockIO -> String
- (<&>) :: Functor f => f a -> (a -> b) -> f b
- eitherToMaybe :: Either a b -> Maybe b
Virtual Machine Instructions
Operations or instructions in the Brainfuck virtual machine.
Some of these operations are "native" to Brainfuck and others are the result of optimization during compilation. The compiler generates these types of instructions and the virtual machine can execute them.
In all these instructions the MemOffset
represents a shift relative to the current position of the pointer.
The operation will refer and apply its action to this shifted position.
Inc !Int !MemOffset | Increment by the amount specified by the |
Move !MemOffset | Move the current pointer by the specified amount |
In !Int !MemOffset | Repeatedly read a byte into the machine and write the last one read to the shifted position.
|
Out !Int !MemOffset | Repeatedly write the byte in the shifted position. Where the byte is written will depend on the |
Loop ![Op] | Native Brainfuck looping instruction. |
Clear !MemOffset | Optimized instruction. Set the shifted position to zero. In Brainfuck this is usually written as |
Mul !MulFactor !MemOffset !MemOffset | Optimized instruction. Multiply by the factor the byte in the first |
Scan !Direction !MemOffset | Find the nearest zero in the given direction, starting at the offset position. See |
An offset into the Brainfuck VM memory. Positive numbers are in the direction of higher memory.
A factor to multiply by in the Mul
instruction.
Programs
Marker type to distinguish optimized and Unoptimized
Program
s.
data Unoptimized Source #
A list of Op
s. opt
will be one of Optimized
or Unoptimized
to
distinguish both types of programs at the type level.
Program | |
|
Eq (Program opt) Source # | |
Show (Program opt) Source # | |
Generic (Program opt) Source # | |
Semigroup (Program o) Source # | Apply |
Monoid (Program o) Source # | The |
Binary (Program opt) Source # | |
NFData (Program opt) Source # | |
type Rep (Program opt) Source # | |
flattened :: Program o -> [Op] Source #
Return the full list of instructions in a program, by unrolling Loop
instructions
into the list.
>>>
flattened $ Program [Inc 1 0, Loop [Move 1, Scan Up 0]]
[Inc 1 0,Move 1,Scan Up 0]
Runtime State
The state of a Brainfuck virtual machine.
VM Input/Output
class MachineIO m where Source #
Provide input and output to a Brainfuck virtual machine.
This class allows to run the VM in different monads, like IO
or StateT
.
:: Int8 | |
-> m () | Write the byte to the output of the VM. |
Test Helpers
A data structure for mocking input and output to the VM. This can be used to run the VM
in a StateT
monad for testing purposes.
mkMockIOS :: String -> MockIO Source #
Create a MockIO
that will have the given input available. ASCII encoding.
mockOutputS :: MockIO -> String Source #
Get the output after a VM has ran using this MockIO
. ASCII encoding.