Skip to content

Instantly share code, notes, and snippets.

@micahrj
Created May 31, 2010 01:59
Show Gist options
  • Save micahrj/419469 to your computer and use it in GitHub Desktop.
Save micahrj/419469 to your computer and use it in GitHub Desktop.
module BF (run, Memory, Command) where
import Data.Char
data Memory = Memory [Integer] Integer [Integer]
empty = Memory [0,0..] 0 [0,0..]
type Command = Char
run :: [Commmand] -> (Memory, String)
run cs = exec cs empty ""
exec :: [Command] -> Memory -> String -> (Memory, String)
exec [] m s = (m, s)
exec ('+':cs) (Memory xs n ys) s = run cs (Memory xs (n+1) ys) s
exec ('-':cs) (Memory xs n ys) s = run cs (Memory xs (n-1) ys) s
exec ('<':cs) (Memory (x:xs) y ys) s = run cs (Memory xs x (y:ys)) s
exec ('>':cs) (Memory xs x (y:ys)) s = run cs (Memory (x:xs) y ys) s
exec ('.':cs) m@(Memory xs y zs) s = run cs m (s ++ [(chr y)])
exec ('[':cs) m s =
@devyn
Copy link

devyn commented May 31, 2010

Awesome. No 'while' though, and no 'getc'. :(

@devyn
Copy link

devyn commented Jun 1, 2010

I'm going to implement 'while' in this (or at least attempt to)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment