Skip to contents

Produces a flat instruction stream suitable for run_bytecode(). Compilation is a post-order tree walk; each leaf emits a LIT or VAR, each interior node emits an EML. The constants pool deduplicates identical literals (eml(1, eml(1, x)) has one shared entry for 1).

Usage

compile_eml(expr)

Arguments

expr

an EML expression.

Value

A list with elements:

ops

integer vector of opcodes (0 LIT, 1 VAR, 2 EML).

args

integer vector of opcode arguments (constant index for LIT, variable index for VAR, 0 for EML).

consts

complex vector — the deduplicated constants pool.

vars

character vector — variable names in declaration order.

max_stack

integer — the peak stack depth required.

source

the original expr, for inspection.

See also

run_bytecode() for executing the compiled output; eml_eval() for the slower tree-walking evaluator.

Examples

bc <- compile_eml(quote(eml(1, eml(eml(1, x), 1))))
bc$ops # 0 0 1 2 0 2 2
#> [1] 0 0 1 2 0 2 2
bc$consts # 1+0i
#> [1] 1+0i
bc$vars # "x"
#> [1] "x"