Execute Brain****

From Rosetta Code
Task
Execute Brain****
You are encouraged to solve this task according to the task description, using any language you may know.
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 '[', ']', '+', '-', '<', '>', ',', and '.' instructions. Any cell size is allowed, EOF support is optional, as is whether you have bounded or unbounded memory.

ALGOL 68

See RCBF/ALGOL 68.

Ada

See RCBF/Ada.

BASIC

See RCBF/BASIC/QuickBasic.

C++

See RCBF/C++.

Common Lisp

See RCBF/Common Lisp.

D

See RCBF/D.

E

See RCBF/E.

Erlang

See RCBF/Erlang.

Forth

See RCBF/Forth.

F#

Seems like everyone else has made a separate page, but I don't know how to do that. If it's the thing to do, maybe somebody else can do the "right thing" for me and I apologize.

Anyway, here's a relatively straightforward version of RCBF. <lang fsharp>open System open System.Collections.Generic

let Rcbf (pgmStr : string) (input : int array) =

   let pgm = Seq.toArray pgmStr                                        // Turn program into an array
   let ptr = ref 0                                                     // Pointer into input
   let ip = ref 0                                                      // Instruction Pointer
   let InputNumber() =
       let mutable fValid = false
       let mutable num = 0
       printfn "Enter a valid number"
       fValid <- System.Int32.TryParse(Console.ReadLine(), &num);
       while not fValid do
           printfn "Invalid input.  Please enter a valid number"
           fValid <- System.Int32.TryParse(Console.ReadLine(), &num);
       num
   let AdvanceIp() = ip := !ip + 1                                     // Advance IP
   let RetreatIp() = ip := !ip - 1                                     // Decrement IP
   let SkipBrackets fForward =
       if input.[!ptr] <> 0 then                                       // If we have a 0 input
           AdvanceIp()                                                 // Then just advance to next instruction
       else                                                            // otherwise
           let fnMove = if fForward then AdvanceIp else RetreatIp      // get the appropriate function to move forward or backward
           let mutable cBrackets = 1                                   // Count of brackets we've seen
           while cBrackets > 0 do                                      // While we have unmatched brackets
               fnMove()                                                // move to the next character
               if !ip >= 0 && !ip < pgm.Length then                    // and if we're still within range
                   match pgm.[!ip] with                                // look at the character
                   | '[' -> cBrackets <- cBrackets + 1                 // If it's a '[' then count up brackets
                   | ']' -> cBrackets <- cBrackets - 1                 // If it's a ']' then count down brackets
                   | _ -> ignore(0)                                    // Ignore anything else
           AdvanceIp()                                                 // When we're on the matching bracket, skip to the next instruction
   let interpretCmd() =
       match pgm.[!ip] with
       | '>' -> ptr := !ptr + 1; AdvanceIp()
       | '<' -> ptr := !ptr - 1; AdvanceIp()
       | '+' -> input.[!ptr] <- input.[!ptr] + 1; AdvanceIp()
       | '-' -> input.[!ptr] <- input.[!ptr] - 1; AdvanceIp()
       | '.' -> printf "%c" (char input.[!ptr]); AdvanceIp()
       | ',' -> input.[!ptr] <- InputNumber(); AdvanceIp()
       | '[' -> SkipBrackets true
       | ']' -> SkipBrackets false
       | _ -> ignore(0)
   while !ip >= 0 && !ip < pgm.Length do
       interpretCmd()</lang>

Hello world in RCBF (stolen from the Wiki site): <lang fsharp>let pgmHelloWorld = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>." let tape = Array.create 5 0 Rcbf pgmHelloWorld tape</lang>

Haskell

See RCBF/Haskell.

Java

See RCBF/Java.

JavaScript

See RCBF/JavaScript.

Modula-3

See RCBF/Modula-3.

OCaml

See RCBF/OCaml.

Perl

See RCBF/Perl.

Python

See RCBF/Python.

Ruby

See RCBF/Ruby.

Standard ML

See RCBF/Standard ML.

TI-83 BASIC

See RCBF/TI-83 BASIC.

TI-89 BASIC

See RCBF/TI-89 BASIC.

Tcl

See RCBF/Tcl.