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

expressionText holds this document:

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

inputText holds this document:

json14 B
{
  "x": 41
}
go422 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

program, err := stardust.Build(expression, nil)
if err != nil {
	return err
}
defer program.Close()

result, err := program.Evaluate(input, nil)
if err != nil {
	return err
}
defer result.Close()
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.
BuildMacroprogrambuild_macro(title, authored, arguments) binds AST arguments into an unstored macro and returns an owned program.
ExpandMacrodocumentexpand_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

go73 B
func Build(expression *Document, explain *BuildExplain) (*Program, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
expression*DocumentBorrowed for the callRequiredThe native expression AST to compile. The call borrows this document.
explain*BuildExplainCaller-owned outputAbsent skips explain outputOptional explain output. This record contains explain output for build.

Errors

StatusWhen
StatusCompileFailedthe expression does not compile
StatusOutOfMemorynative memory is exhausted

Notes

Expression documents.

Examples

Build an expression and evaluate its input

expressionText holds this document:

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

inputText holds this document:

json14 B
{
  "x": 41
}
go422 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

program, err := stardust.Build(expression, nil)
if err != nil {
	return err
}
defer program.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

result, err := program.Evaluate(input, nil)
if err != nil {
	return err
}
defer result.Close()
Result / 2 B
42

Inspect one measured cost stage

expressionText holds this document:

json23 B
[
  "+",
  "?x",
  1
]
go284 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

var explain stardust.BuildExplain
program, err := stardust.Build(expression, &explain)
if err != nil {
	return err
}
defer program.Close()

result := explain.Total
Result / 49 B
{
  "cpu": 17,
  "wall": 18,
  "arena_bytes": 0
}

Explain expression compilation

expressionText holds this document:

json23 B
[
  "+",
  "?x",
  1
]
go278 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

var explain stardust.BuildExplain
program, err := stardust.Build(expression, &explain)
if err != nil {
	return err
}
defer program.Close()

result := explain
Result / 298 B
{
  "prepare_expression": {
    "cpu": 2,
    "wall": 3,
    "arena_bytes": 0
  },
  "compile": {
    "cpu": 11,
    "wall": 11,
    "arena_bytes": 0
  },
  "total": {
    "cpu": 15,
    "wall": 16,
    "arena_bytes": 0
  },
  "arena_capacity": 65536,
  "stages_completed": 2,
  "cache_hit": true
}

C exports: stardust_expr_build, stardust_expr_program_error

BuildMacro

go78 B
func BuildMacro(title string, authored, arguments *Document) (*Program, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
titlestringValueRequiredThe 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
StatusInvalidArgumentthe arguments include an undeclared macro parameter
StatusInvalidDefinitionthe macro is invalid or expands recursively
StatusNotFoundthe macro refers to an unknown macro
StatusCompileFailedthe expansion does not compile
StatusOutOfMemorynative memory is exhausted

Notes

Macro documents.

Examples

Bind an unstored macro and evaluate it

macroText holds this document:

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

argumentsText holds this document:

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

inputText holds this document:

json19 B
{
  "amount": 10
}
go549 B
macro, err := stardust.Parse(macroText, stardust.JSON)
if err != nil {
	return err
}
defer macro.Close()

arguments, err := stardust.Parse(argumentsText, stardust.JSON)
if err != nil {
	return err
}
defer arguments.Close()

program, err := stardust.BuildMacro("twice-plus-one", macro, arguments)
if err != nil {
	return err
}
defer program.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

result, err := program.Evaluate(input, nil)
if err != nil {
	return err
}
defer result.Close()
Result / 2 B
21

C exports: stardust_macro_build, stardust_expr_program_error

ExpandMacro

go80 B
func ExpandMacro(title string, authored, arguments *Document) (*Document, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
titlestringValueRequiredThe 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
StatusInvalidArgumentthe arguments include an undeclared macro parameter
StatusInvalidDefinitionthe macro is invalid or expands recursively
StatusNotFoundthe macro refers to an unknown macro
StatusOutOfMemorynative memory is exhausted

Notes

Macro documents.

Examples

Expand an unstored macro as a native document

macroText holds this document:

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

argumentsText holds this document:

json51 B
{
  "input": [
    "*",
    "?amount",
    2
  ]
}
go346 B
macro, err := stardust.Parse(macroText, stardust.JSON)
if err != nil {
	return err
}
defer macro.Close()

arguments, err := stardust.Parse(argumentsText, stardust.JSON)
if err != nil {
	return err
}
defer arguments.Close()

result, err := stardust.ExpandMacro("twice-plus-one", macro, arguments)
if err != nil {
	return err
}
defer result.Close()
Result / 53 B
[
  "+",
  [
    "*",
    "?amount",
    2
  ],
  1
]

C exports: stardust_macro_expand, stardust_document_error

Program.Evaluate

go84 B
func (p *Program) Evaluate(input *Document, explain *EvalExplain) (*Document, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
input*DocumentBorrowed for the callAbsentVariables for evaluation. Absent input uses an empty object.
explain*EvalExplainCaller-owned outputAbsent skips explain outputOptional explain output. This record contains explain output for evaluate.

Errors

StatusWhen
StatusEvaluationFailedthe expression fails on the input
StatusClosedthe program is closed
StatusOutOfMemorynative memory is exhausted

Notes

Expression evaluation.

Examples

Build an expression and evaluate its input

expressionText holds this document:

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

inputText holds this document:

json14 B
{
  "x": 41
}
go422 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

program, err := stardust.Build(expression, nil)
if err != nil {
	return err
}
defer program.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

result, err := program.Evaluate(input, nil)
if err != nil {
	return err
}
defer result.Close()
Result / 2 B
42

Explain expression evaluation

expressionText holds this document:

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

inputText holds this document:

json14 B
{
  "x": 41
}
go458 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

program, err := stardust.Build(expression, nil)
if err != nil {
	return err
}
defer program.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

var result stardust.EvalExplain
output, err := program.Evaluate(input, &result)
if err != nil {
	return err
}
defer output.Close()
Result / 378 B
{
  "prepare_variables": {
    "cpu": 2,
    "wall": 3,
    "arena_bytes": 0
  },
  "evaluate": {
    "cpu": 35,
    "wall": 35,
    "arena_bytes": 0
  },
  "adopt_document": {
    "cpu": 3,
    "wall": 4,
    "arena_bytes": 0
  },
  "total": {
    "cpu": 44,
    "wall": 45,
    "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

go25 B
func (p *Program) Close()

Consumes the receiver handle.

Examples

Build an expression and evaluate its input

expressionText holds this document:

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

inputText holds this document:

json14 B
{
  "x": 41
}
go439 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

program, err := stardust.Build(expression, nil)
if err != nil {
	return err
}
defer program.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

result, err := program.Evaluate(input, nil)
if err != nil {
	return err
}
defer result.Close()

program.Close()
Result / 2 B
42

C exports: stardust_expr_program_release

Database.Expression

go92 B
func (d *Database) Expression(definition *Document, explain *BuildExplain) (*Program, error)
go87 B
func (d *Database) ExpressionPath(path string, explain *BuildExplain) (*Program, error)

Parameters

ParameterTypeOwnershipDefaultMeaning
definition*DocumentBorrowed for the callRequiredThe inline native expression document compiled into the program.
explain*BuildExplainCaller-owned outputAbsent skips explain outputOptional explain output. This record contains explain output for build.
pathstringValueRequiredThe stored expression definition path.

Errors

StatusWhen
StatusNotFoundno expression is stored at the path
StatusInvalidDefinitiona macro it calls is invalid or expands recursively
StatusCompileFailedthe expanded expression does not compile
StatusClosedthe database is closed
StatusOutOfMemorynative 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

expressionText holds this document:

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

inputText holds this document:

json14 B
{
  "x": 41
}
go421 B
expression, err := stardust.Parse(expressionText, stardust.JSON)
if err != nil {
	return err
}
defer expression.Close()

program, err := db.Expression(expression, nil)
if err != nil {
	return err
}
defer program.Close()

input, err := stardust.Parse(inputText, stardust.JSON)
if err != nil {
	return err
}
defer input.Close()

result, err := program.Evaluate(input, nil)
if err != nil {
	return err
}
defer result.Close()
Result / 2 B
42

C exports: stardust_db_expression_build, stardust_expr_program_error