How to resolve the algorithm Execute Brain step by step in the R programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Execute Brain step by step in the R programming language

Table of Contents

Problem Statement

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: Any cell size is allowed,   EOF   (End-O-File)   support is optional, as is whether you have bounded or unbounded memory.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Execute Brain step by step in the R programming language

Source code in the brainfuc programming language

bf <- function(code) {
  instructions <- strsplit(code, "")[[1]]
  tape <- c()
  visited <- c()

  pset <- function(n) {
    if (n %in% visited)
      p <<- n
    else {
      visited[length(visited)+1] <<- n
      tape[as.character(n)] <<- 0
      pset(n)
    }
  }

  bracket <- function(b1, b2, x) {
    nest <- 1
    j <- i + x
    while (nest != 0) {
      if (instructions[j] == b1)
        nest <- nest + 1
      if (instructions[j] == b2)
        nest <- nest - 1
      j <- j + x
    }
    i <<- j    
  }

  pset(0)
  i <- 1
  while (i <= length(instructions)) {
    p_ <- as.character(p)
    c <- instructions[i]
    switch(c,
           ">" = pset(p + 1),
           "<" = pset(p - 1),
           "+" = tape[p_] <- tape[p_] + 1,
           "-" = tape[p_] <- tape[p_] - 1,
           "." = cat(intToUtf8(tape[p_])),
           # TODO: IMPLEMENT ","
           "[" = if (tape[p_] == 0) {
                   bracket("[", "]", 1)
                   i <- i - 1 # off by one error
                 },
           "]" = bracket("]", "[", -1))
    i <- i + 1
  }
}

bf("++++++++++[>+>+++>+++++++>++++++++++<<<<-]>>>++.>+.+++++++..+++.<<++.>+++++++++++++++.>.+++.------.--------.<<+.<.")


  

You may also check:How to resolve the algorithm Play recorded sounds step by step in the Delphi programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Objeck programming language
You may also check:How to resolve the algorithm Filter step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the min programming language
You may also check:How to resolve the algorithm Terminal control/Unicode output step by step in the zkl programming language