Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BFI
- cell_max = 128
- cell_min = -128
- cell_start = 0
- stack = []
- stack_max = 2048
- sp = 0
- code = ''
- cp = 0
- loops = []
- output = ''
- comment = ''
- constructor: ->
- reset:->
- stack = []
- sp = 0
- loops = []
- output = ''
- comment = ''
- cp = 0
- return
- load:(c)->
- code = c
- @reset()
- return
- next:->
- if cp >= code.length then return -1
- if sp == stack.length then stack.push(cell_start)
- switch code.charAt(cp)
- when '+'
- stack[sp]++
- if stack[sp] > cell_max then stack[sp] = cell_min
- when '-'
- stack[sp]--
- if stack[sp] < cell_min then stack[sp] = cell_max
- when '>'
- sp++
- if sp > stack_max then sp = 0
- when '<'
- sp--
- if sp < 0 then sp = stack_max
- when '['
- if not stack[sp] then cp = loopEnd()
- loops.push(cp)
- when ']'
- if not loops.length then throw "Found ] when no loop is open - " + cp
- cp = loops.pop() - 1
- when '.'
- output += String.fromCharCode(stack[sp])
- when ','
- stack[sp] = input()
- else
- comment += code.charAt(cp)
- return cp++
- run:(c)->
- if c then @load(c)
- while @next() != -1
- continue
- return output
- @run:(c)->
- bfi = new BFI()
- return bfi.run(c);
- input = ->
- if str = prompt('Input only one char') then return str.charCodeAt(0)
- return 0
- loopEnd = ->
- skip = 0
- for i in [(cp + 1)..code.length]
- switch code.charAt(i)
- when '[' then skip++
- when ']'
- if not skip then return i
- skip--
- throw "end of loop not found - " + cp
- alert(BFI.run('+++++++++[>++++++++<-]>.+.'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement