Guest User

Untitled

a guest
May 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. module BFLike2
  2. (run, eval) where
  3.  
  4. import Data.Char
  5.  
  6. type BFState = ([Char], [Int], Int, (Bool, [Char]))
  7.  
  8. while :: BFState -> BFState
  9. run :: BFState -> Char -> BFState
  10. eval :: [Char] -> [Char]
  11.  
  12. while (value, state, ptr, rc) =
  13. if state' !! ptr' < 1
  14. then (value', state', ptr', (False, []))
  15. else while (value', state', ptr', rc)
  16. where (value', state', ptr', rc') =
  17. foldl run (value, state, ptr, (False, [])) (snd rc)
  18.  
  19. run (value, state, ptr, rc) inst =
  20. if fst rc
  21. then case inst of
  22. ']' -> while (value, state, ptr, (False, snd rc))
  23. _ -> (value, state, ptr, (True, (snd rc) ++ [inst]))
  24. else case inst of
  25. '+' -> (value, (take ptr state) ++
  26. [(state !! ptr) + 1] ++
  27. (drop (ptr + 1) state), ptr, rc)
  28. '-' -> (value, (take ptr state) ++
  29. [(state !! ptr) - 1] ++
  30. (drop (ptr + 1) state), ptr, rc)
  31. '>' -> (value, if length state == ptr + 1
  32. then state ++ [0]
  33. else state, ptr + 1, rc)
  34. '<' -> (value, state, ptr - 1, rc)
  35. '.' -> (value ++ [chr $ state !! ptr], state, ptr, rc)
  36. '[' -> (value, state, ptr, (True, []))
  37. _ -> (value, state, ptr, rc)
  38.  
  39. eval l = value where (value, _, _, _) =
  40. foldl run ([], [0], 0, (False, [])) l
Add Comment
Please, Sign In to add comment