Tiny Programs

Brainfuck in r

Source
Contributed by spasticus74

Implementation

An implementation of the Brainfuck programming language.

See https://en.wikipedia.org/wiki/Brainfuck for details.

args <- commandArgs(trailingOnly=TRUE)

program <- strsplit(readChar(args[1], file.info(args[1])$size), "")[[1]]

dataStack <- c(rep(0, 30000))
dataPointer <- 1

instructionStack <- c()
instructionPointer <- 1

while (instructionPointer < (length(program) + 1)) {
  switch (program[instructionPointer],
    '>' = dataPointer <- dataPointer + 1,
    '<' = dataPointer <- dataPointer - 1,
    '+' = dataStack[dataPointer] <- dataStack[dataPointer] + 1,
    '-' = dataStack[dataPointer] <- dataStack[dataPointer] - 1,
    '.' = cat(rawToChar(as.raw(dataStack[dataPointer]))),
    ',' = {
      c <- readChar(con = file("stdin"), nchars = 1)
      dataStack[dataPointer] <- c
    },
    '[' = {
      stack <- 1
      end <- instructionPointer + 1
      repeat {
        switch(program[end],
               '[' = stack <- stack + 1,
               ']' = {
                 stack <- stack - 1
                 if(stack == 0) {
                   break
                 }
               }
               )
        end <- end + 1
      }
      if(dataStack[dataPointer] == 0) {
        instructionPointer <- end
      } else {
        instructionStack <- append(instructionStack, instructionPointer - 1)
      }
    },
    ']' = {
      if(dataStack[dataPointer] != 0) {
        instructionPointer <- instructionStack[length(instructionStack)]
      }
      instructionStack <- instructionStack[1:(length(instructionStack)-1)]
    }
  )
  instructionPointer <- instructionPointer + 1
}

Build and run

These steps are linux only.


$ Rscript bf.R $myProgram

All implementations