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.




import "io" for Stdin
 
class Brainf__k {
    construct new(prog, memSize) {
        _prog = prog
        _memSize = memSize
        _mem = List.filled(memSize, 0)
        _ip = 0
        _dp = 0
    }
 
    memVal_ { (_dp >= 0 && _dp < _memSize) ? _mem[_dp] : 0 }
 
    execute() {
        while (_ip < _prog.count) {
            var cmd = _prog[_ip]
            _ip = _ip + 1
            if (cmd == ">") {
                _dp = _dp + 1
            } else if (cmd == "<") {
                _dp = _dp - 1
            } else if (cmd == "+") {
                _mem[_dp] = memVal_ + 1
            } else if (cmd == "-") {
                _mem[_dp] = memVal_ - 1
            } else if (cmd == ",") {
                _mem[_dp] = Stdin.readByte()
            } else if (cmd == ".") {
                System.write(String.fromByte(memVal_))
            } else if (cmd == "[") {
                handleLoopStart_()
            } else if (cmd == "]") {
                handleLoopEnd_()
            }
        }
    }
 
    handleLoopStart_() {
        if (memVal_ != 0) return
        var depth = 1
        while (_ip < _prog.count) {
            var cmd = _prog[_ip]
            _ip = _ip + 1
            if (cmd == "[") {
                depth = depth + 1
            } else if (cmd == "]") {
                depth = depth - 1
                if (depth == 0) return
            }
        }
        Fiber.abort("Could not find matching end bracket.")
    }
 
    handleLoopEnd_() {
        var depth = 0
        while (_ip >= 0) {
            _ip = _ip - 1
            var cmd = _prog[_ip]
            if (cmd == "]") {
                depth = depth + 1
            } else if (cmd == "[") {
                depth = depth - 1
                if (depth == 0) return
            }
        }
        Fiber.abort("Could not find matching start bracket.")
    }
}
 
var prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."
Brainf__k.new(prog, 10).execute()