Skip to content

Instantly share code, notes, and snippets.

@kuuote
Created December 23, 2022 11:11
Show Gist options
  • Save kuuote/e06afdde73d4cf9f632013b8c345f5cf to your computer and use it in GitHub Desktop.
Save kuuote/e06afdde73d4cf9f632013b8c345f5cf to your computer and use it in GitHub Desktop.
function! BF(fxxk) abort
let mem = 0z00
for i in range(15)
let mem = mem + mem
endfor
let ip = 0
let dp = 0
let out = []
while ip < len(a:fxxk)
let c = a:fxxk[ip]
if c == ">"
let dp += 1
elseif c == "<"
let dp -= 1
elseif c == "+"
let mem[dp] = mem[dp] + 1
elseif c == "-"
let mem[dp] = mem[dp] - 1
elseif c == "."
call add(out, mem[dp])
elseif c == ","
let mem[dp] = getchar()
elseif c == "["
if mem[dp] == 0
let s = 0
while v:true
let ip += 1
let c = a:fxxk[ip]
if c == "["
let s += 1
elseif c == "]"
if s == 0
break
else
let s -= 1
endif
endif
endwhile
endif
elseif c == "]"
if mem[dp] != 0
let s = 0
while v:true
let ip -= 1
let c = a:fxxk[ip]
if c == "]"
let s += 1
elseif c == "["
if s == 0
break
else
let s -= 1
endif
endif
endwhile
endif
endif
let ip += 1
endwhile
return join(map(out, "nr2char(v:val)"), "")
endfunction
echo BF(">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++ ++>-]<.>+++++++++++[<+++++>-]<.>++++++++[<+++>-]<.+++.------.--------.[-]> ++++++++[<++++>-]<+.[-]++++++++++.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment