knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

RcppBrainfuck

Lifecycle: experimental

The goal of RcppBrainfuck is to compile brainfuck code to C++ code that can be run from R.

NOT ENTERPRISE READY

Installation

You can install the released version of RcppBrainfuck from CRAN with:

remotes::install_github("dirkschumacher/RcppBrainfuck")

The package essentially just implements the translated brainfuck <-> C OP codes from Wikipedia. It is just for fun and was an evening hack.

Example

This is a basic example which shows you how to solve a common problem:

library(RcppBrainfuck)

Here is a "Hello World" program taken from Wikipedia:

code <- "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
hello <- compile(code)
invisible(hello())

But you can also communicate with the Brainfuck code using a shared Memory.

# add 2 to the first cell and then add 3 to the second cell
# and then while the second cell is not 0, decrement it and increment the first one
code <- "++>+++[<+>-]" 
add <- compile(code)
cells <- add()
as.integer(cells[[1L]])

In order to input values we need to construct our own cell raw array and pass it to the function.

cells2 <- add(cells)
# passing in 7 for the first value we expect 10 as the final result
as.integer(cells2[[1L]])

References

Related Projects



dirkschumacher/RcppBrainfuck documentation built on Nov. 4, 2019, 10:32 a.m.