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:

edn10 B
["+" ?x 1]

input_text holds this document:

edn7 B
{:x 41}
rust222 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let program = stardust::build(&expression, None)?;
let input = stardust::parse(input_text, Format::EDN)?;
let result = program.evaluate(Some(&input), None)?;
Result / 11 B
{: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

OperationReturnsSummary
buildprogrambuild(expression) borrows the expression document and returns an owned program.
build_macroprogrambuild_macro(title, authored, arguments) binds AST arguments into an unstored macro and returns an owned program.
expand_macrodocumentexpand_macro(title, authored, arguments) returns the expanded expression document without building it.
Program::evaluatedocumentprogram.evaluate(input) returns an independent owned result document.
Program::closenoneRelease the program handle.
Database::expressionprogramBuild an owned program from a path or inline expression, expanding stored macros.

build

rust90 B
pub fn build(expression: &Document, explain: Option<&mut BuildExplain>) -> Result<Program>

Parameters

ParameterTypeOwnershipDefaultMeaning
expression&DocumentBorrowed for the callRequiredThe native expression AST to compile. The call borrows this document.
explainOption<&mut BuildExplain>Caller-owned outputAbsent skips explain outputOptional explain output. This record contains explain output for build.

Errors

StatusWhen
Status::COMPILE_FAILEDthe expression does not compile
Status::OUT_OF_MEMORYnative memory is exhausted

Notes

Expression documents.

Examples

Build an expression and evaluate its input

expression_text holds this document:

edn10 B
["+" ?x 1]

input_text holds this document:

edn7 B
{:x 41}
rust222 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let program = stardust::build(&expression, None)?;
let input = stardust::parse(input_text, Format::EDN)?;
let result = program.evaluate(Some(&input), None)?;
Result / 11 B
{:value 42}

Inspect one measured cost stage

expression_text holds this document:

edn10 B
["+" ?x 1]
rust211 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let mut explain = stardust::BuildExplain::default();
let _program = stardust::build(&expression, Some(&mut explain))?;
let result = explain.total;
Result / 75 B
{:value {:cpu         22
         :wall        23
         :arena_bytes 0}}

Explain expression compilation

expression_text holds this document:

edn10 B
["+" ?x 1]
rust205 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let mut explain = stardust::BuildExplain::default();
let _program = stardust::build(&expression, Some(&mut explain))?;
let result = explain;
Result / 513 B
{:value {:prepare_expression {:cpu         4
                              :wall        4
                              :arena_bytes 0}
         :compile            {:cpu         15
                              :wall        15
                              :arena_bytes 0}
         :total              {:cpu         23
                              :wall        23
                              :arena_bytes 0}
         :arena_capacity     65536
         :stages_completed   2
         :cache_hit          true}}

C exports: stardust_expr_build, stardust_expr_program_error

build_macro

rust93 B
pub fn build_macro(title: &str, authored: &Document, arguments: &Document) -> Result<Program>

Parameters

ParameterTypeOwnershipDefaultMeaning
title&strValueRequiredThe diagnostic title of this unstored macro.
authored&DocumentBorrowed for the callRequiredThe native authored macro definition with its declared parameters and expression body.
arguments&DocumentBorrowed for the callRequiredNative AST arguments bound to the macro's declared parameters before expansion.

Errors

StatusWhen
Status::INVALID_ARGUMENTthe arguments include an undeclared macro parameter
Status::INVALID_DEFINITIONthe macro is invalid or expands recursively
Status::NOT_FOUNDthe macro refers to an unknown macro
Status::COMPILE_FAILEDthe expansion does not compile
Status::OUT_OF_MEMORYnative memory is exhausted

Notes

Macro documents.

Examples

Bind an unstored macro and evaluate it

macro_text holds this document:

edn56 B
{:params  ["input"]
 :expands ["+" {:insert "input"} 1]}

arguments_text holds this document:

edn24 B
{:input ["*" ?amount 2]}

input_text holds this document:

edn12 B
{:amount 10}
rust306 B
let authored = stardust::parse(macro_text, Format::EDN)?;
let arguments = stardust::parse(arguments_text, Format::EDN)?;
let program = stardust::build_macro("twice-plus-one", &authored, &arguments)?;
let input = stardust::parse(input_text, Format::EDN)?;
let result = program.evaluate(Some(&input), None)?;
Result / 11 B
{:value 21}

C exports: stardust_macro_build, stardust_expr_program_error

expand_macro

rust95 B
pub fn expand_macro(title: &str, authored: &Document, arguments: &Document) -> Result<Document>

Parameters

ParameterTypeOwnershipDefaultMeaning
title&strValueRequiredThe diagnostic title of this unstored macro.
authored&DocumentBorrowed for the callRequiredThe native authored macro definition with its declared parameters and expression body.
arguments&DocumentBorrowed for the callRequiredNative AST arguments bound to the macro's declared parameters before expansion.

Errors

StatusWhen
Status::INVALID_ARGUMENTthe arguments include an undeclared macro parameter
Status::INVALID_DEFINITIONthe macro is invalid or expands recursively
Status::NOT_FOUNDthe macro refers to an unknown macro
Status::OUT_OF_MEMORYnative memory is exhausted

Notes

Macro documents.

Examples

Expand an unstored macro as a native document

macro_text holds this document:

edn56 B
{:params  ["input"]
 :expands ["+" {:insert "input"} 1]}

arguments_text holds this document:

edn24 B
{:input ["*" ?amount 2]}
rust199 B
let authored = stardust::parse(macro_text, Format::EDN)?;
let arguments = stardust::parse(arguments_text, Format::EDN)?;
let result = stardust::expand_macro("twice-plus-one", &authored, &arguments)?;
Result / 32 B
{:value ["+" ["*" ?amount 2] 1]}

C exports: stardust_macro_expand, stardust_document_error

Program::evaluate

rust134 B
pub fn evaluate(
        &self,
        input: Option<&Document>,
        explain: Option<&mut EvalExplain>,
    ) -> Result<Document>

Parameters

ParameterTypeOwnershipDefaultMeaning
self&SelfBorrowed for the callRequiredThe program owner for this operation. Keep it alive during the call.
inputOption<&Document>Borrowed for the callAbsentVariables for evaluation. Absent input uses an empty object.
explainOption<&mut EvalExplain>Caller-owned outputAbsent skips explain outputOptional explain output. This record contains explain output for evaluate.

Errors

Close consumes this owner. Rust ownership prevents later access. Thus, the safe API does not report closed for this receiver.

StatusWhen
Status::EVALUATION_FAILEDthe expression fails on the input
Status::OUT_OF_MEMORYnative memory is exhausted

Notes

Expression evaluation.

Examples

Build an expression and evaluate its input

expression_text holds this document:

edn10 B
["+" ?x 1]

input_text holds this document:

edn7 B
{:x 41}
rust222 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let program = stardust::build(&expression, None)?;
let input = stardust::parse(input_text, Format::EDN)?;
let result = program.evaluate(Some(&input), None)?;
Result / 11 B
{:value 42}

Explain expression evaluation

expression_text holds this document:

edn10 B
["+" ?x 1]

input_text holds this document:

edn7 B
{:x 41}
rust311 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let program = stardust::build(&expression, None)?;
let input = stardust::parse(input_text, Format::EDN)?;
let mut explain = stardust::EvalExplain::default();
let _output = program.evaluate(Some(&input), Some(&mut explain))?;
let result = explain;
Result / 632 B
{:value {:prepare_variables {:cpu         4
                             :wall        5
                             :arena_bytes 0}
         :evaluate          {:cpu         50
                             :wall        50
                             :arena_bytes 0}
         :adopt_document    {:cpu         5
                             :wall        5
                             :arena_bytes 0}
         :total             {:cpu         64
                             :wall        65
                             :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

Program::close

rust18 B
pub fn close(self)

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
selfSelfConsumed ownerRequiredThe program owner to release. Do not use it after this call.

Errors

Close consumes this owner. Rust ownership prevents later access. Thus, the safe API does not report closed for this receiver.

Examples

Build an expression and evaluate its input

expression_text holds this document:

edn10 B
["+" ?x 1]

input_text holds this document:

edn7 B
{:x 41}
rust239 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let program = stardust::build(&expression, None)?;
let input = stardust::parse(input_text, Format::EDN)?;
let result = program.evaluate(Some(&input), None)?;
program.close();
Result / 11 B
{:value 42}

C exports: stardust_expr_program_release

Database::expression

rust145 B
pub fn expression<'a>(
        &self,
        source: impl Into<Source<'a>>,
        explain: Option<&mut BuildExplain>,
    ) -> Result<Program>

Parameters

ParameterTypeOwnershipDefaultMeaning
self&SelfBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
sourceimpl Into<Source<'a>>ValueRequiredAn inline native definition or a stored definition path.
explainOption<&mut BuildExplain>Caller-owned outputAbsent skips explain outputOptional explain output. This record contains explain output for build.

Errors

StatusWhen
Status::NOT_FOUNDno expression is stored at the path
Status::INVALID_DEFINITIONa macro it calls is invalid or expands recursively
Status::COMPILE_FAILEDthe expanded expression does not compile
Status::CLOSEDthe database is closed
Status::OUT_OF_MEMORYnative 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.

Expression documents.

Examples

Build an expression with the database's macros and evaluate it

expression_text holds this document:

edn10 B
["+" ?x 1]

input_text holds this document:

edn7 B
{:x 41}
rust220 B
let expression = stardust::parse(expression_text, Format::EDN)?;
let program = db.expression(&expression, None)?;
let input = stardust::parse(input_text, Format::EDN)?;
let result = program.evaluate(Some(&input), None)?;
Result / 11 B
{:value 42}

C exports: stardust_db_expression_build, stardust_expr_program_error