Advertisement
Guest User

Brainfuck interpreter in J

a guest
Mar 3rd, 2018
534
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
J 0.90 KB | None | 0 0
  1. NB. Brainfuck interpreter
  2. NB. Cells are arbitrary size, non-wrapping.
  3. NB. Tape does not extend to the left.
  4. tape =: 0x
  5. ip =: 0
  6. code =: '+-><'
  7.  
  8. fuck =: 3 : 0
  9. tape =: (,[:x:0:)^:(ip&>:@#) tape
  10. if. ',' = {.y do.
  11.         tape =: ({.a.i.1!:1]1) ip} tape
  12. elseif. '.' = {.y do.
  13.         4 (1!:2)~ a.{~ip{tape
  14. elseif. 2 > code i. {.y do.
  15.         tape =: (>:`<: @. (code i. {.y) ip{tape) ip} tape
  16. elseif. 4 > code i. {.y do.
  17.         ip =: >:`<: @. (2-~code i. {.y) ip
  18. elseif. '[' = {.y do.
  19.         while. 0 ~: ip{tape do.
  20.                 fuck }. (#~ */\@:(+/\@:=&']' 0&>@- +/\@:=&'[')) y
  21.         end.
  22. end.
  23. fuck^:(0:~:#) }. (#~ -.@(*/\)@:(+/\@:=&']' 0&>@- +/\@:=&'[')) y
  24. )
  25.  
  26. NB. Cleans up tape and IP after execution,
  27. NB. so that multiple programs can be ran in succession.
  28.  
  29. BF =: 3 : 0
  30. fuck y
  31. tape =: 0
  32. ip =: 0
  33. i.0             NB. Suppresses output (ip =: 0 would return 0, which would be printed)
  34. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement