The goal of RcppBrainfuck
is to compile
brainfuck code to C++ code
that can be run from R.
NOT ENTERPRISE READY
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.
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())
#> Hello World!
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]])
#> [1] 5
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]])
#> [1] 10
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.