fork(1) download
  1. {-# LANGUAGE MultiWayIf, BangPatterns #-}
  2.  
  3. import Data.Word
  4. import Data.Char
  5.  
  6. data Tape = Tape { negatives :: [Word8], currentTape :: !Word8, positives :: [Word8] }
  7.  
  8. mkTape = Tape (repeat 0) 0 (repeat 0)
  9. shiftLeft (Tape (x:left) curr right) = Tape left x (curr:right)
  10. shiftRight (Tape left curr (x:right)) = Tape (curr:left) x right
  11. modifyTape f (Tape left curr right) = Tape left (f curr) right
  12.  
  13. newtype Stack a = Stack [a]
  14.  
  15. push x (Stack s) = Stack (x:s)
  16. top (Stack s) = head s
  17. pop (Stack (s:ss)) = Stack ss
  18.  
  19. evalReal program = eval 0 (Stack [0]) mkTape
  20. where eval !curr !loops !tape = if
  21. | length program == curr -> return ()
  22. | command == '[' -> (if
  23. | skip || currentTape tape == 0 -> eval (curr + 1) (push (-1) loops) tape
  24. | otherwise -> eval (curr + 1) (push curr loops) tape)
  25. | command == ']' -> (if
  26. | skip || currentTape tape == 0 -> eval (curr + 1) (pop loops) tape
  27. | otherwise -> eval (top loops + 1) loops tape)
  28. | skip -> nextCommand tape
  29. | command == '+' -> nextCommand (modifyTape (+1) tape)
  30. | command == '-' -> nextCommand (modifyTape (subtract 1) tape)
  31. | command == '>' -> nextCommand (shiftRight tape)
  32. | command == '<' -> nextCommand (shiftLeft tape)
  33. | command == '.' -> putChar (chr $ fromIntegral $ currentTape tape) >> nextCommand tape
  34. | command == ',' -> getChar >>= \x -> return (tape {currentTape = (fromIntegral $ ord x)}) >> nextCommand tape
  35. | otherwise -> nextCommand tape
  36. where nextCommand = eval (curr + 1) loops
  37. skip = top loops == (-1)
  38. command = program !! curr
  39.  
  40. main = evalReal "->+>+++>>+>++>+>+++>>+>++>>>+>+>+>++>+>>>>+++>+>>++>+>+++>>++>++>>+>>+>++>++>+>>>>+++>+>>>>++>++>>>>+>>++>+>+++>>>++>>++++++>>+>>++>+>>>>+++>>+++++>>+>+++>>>++>>++>>+>>++>+>+++>>>++>>+++++++++++++>>+>>++>+>+++>+>+++>>>++>>++++>>+>>++>+>>>>+++>>+++++>>>>++>>>>+>+>++>>+++>+>>>>+++>+>>>>+++>+>>>>+++>>++>++>+>+++>+>++>++>>>>>>++>+>+++>>>>>+++>>>++>+>+++>+>+>++>>>>>>++>>>+>>>++>+>>>>+++>+>>>+>>++>+>++++++++++++++++++>>>>+>+>>>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++++>>>+>++>>+++>+>+>++>+>+++>>>>>+++>>>+>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++>>+>+++>>>>>>++>+>+++>>+>++>>>>+>+>++>+>>>>+++>>+++>>>+[[->>+<<]<+]+++++[->+++++++++<]>.[+]>>[<<+++++++[->+++++++++<]>-.------------------->-[-<.<+>>]<[+]<+>>>]<<<[-[-[-[>>+<++++++[->+++++<]]>++++++++++++++<]>+++<]++++++[->+++++++<]>+<<<-[->>>++<<<]>[->>.<<]<<]"
Success #stdin #stdout 0.81s 6216KB
stdin
Standard input is empty
stdout
->+>+++>>+>++>+>+++>>+>++>>>+>+>+>++>+>>>>+++>+>>++>+>+++>>++>++>>+>>+>++>++>+>>>>+++>+>>>>++>++>>>>+>>++>+>+++>>>++>>++++++>>+>>++>+>>>>+++>>+++++>>+>+++>>>++>>++>>+>>++>+>+++>>>++>>+++++++++++++>>+>>++>+>+++>+>+++>>>++>>++++>>+>>++>+>>>>+++>>+++++>>>>++>>>>+>+>++>>+++>+>>>>+++>+>>>>+++>+>>>>+++>>++>++>+>+++>+>++>++>>>>>>++>+>+++>>>>>+++>>>++>+>+++>+>+>++>>>>>>++>>>+>>>++>+>>>>+++>+>>>+>>++>+>++++++++++++++++++>>>>+>+>>>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++++>>>+>++>>+++>+>+>++>+>+++>>>>>+++>>>+>+>>++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++>>+>+++>>>>>>++>+>+++>>+>++>>>>+>+>++>+>>>>+++>>+++>>>+[[->>+<<]<+]+++++[->+++++++++<]>.[+]>>[<<+++++++[->+++++++++<]>-.------------------->-[-<.<+>>]<[+]<+>>>]<<<[-[-[-[>>+<++++++[->+++++<]]>++++++++++++++<]>+++<]++++++[->+++++++<]>+<<<-[->>>++<<<]>[->>.<<]<<]