Execute Brain**** is an implementation of Brainf***.
Other implementations of Brainf***.
RCBF is a set of Brainf*** compilers and interpreters written for Rosetta Code in a variety of languages.

Below are links to each of the versions of RCBF.

An implementation need only properly implement the following instructions:



Command

Description


 > 
 Move the pointer to the right


 < 
 Move the pointer to the left


 + 
 Increment the memory cell under the pointer


 - 
 Decrement the memory cell under the pointer


 . 
 Output the character signified by the cell at the pointer


 , 
 Input a character and store it in the cell at the pointer


 [ 
 Jump past the matching ] if the cell under the pointer is 0


 ] 
 Jump back to the matching [ if the cell under the pointer is nonzero

Any cell size is allowed,   EOF   (End-O-File)   support is optional, as is whether you have bounded or unbounded memory.




get "libhdr"
 
manifest
$(  bfeof = 0
$)
 
let reads(v) be
$(  let ch = ?
    v%0 := 0
    ch := rdch()
    until ch = '*N' do
    $(  v%0 := v%0 + 1
        v%(v%0) := ch
        ch := rdch()
    $)
$)
 
let contains(str, ch) = valof
$(  for i = 1 to str%0 do
        if ch = str%i then resultis true
    resultis false
$)
 
let readbf(file, v) = valof
$(  let i, ch = 1, ?
    let curin = input()
    v%0 := 0
    selectinput(file)
    ch := rdch()
    until ch = endstreamch do
    $(  if contains("+-<>.,[]", ch) then
        $(  v%i := ch
            i := i + 1
        $)
        ch := rdch()
    $)
 
    v%i := 0
    endread()
    selectinput(curin)
    resultis i + 1
$)
 
let bfout(ch) be wrch(ch=10 -> '*N', ch)
let bfin() = valof
$(  let ch = rdch()
    resultis ch = endstreamch -> bfeof, ch
$)
 
let scan(v, i, dir) = valof
$(  let d = 1
    until d = 0 do
    $(  i := i + dir
        if v%i = 0 then
        $(  writes("Unbalanced brackets*N")
            resultis 0
        $)
        if v%i = '[' then d := d + dir
        if v%i = ']' then d := d - dir
    $)
    resultis i
$)
 
let run(v, m) be
$(  let i = 1
    until v%i = 0 do
    $(  switchon v%i into
        $(  case '+': v%m := v%m + 1 ; endcase 
            case '-': v%m := v%m - 1 ; endcase
            case '>': m := m + 1 ; endcase
            case '<': m := m - 1 ; endcase
            case '.': bfout(v%m) ; endcase
            case ',': v%m := bfin() ; endcase
            case '[':
                if v%m = 0 then i := scan(v, i, 1)
                if i = 0 then return
                endcase
            case ']':
                unless v%m = 0 do i := scan(v, i, -1)
                if i = 0 then return 
                endcase
        $)
        i := i + 1
    $)
$)
 
let start() be
$(  let fname = vec 63
    let file = ?
 
    writes("Filename? ")
    reads(fname)
    file := findinput(fname)
 
    test file = 0 then 
        writes("Cannot open file.*N")
    else
    $(  let mvec = getvec(maxvec())
        let m = readbf(file, mvec)
        run(mvec, m)
        freevec(mvec)
    $)
$)