CPBrainfuck 1.0 → 1.1
raw patch · 2 files changed
+21/−22 lines, 2 files
Files
- BF.hs +20/−21
- CPBrainfuck.cabal +1/−1
BF.hs view
@@ -16,6 +16,7 @@ deriving Show type Machine = ([Int],[Int])+type Interpreter = Machine -> String -> String {- | A brainFuck and derived languages interpreter. Currently supports Brainfuck@@ -30,11 +31,12 @@ ook -- Ook! -} main = do args <- System.getArgs- hSetBuffering stdin NoBuffering+ hSetBuffering stdin NoBuffering+ hSetBuffering stdout NoBuffering let (tokens,prog) = parseArgs args- interpret (\x -> putStrLn "") - (fst $ parse $ tokenise prog tokens)- emptyMachine+ interact $ interpret (const (const "")) + (fst $ parse $ tokenise prog tokens)+ emptyMachine emptyMachine :: Machine emptyMachine = (repeat 0,repeat 0)@@ -92,20 +94,17 @@ parse (JMatch:xs) = ([],xs) {- | Interpret some brain fuck -}-interpret :: (Machine -> IO ()) -> [Stmt] -> Machine -> IO ()-interpret c [] x = c x-interpret c (TMRight:xs) (l,(h:r)) = interpret c xs ((h:l),r)-interpret c (TMLeft:xs) ((h:l),r) = interpret c xs (l,(h:r))-interpret c (TInc:xs) (l,(h:r)) = interpret c xs (l,(h+1:r))-interpret c (TDec:xs) (l,(h:r)) = interpret c xs (l,(h-1:r))-interpret c (TIn:xs) (l,(_:r)) =- do y <- isEOF- if y- then interpret c xs (l,(-1:r))- else do x <- getChar- interpret c xs (l,(ord x:r))-interpret c (TOut:xs) (l,(x:r)) = do putChar (chr x)- interpret c xs (l,x:r)-interpret c (TLoop loop:xs) (l,(h:r)) =- if h == 0 then interpret c xs (l,(h:r))- else interpret (interpret c (TLoop loop:xs)) loop (l,(h:r))+interpret :: Interpreter -> [Stmt] -> Interpreter+interpret c [] x i = c x i+interpret c (TMRight:xs) (l,(h:r)) i = interpret c xs ((h:l),r) i+interpret c (TMLeft:xs) ((h:l),r) i = interpret c xs (l,(h:r)) i+interpret c (TInc:xs) (l,(h:r)) i = interpret c xs (l,(h+1:r)) i+interpret c (TDec:xs) (l,(h:r)) i = interpret c xs (l,(h-1:r)) i+interpret c (TIn:xs) (l,(_:r)) i =+ case i of+ [] -> interpret c xs (l,(-1:r)) []+ (y:ys) -> interpret c xs (l,(ord y:r)) ys+interpret c (TOut:xs) (l,(x:r)) i = (chr x) : (interpret c xs (l,x:r) i)+interpret c (TLoop loop:xs) (l,(h:r)) i =+ if h == 0 then interpret c xs (l,(h:r)) i+ else interpret (interpret c (TLoop loop:xs)) loop (l,(h:r)) i
CPBrainfuck.cabal view
@@ -1,6 +1,6 @@ Name: CPBrainfuck Cabal-Version: >= 1.2-Version: 1.0+Version: 1.1 Synopsis: A simple Brainfuck interpretter. Description: This is a very simple brainfuck interpretter, that's easy to understand.