nrc aims to be an R version of 9cc: R compiler written in R.
You can install nrc from github with:
# install.packages("remotes")
remotes::install_github("igjit/nrc")
Note: currently nrc only works on Linux.
Try in Docker:
$ docker run --rm -it rocker/tidyverse:3.6.2 R -q
> remotes::install_github("igjit/nrc")
library(nrc)
compile("1 + 2")
#> .intel_syntax noprefix
#> .global main
#> main:
#> push rbp
#> mov rbp, rsp
#> sub rsp, 0
#> push 1
#> push 2
#> pop rdi
#> pop rax
#> add rax, rdi
#> push rax
#> pop rax
#> mov rsp, rbp
#> pop rbp
#> ret
compile("1 + 2") %>% assemble() %>% execute()
#> [1] 3
compile("a <- 2; a * 3") %>% assemble() %>% execute()
#> [1] 6
compile("add2 <- function(x) x + 2; add2(40)") %>% assemble() %>% execute()
#> [1] 42