Expressions and macros
An expression is a document. The example builds an expression program once and evaluates it with an input document. The macro example expands an expression before compilation and evaluation.
Example
Build an expression and evaluate it
expression-text holds this document:
["+" ?x 1]input-text holds this document:
{:x 41}(with-open [expression (sd/parse expression-text :edn)
program (sd/build expression)
input (sd/parse input-text :edn)]
(sd/evaluate program input)){:value 42}Reusing programs
A program keeps the expression that you built, independently of any one evaluation.
- You can evaluate one program concurrently with separate input documents.
- Keep the program until its evaluations finish.
- Then release it under the ownership rule of its operation.
Operations
| Operation | Returns | Summary |
|---|---|---|
| build | program | build(expression) borrows the expression document and returns an owned program. |
| build-macro | program | build_macro(title, authored, arguments) binds AST arguments into an unstored macro and returns an owned program. |
| expand-macro | document | expand_macro(title, authored, arguments) returns the expanded expression document without building it. |
| evaluate | document | program.evaluate(input) returns an independent owned result document. |
| close | none | Release the program handle. |
| expression | program | Build an owned program from a path or inline expression, expanding stored macros. |
build
[expression][expression options]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
expression | Document | Borrowed for the call | Required | The native expression AST to compile. The call borrows this document. |
options | map | Value | {} | Binding options map. The :library key selects the loaded native library. Omission uses the binding's default library. Optional explain output. This record contains explain output for build. |
Errors
| Status | When |
|---|---|
:compile-failed | the expression does not compile |
:out-of-memory | native memory is exhausted |
Notes
Examples
Build an expression and evaluate its input
expression-text holds this document:
["+" ?x 1]input-text holds this document:
{:x 41}(with-open [expression (sd/parse expression-text :edn)
program (sd/build expression)
input (sd/parse input-text :edn)]
(sd/evaluate program input)){:value 42}Inspect one measured cost stage
expression-text holds this document:
["+" ?x 1](with-open [expression (sd/parse expression-text :edn)
program (sd/build expression {:explain? true})]
(:total (sd/explain program))){:value {:cpu 63
:wall 69
:arena_bytes 0}}Explain expression compilation
expression-text holds this document:
["+" ?x 1](with-open [expression (sd/parse expression-text :edn)
program (sd/build expression {:explain? true})]
(sd/explain program)){:value {:prepare_expression {:cpu 7
:wall 13
:arena_bytes 0}
:compile {:cpu 40
:wall 40
:arena_bytes 0}
:total {:cpu 53
:wall 59
:arena_bytes 0}
:arena_capacity 65536
:stages_completed 2
:cache_hit true}}C exports: stardust_expr_build, stardust_expr_program_error
build-macro
[title authored arguments]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
title | Name | Value | Required | The diagnostic title of this unstored macro. |
authored | Document | Borrowed for the call | Required | The native authored macro definition with its declared parameters and expression body. |
arguments | Document | Borrowed for the call | Required | Native AST arguments bound to the macro's declared parameters before expansion. |
Errors
| Status | When |
|---|---|
:invalid-argument | the arguments include an undeclared macro parameter |
:invalid-definition | the macro is invalid or expands recursively |
:not-found | the macro refers to an unknown macro |
:compile-failed | the expansion does not compile |
:out-of-memory | native memory is exhausted |
Notes
Examples
Bind an unstored macro and evaluate it
macro-text holds this document:
{:params ["input"]
:expands ["+" {:insert "input"} 1]}arguments-text holds this document:
{:input ["*" ?amount 2]}input-text holds this document:
{:amount 10}(with-open [authored (sd/parse macro-text :edn)
arguments (sd/parse arguments-text :edn)
program (sd/build-macro "twice-plus-one" authored arguments)
input (sd/parse input-text :edn)]
(sd/evaluate program input)){:value 21}C exports: stardust_macro_build, stardust_expr_program_error
expand-macro
[title authored arguments]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
title | Name | Value | Required | The diagnostic title of this unstored macro. |
authored | Document | Borrowed for the call | Required | The native authored macro definition with its declared parameters and expression body. |
arguments | Document | Borrowed for the call | Required | Native AST arguments bound to the macro's declared parameters before expansion. |
Errors
| Status | When |
|---|---|
:invalid-argument | the arguments include an undeclared macro parameter |
:invalid-definition | the macro is invalid or expands recursively |
:not-found | the macro refers to an unknown macro |
:out-of-memory | native memory is exhausted |
Notes
Examples
Expand an unstored macro as a native document
macro-text holds this document:
{:params ["input"]
:expands ["+" {:insert "input"} 1]}arguments-text holds this document:
{:input ["*" ?amount 2]}(with-open [authored (sd/parse macro-text :edn)
arguments (sd/parse arguments-text :edn)]
(sd/expand-macro "twice-plus-one" authored arguments)){:value ["+" ["*" ?amount 2] 1]}C exports: stardust_macro_expand, stardust_document_error
evaluate
[program][program input][program input options]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
program | Program | Borrowed for the call | Required | The program owner for this operation. Keep it alive during the call. |
input | Document | Borrowed for the call | Absent | Variables for evaluation. Absent input uses an empty object. |
options | map | Value | {} | Binding options map. The :library key selects the loaded native library. Omission uses the binding's default library. Optional explain output. This record contains explain output for evaluate. |
Errors
| Status | When |
|---|---|
:evaluation-failed | the expression fails on the input |
:closed | the program is closed |
:out-of-memory | native memory is exhausted |
Notes
Examples
Build an expression and evaluate its input
expression-text holds this document:
["+" ?x 1]input-text holds this document:
{:x 41}(with-open [expression (sd/parse expression-text :edn)
program (sd/build expression)
input (sd/parse input-text :edn)]
(sd/evaluate program input)){:value 42}Explain expression evaluation
expression-text holds this document:
["+" ?x 1]input-text holds this document:
{:x 41}(with-open [expression (sd/parse expression-text :edn)
program (sd/build expression)
input (sd/parse input-text :edn)
output (sd/evaluate program input {:explain? true})]
(sd/explain output)){:value {:prepare_variables {:cpu 10
:wall 18
:arena_bytes 0}
:evaluate {:cpu 136
:wall 136
:arena_bytes 0}
:adopt_document {:cpu 6
:wall 6
:arena_bytes 0}
:total {:cpu 158
:wall 166
:arena_bytes 0}
:arena_capacity 65536
:document_bytes 42
:stages_completed 3}}C exports: stardust_expr_evaluate, stardust_result_take_document, stardust_result_error, stardust_result_release
close
[handle]Consumes the receiver handle.
Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
handle | Program | Consumed owner | Required | The program owner to release. Do not use it after this call. |
Examples
Build an expression and evaluate its input
expression-text holds this document:
["+" ?x 1]input-text holds this document:
{:x 41}(with-open [expression (sd/parse expression-text :edn)
program (sd/build expression)
input (sd/parse input-text :edn)]
(sd/evaluate program input)){:value 42}C exports: stardust_expr_program_release
expression
[db source][db source options]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
db | Database | Borrowed for the call | Required | The database owner for this operation. Keep it alive during the call. |
source | Source | Value | Required | An inline native definition or a stored definition path. |
options | map | Value | {} | Binding options map. The :library key selects the loaded native library. Omission uses the binding's default library. Optional explain output. This record contains explain output for build. |
Errors
| Status | When |
|---|---|
:not-found | no expression is stored at the path |
:invalid-definition | a macro it calls is invalid or expands recursively |
:compile-failed | the expanded expression does not compile |
:closed | the database is closed |
:out-of-memory | native memory is exhausted |
Notes
The program keeps the expression and macros selected at build time and survives database closure. Build again to use later definition changes.
Examples
Build an expression with the database's macros and evaluate it
expression-text holds this document:
["+" ?x 1]input-text holds this document:
{:x 41}(with-open [expression (sd/parse expression-text :edn)
program (sd/expression db expression)
input (sd/parse input-text :edn)]
(sd/evaluate program input)){:value 42}C exports: stardust_db_expression_build, stardust_expr_program_error