Skip to content

Instantly share code, notes, and snippets.

@alvaro-cuesta
Created February 9, 2013 12:13
Show Gist options
  • Save alvaro-cuesta/4745047 to your computer and use it in GitHub Desktop.
Save alvaro-cuesta/4745047 to your computer and use it in GitHub Desktop.
byte2str = (byte) ->
(String.fromCharCode(x) for x in byte).join('')
str2byte = (str) ->
(x.charCodeAt 0 for x in str)
TAPE_SIZE = 30000
CELL_WIDTH = 16
IO_WIDTH = 8
mod = (a, n) -> (a % n + n) % n
bf = (code, stdin) ->
code = code.split ''
cptr = iptr = tptr = 0
tape = (0 for x in [1..TAPE_SIZE])
out = []
stack = []
backFrom = []
forwardFrom = []
for op, i in code
if op == '['
stack.push(i + 1)
else if op == ']'
origin = stack.pop()
forwardFrom[origin] = i+1
backFrom[i+1] = origin
throw "Umbalanced [" if stack.length
ops =
'>': -> tptr = mod tptr + 1, TAPE_SIZE
'<': -> tptr = mod tptr - 1, TAPE_SIZE
'+': -> tape[tptr] = mod tape[tptr] + 1, (1 << CELL_WIDTH)
'-': -> tape[tptr] = mod tape[tptr] - 1, (1 << CELL_WIDTH)
'.': -> out.push mod tape[tptr], (1 << IO_WIDTH)
',': -> tape[tptr] = if iptr < stdin.length then stdin[iptr++] else (1 << CELL_WIDTH) - 1
'[': -> cptr = forwardFrom[cptr] if not tape[tptr]
']': -> cptr = backFrom[cptr] if tape[tptr]
ops[x]?() while (x = code[cptr++])?
out: out
tape: tape
compile = (code, size) ->
(stdin) -> bf code, stdin
hello = compile '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.'
sierpinski = compile '>++++[<++++++++>-]>++++++++[>++++<-]>>++>>>+>>>+<<<<<<<<<<[-[->+<]>[-<+>>>.<<]>>>[[->++++++++[>++++<-]>.<<[->+<]+>[->++++++++++<<+>]>.[-]>]]+<<<[-[->+<]+>[-<+>>>-[->+<]++>[-<->]<<<]<<<<]++++++++++.+++.[-]<]+++++'
fibonacci = compile '+++++++++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]'
prime = compile '>++++++++[<++++++++>-]<++++++++++++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++.[-]>++++++++++[<++++++++++>-]<+++++++++.[-]>++++++++++[<++++++++++>-]<+.[-]>++++++++++[<++++++++++>-]<+++++++++++++++.[-]>+++++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<+++++++++++++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++.[-]>+++++[<+++++>-]<+++++++.[-]>++++++++++[<++++++++++>-]<++++++++++++++++.[-]>++++++++++[<++++++++++>-]<+++++++++++.[-]>+++++++[<+++++++>-]<+++++++++.[-]>+++++[<+++++>-]<+++++++.[-]+[->,----------[<+>-------------------------------------->[>+>+<<-]>>[<<+>>-]<>>>+++++++++[<<<[>+>+<<-]>>[<<+>>-]<[<<+>>-]>>-]<<<[-]<<[>+<-]]<]>>[<<+>>-]<<>+<-[>+[>+>+<<-]>>[<<+>>-]<>+<-->>>>>>>>+<<<<<<<<[>+<-<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<<<>[>>+>+<<<-]>>>[<<<+>>>-]<<<<>>>[>+>+<<-]>>[<<+>>-]<<<[>>>>>+<<<[>+>+<<-]>>[<<+>>-]<[>>[-]<<-]>>[<<<<[>+>+<<-]>>[<<+>>-]<>>>-]<<<-<<-]+>>[<<[-]>>-]<<>[-]<[>>>>>>[-]<<<<<<-]<<>>[-]>[-]<<<]>>>>>>>>[-<<<<<<<[-]<<[>>+>+<<<-]>>>[<<<+>>>-]<<<>>[>+<-]>[[>+>+<<-]>>[<<+>>-]<>+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>-]<<-<-]+++++++++>[<->-]<[>+<-]<[>+<-]<[>+<-]>>>[<<<+>>>-]<>+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<-]<<-<-]>>>>[<<<<+>>>>-]<<<<>[-]<<+>]<[[>+<-]+++++++[<+++++++>-]<-><.[-]>>[<<+>>-]<<-]>++++[<++++++++>-]<.[-]>>>>>>>]<<<<<<<<>[-]<[-]<<-]++++++++++.[-]'
console.log byte2str hello().out
console.log byte2str fibonacci().out
console.log byte2str sierpinski().out
start = +new Date()
console.log byte2str prime(str2byte('20\n')).out
console.log 'Shown in ' + (+new Date() - start) + ' msecs'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment