Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- NB. Brainfuck interpreter
- NB. Cells are arbitrary size, non-wrapping.
- NB. Tape does not extend to the left.
- tape =: 0x
- ip =: 0
- code =: '+-><'
- fuck =: 3 : 0
- tape =: (,[:x:0:)^:(ip&>:@#) tape
- if. ',' = {.y do.
- tape =: ({.a.i.1!:1]1) ip} tape
- elseif. '.' = {.y do.
- 4 (1!:2)~ a.{~ip{tape
- elseif. 2 > code i. {.y do.
- tape =: (>:`<: @. (code i. {.y) ip{tape) ip} tape
- elseif. 4 > code i. {.y do.
- ip =: >:`<: @. (2-~code i. {.y) ip
- elseif. '[' = {.y do.
- while. 0 ~: ip{tape do.
- fuck }. (#~ */\@:(+/\@:=&']' 0&>@- +/\@:=&'[')) y
- end.
- end.
- fuck^:(0:~:#) }. (#~ -.@(*/\)@:(+/\@:=&']' 0&>@- +/\@:=&'[')) y
- )
- NB. Cleans up tape and IP after execution,
- NB. so that multiple programs can be ran in succession.
- BF =: 3 : 0
- fuck y
- tape =: 0
- ip =: 0
- i.0 NB. Suppresses output (ip =: 0 would return 0, which would be printed)
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement