Title: | R Byte Code Assembler/Disassembler |
---|---|
Description: | Assembler/Disassembler for R's byte code. |
Authors: | Mike Cheng [aut, cre, cph] |
Maintainer: | Mike Cheng <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-10-25 02:44:06 UTC |
Source: | https://github.com/coolbutuseless/rbytecode |
Convert a bytecode data.frame to a text representation
## S3 method for class 'bcdf' as.character(x, incl_expr = FALSE, ...)
## S3 method for class 'bcdf' as.character(x, incl_expr = FALSE, ...)
x |
bytecode data.frame produced by |
incl_expr |
Include the stored expression? Default: FALSE |
... |
ignored |
## Not run: as.character(disq(1 + 1)) ## End(Not run)
## Not run: as.character(disq(1 + 1)) ## End(Not run)
Compile bytecode assembly into an R bytecode object
asm(code, expr_default = quote(stop("[rbytecode]")))
asm(code, expr_default = quote(stop("[rbytecode]")))
code |
R bytecode assembly code as a single string |
expr_default |
The default expression stored with the bytecode. In general this should be a quoted 'call' object, and if you are running a standard modern version of R, then it will almost never get evaluated/called. Default: quote(stop("[rbytecode]")). |
bytecode object
## Not run: code <- "LDCONST 1\nLDCONST 2\nADD\nRETURN" bc <- asm(code) eval(bc) # 3 ## End(Not run)
## Not run: code <- "LDCONST 1\nLDCONST 2\nADD\nRETURN" bc <- asm(code) eval(bc) # 3 ## End(Not run)
Compile a bytecode data.frame to an executable R bytecode object
compile_bcdf(bcdf, expr_default = quote(stop("[rbytecode]")))
compile_bcdf(bcdf, expr_default = quote(stop("[rbytecode]")))
bcdf |
bytecode data.frame |
expr_default |
The default expression stored with the bytecode. In general this should be a quoted 'call' object, and if you are running a standard modern version of R, then it will almost never get evaluated/called. Default: quote(stop("[rbytecode]")). |
bytecode object
## Not run: code <- "LDCONST 1\nLDCONST 2\nADD\nRETURN" bcdf <- parse_code(code) bc <- compile_bcdf(bcdf) eval(bc) ## End(Not run)
## Not run: code <- "LDCONST 1\nLDCONST 2\nADD\nRETURN" bcdf <- parse_code(code) bc <- compile_bcdf(bcdf) eval(bc) ## End(Not run)
Disassemble R objects to a bytecode data.frame
dis(x, incl_expr = FALSE, depth = 0L, env = NULL)
dis(x, incl_expr = FALSE, depth = 0L, env = NULL)
x |
Possible inputs: language object, call object, compiled bytecode, functionquoted expression. |
incl_expr |
include the expression index in the output. Default: FALSE. This is an advanced feature |
depth |
internal variable tracking recursion depth. Not usually set by user. |
env |
environment for keeping track of labels. Not usually set by user. |
bytecode data.frame (bcdf
)
## Not run: dis(quote(1 + x)) fc <- compiler::cmpfun(\(x) {2 * x + 1}) dis(fc) ## End(Not run)
## Not run: dis(quote(1 + x)) fc <- compiler::cmpfun(\(x) {2 * x + 1}) dis(fc) ## End(Not run)
dis()
which accepts unquoted expressions.A wrapper for dis()
which accepts unquoted expressions.
disq(x, incl_expr = FALSE)
disq(x, incl_expr = FALSE)
x |
unquoted expression |
incl_expr |
include the expression index in the output. Default: FALSE. This is an advanced feature |
## Not run: disq(1 + 3) dis(quote(1 + 3)) ## End(Not run)
## Not run: disq(1 + 3) dis(quote(1 + 3)) ## End(Not run)
R has many SPECIAL functions in the base R package that should be called in bytecode with CALLSPECIAL
eval_special(bc)
eval_special(bc)
bc |
bytecode object to be evaluated |
But given it's a bit difficult to construct a CALLSPECIAL without just writing actual R code, there are some SPECIALs which can be handled by wrapping the special functions in a non-special wrapper.
Information about all bytecode operators
ops
ops
An object of class list
of length 129.
Parse R bytecode assembly into a bytecode data.frame
parse_code(code)
parse_code(code)
code |
Single string containing bytecode instructions |
bytecode data.frame (bcdf
)
## Not run: code <- "LDCONST 1\nLDCONST 2\nADD\nRETURN" parse_code(code) ## End(Not run)
## Not run: code <- "LDCONST 1\nLDCONST 2\nADD\nRETURN" parse_code(code) ## End(Not run)
Print a bytecode data.frame
## S3 method for class 'bcdftxt' print(x, ...)
## S3 method for class 'bcdftxt' print(x, ...)
x |
Character representation of a bytecode data.frame |
... |
arguments passed to |
## Not run: bcdf <- disq(1 + x) txt <- as.character(bcdf) txt ## End(Not run)
## Not run: bcdf <- disq(1 + x) txt <- as.character(bcdf) txt ## End(Not run)