public
Created

Brainfuck

  • Download Gist
gistfile1.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
import System.Environment
import Control.Monad.State
import Data.Char (ord, chr)
 
data BFState = BFState [Int] Int [Int]
 
type BFT a = StateT BFState IO a
 
sanitise [] = []
sanitise (x:xs)
| x == '\n' || x == ' ' || x == '\t' = sanitise xs
| otherwise = x : sanitise xs
 
next :: BFT ()
next = do
(BFState xs y ys) <- get
put (BFState (xs ++ [y]) (head ys) (tail' ys))
 
prev :: BFT ()
prev = do
(BFState xs y ys) <- get
put (BFState (init xs) (last xs) (y : ys))
 
apply :: (Int -> Int) -> BFT ()
apply f = do
(BFState xs y ys) <- get
put (BFState xs (f y) ys)
 
inc :: BFT ()
inc = apply (+1)
 
dec :: BFT ()
dec = apply (\x -> x - 1)
 
inb :: BFT Int
inb = do
c <- lift getChar
return (ord c)
 
out :: BFT ()
out = do
(BFState _ y _) <- get
lift (putStr [chr y])
 
execute :: String -> BFT ()
execute [] = return ()
execute ('>':xs) = next >> execute xs
execute ('<':xs) = prev >> execute xs
execute ('+':xs) = inc >> execute xs
execute ('-':xs) = dec >> execute xs
execute ('.':xs) = out >> execute xs
execute (',':xs) = inb >> execute xs
execute ('[':xs) = executeBracket xs >> execute xs
execute (']':xs) = return ()
 
tail' [] = []
tail' (x:xs) = xs
 
executeBracket xs = do
(BFState _ y _) <- get
if y == 0
then execute (tail' $ dropWhile (/= ']') xs)
else execute xs >> executeBracket xs
 
main = do
args <- getArgs
conts <- readFile (args !! 0)
runStateT (execute $ sanitise conts) (BFState [] 0 (replicate 9999 0))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.