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
}
python228 B
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression)) as program, \
        (await stardust.parse(input_text, "json")) as input:
    result = (await program.evaluate(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.
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

python79 B
def build(expression: Document, explain: BuildExplain | None = None) -> Program

Parameters

ParameterTypeOwnershipDefaultMeaning
expressionDocumentBorrowed for the callRequiredThe native expression AST to compile. The call borrows this document.
explainBuildExplain | NoneCaller-owned outputNoneOptional explain output. This record contains explain output for build.

Errors

StatusWhen
"compile_failed"the expression does not compile
"out_of_memory"native 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
}
python228 B
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression)) as program, \
        (await stardust.parse(input_text, "json")) as input:
    result = (await program.evaluate(input))
Result / 2 B
42

Inspect one measured cost stage

expression_text holds this document:

json23 B
[
  "+",
  "?x",
  1
]
python179 B
explain = stardust.BuildExplain()
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression, explain)):
    result = explain.total
Result / 49 B
{
  "cpu": 40,
  "wall": 50,
  "arena_bytes": 0
}

Explain expression compilation

expression_text holds this document:

json23 B
[
  "+",
  "?x",
  1
]
python173 B
explain = stardust.BuildExplain()
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression, explain)):
    result = explain
Result / 302 B
{
  "prepare_expression": {
    "cpu": 10,
    "wall": 20,
    "arena_bytes": 0
  },
  "compile": {
    "cpu": 70,
    "wall": 60,
    "arena_bytes": 0
  },
  "total": {
    "cpu": 100,
    "wall": 110,
    "arena_bytes": 0
  },
  "arena_capacity": 65536,
  "stages_completed": 2,
  "cache_hit": true
}

C exports: stardust_expr_build, stardust_expr_program_error

build_macro

python79 B
def build_macro(title: str, authored: Document, arguments: Document) -> Program

Parameters

ParameterTypeOwnershipDefaultMeaning
titlestrValueRequiredThe 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_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

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
}
python325 B
async with (await stardust.parse(macro_text, "json")) as authored, (await stardust.parse(arguments_text, "json")) as arguments, \
        (await stardust.build_macro("twice-plus-one", authored, arguments)) as program, \
        (await stardust.parse(input_text, "json")) as input:
    result = (await program.evaluate(input))
Result / 2 B
21

C exports: stardust_macro_build, stardust_expr_program_error

expand_macro

python81 B
def expand_macro(title: str, authored: Document, arguments: Document) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
titlestrValueRequiredThe 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_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

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
  ]
}
python209 B
async with (await stardust.parse(macro_text, "json")) as authored, (await stardust.parse(arguments_text, "json")) as arguments:
    result = (await stardust.expand_macro("twice-plus-one", authored, arguments))
Result / 53 B
[
  "+",
  [
    "*",
    "?amount",
    2
  ],
  1
]

C exports: stardust_macro_expand, stardust_document_error

Program.evaluate

python97 B
def evaluate(self, input: Document | None = None, explain: EvalExplain | None = None) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
selfProgramBorrowed for the callRequiredThe program owner for this operation. Keep it alive during the call.
inputDocument | NoneBorrowed for the callNoneVariables for evaluation. Absent input uses an empty object.
explainEvalExplain | NoneCaller-owned outputNoneOptional explain output. This record contains explain output for evaluate.

Errors

StatusWhen
"evaluation_failed"the expression fails on the input
"closed"the program is closed
"out_of_memory"native 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
}
python228 B
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression)) as program, \
        (await stardust.parse(input_text, "json")) as input:
    result = (await program.evaluate(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
}
python279 B
explain = stardust.EvalExplain()
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression)) as program, \
        (await stardust.parse(input_text, "json")) as input, (await program.evaluate(input, explain)):
    result = explain
Result / 386 B
{
  "prepare_variables": {
    "cpu": 10,
    "wall": 20,
    "arena_bytes": 0
  },
  "evaluate": {
    "cpu": 280,
    "wall": 280,
    "arena_bytes": 0
  },
  "adopt_document": {
    "cpu": 10,
    "wall": 20,
    "arena_bytes": 0
  },
  "total": {
    "cpu": 320,
    "wall": 330,
    "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

python23 B
def close(self) -> None

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
selfProgramConsumed 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
}
python228 B
async with (await stardust.parse(expression_text, "json")) as expression, (await stardust.build(expression)) as program, \
        (await stardust.parse(input_text, "json")) as input:
    result = (await program.evaluate(input))
Result / 2 B
42

C exports: stardust_expr_program_release

Database.expression

python92 B
def expression(self, source: str | Document, explain: BuildExplain | None = None) -> Program

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDatabaseBorrowed for the callRequiredThe database owner for this operation. Keep it alive during the call.
sourcestr | DocumentValueRequiredAn inline native definition or a stored definition path.
explainBuildExplain | NoneCaller-owned outputNoneOptional explain output. This record contains explain output for build.

Errors

StatusWhen
"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.

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
}
python227 B
async with (await stardust.parse(expression_text, "json")) as expression, (await db.expression(expression)) as program, \
        (await stardust.parse(input_text, "json")) as input:
    result = (await program.evaluate(input))
Result / 2 B
42

C exports: stardust_db_expression_build, stardust_expr_program_error