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:

json23 B
[
  "+",
  "?x",
  1
]

input-text holds this document:

json14 B
{
  "x": 41
}
clojure175 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression)
            input (sd/parse input-text :json)]
  (sd/evaluate program input))
Result / 2 B
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.
evaluatedocumentprogram.evaluate(input) returns an independent owned result document.
closenoneRelease the program handle.
expressionprogramBuild an owned program from a path or inline expression, expanding stored macros.

build

clojure12 B
[expression]
clojure20 B
[expression options]

Parameters

ParameterTypeOwnershipDefaultMeaning
expressionDocumentBorrowed for the callRequiredThe native expression AST to compile. The call borrows this document.
optionsmapValue{}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

StatusWhen
:compile-failedthe expression does not compile
:out-of-memorynative memory is exhausted

Notes

Expression documents.

Examples

Build an expression and evaluate its input

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]

input-text holds this document:

json14 B
{
  "x": 41
}
clojure175 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression)
            input (sd/parse input-text :json)]
  (sd/evaluate program input))
Result / 2 B
42

Inspect one measured cost stage

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]
clojure148 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression {:explain? true})]
  (:total (sd/explain program)))
Result / 49 B
{
  "cpu": 52,
  "wall": 60,
  "arena_bytes": 0
}

Explain expression compilation

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]
clojure139 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression {:explain? true})]
  (sd/explain program))
Result / 299 B
{
  "prepare_expression": {
    "cpu": 7,
    "wall": 14,
    "arena_bytes": 0
  },
  "compile": {
    "cpu": 38,
    "wall": 38,
    "arena_bytes": 0
  },
  "total": {
    "cpu": 51,
    "wall": 58,
    "arena_bytes": 0
  },
  "arena_capacity": 65536,
  "stages_completed": 2,
  "cache_hit": true
}

C exports: stardust_expr_build, stardust_expr_program_error

build-macro

clojure26 B
[title authored arguments]

Parameters

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

Errors

StatusWhen
:invalid-argumentthe arguments include an undeclared macro parameter
:invalid-definitionthe macro is invalid or expands recursively
:not-foundthe macro refers to an unknown macro
:compile-failedthe expansion does not compile
:out-of-memorynative memory is exhausted

Notes

Macro documents.

Examples

Bind an unstored macro and evaluate it

macro-text holds this document:

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

arguments-text holds this document:

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

input-text holds this document:

json19 B
{
  "amount": 10
}
clojure253 B
(with-open [authored (sd/parse macro-text :json)
            arguments (sd/parse arguments-text :json)
            program (sd/build-macro "twice-plus-one" authored arguments)
            input (sd/parse input-text :json)]
  (sd/evaluate program input))
Result / 2 B
21

C exports: stardust_macro_build, stardust_expr_program_error

expand-macro

clojure26 B
[title authored arguments]

Parameters

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

Errors

StatusWhen
:invalid-argumentthe arguments include an undeclared macro parameter
:invalid-definitionthe macro is invalid or expands recursively
:not-foundthe macro refers to an unknown macro
:out-of-memorynative memory is exhausted

Notes

Macro documents.

Examples

Expand an unstored macro as a native document

macro-text holds this document:

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

arguments-text holds this document:

json51 B
{
  "input": [
    "*",
    "?amount",
    2
  ]
}
clojure160 B
(with-open [authored (sd/parse macro-text :json)
            arguments (sd/parse arguments-text :json)]
  (sd/expand-macro "twice-plus-one" authored arguments))
Result / 53 B
[
  "+",
  [
    "*",
    "?amount",
    2
  ],
  1
]

C exports: stardust_macro_expand, stardust_document_error

evaluate

clojure9 B
[program]
clojure15 B
[program input]
clojure23 B
[program input options]

Parameters

ParameterTypeOwnershipDefaultMeaning
programProgramBorrowed for the callRequiredThe program owner for this operation. Keep it alive during the call.
inputDocumentBorrowed for the callAbsentVariables for evaluation. Absent input uses an empty object.
optionsmapValue{}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

StatusWhen
:evaluation-failedthe expression fails on the input
:closedthe program is closed
:out-of-memorynative memory is exhausted

Notes

Expression evaluation.

Examples

Build an expression and evaluate its input

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]

input-text holds this document:

json14 B
{
  "x": 41
}
clojure175 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression)
            input (sd/parse input-text :json)]
  (sd/evaluate program input))
Result / 2 B
42

Explain expression evaluation

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]

input-text holds this document:

json14 B
{
  "x": 41
}
clojure231 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression)
            input (sd/parse input-text :json)
            output (sd/evaluate program input {:explain? true})]
  (sd/explain output))
Result / 383 B
{
  "prepare_variables": {
    "cpu": 9,
    "wall": 18,
    "arena_bytes": 0
  },
  "evaluate": {
    "cpu": 173,
    "wall": 174,
    "arena_bytes": 0
  },
  "adopt_document": {
    "cpu": 7,
    "wall": 7,
    "arena_bytes": 0
  },
  "total": {
    "cpu": 196,
    "wall": 205,
    "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

clojure8 B
[handle]

Consumes the receiver handle.

Parameters

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

Examples

Build an expression and evaluate its input

expression-text holds this document:

json23 B
[
  "+",
  "?x",
  1
]

input-text holds this document:

json14 B
{
  "x": 41
}
clojure175 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/build expression)
            input (sd/parse input-text :json)]
  (sd/evaluate program input))
Result / 2 B
42

C exports: stardust_expr_program_release

expression

clojure11 B
[db source]
clojure19 B
[db source options]

Parameters

ParameterTypeOwnershipDefaultMeaning
dbDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
sourceSourceValueRequiredAn inline native definition or a stored definition path.
optionsmapValue{}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

StatusWhen
:not-foundno expression is stored at the path
:invalid-definitiona macro it calls is invalid or expands recursively
:compile-failedthe expanded expression does not compile
:closedthe database is closed
: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:

json23 B
[
  "+",
  "?x",
  1
]

input-text holds this document:

json14 B
{
  "x": 41
}
clojure183 B
(with-open [expression (sd/parse expression-text :json)
            program (sd/expression db expression)
            input (sd/parse input-text :json)]
  (sd/evaluate program input))
Result / 2 B
42

C exports: stardust_db_expression_build, stardust_expr_program_error