On this page:
defn
defn_  meta.space
defn.macro
defn.sequence_  macro
defn_  meta.Group
defn_  meta.Sequence  Start  Group
defn_  meta.pack_  s_  exp
8.12

7.4 Definition Macros🔗ℹ

space

defn

The space for bindings of identifiers that can be used in definition positions.

Provided as meta.

A compile-time value that identifies the same space as defn. See also SpaceMeta.

definition

defn.macro prefix_macro_patterns

 

prefix_macro_patterns

 = 

'defined_name pattern ...':

  option; ...

  body

  ...

 | 

 | 'defined_name pattern ...':

     option; ...

     body

     ...

 | ...

 

defined_name

 = 

id

 | 

op

 | 

$('$')

 | 

(id_name)

 | 

(op_name)

 

option

 = 

~op_stx: id

 | 

~op_stx id

Defines defined_name as a macro in the expr space. The macro can be used in a definition context, where the compile-time body block returns the expansion result. The macro pattern is matched to an entire group in a definition context.

The expansion result must be a sequence of groups that are inlined in place of the definition-macro use.

The options in the macro body must be distinct; since there is only one option currently, either the ~op_stx option is present or not. The ~op_stx option, if present, binds an identifier for a use of the macro (which cannot be matched directly in the pattern, since that position is used for the name that defn.macro binds).

Using | alternatives, a single definition can have any number of patterns, which are tried in order. The defined_name must be the same across all cases.

> defn.macro 'enum:

                $lhs

                ...':

    let [n, ...] = List.iota([lhs, ...].length())

    'def $lhs = $n

     ...'

> enum:

    a

    b

    c

> b

1

definition

defn.sequence_macro 'defined_name pattern ...

                     pattern

                     ...':

  option; ...

  body

  ...

Similar to defn.macro, but defines a macro for a definition context that is matched against all of the remaining groups in the context, so the pattern is a multi-group pattern.

The macro result is two values: a sequence of groups to splice in place of the sequence-macro use, and a sequence of groups that represent the tail of the definition context that was not consumed.

> defn.sequence_macro 'reverse_defns

                       $defn1

                       $defn2

                       $tail

                       ...':

    values('$defn2; $defn1', '$tail; ...')

> reverse_defns

  def seq_x = seq_y+1

  def seq_y = 10

  seq_x

11

syntax class

syntax_class defn_meta.Group:

  kind: ~group

Provided as meta.

Syntax class that matches only groups that start with an identifier that is bound as a definition form.

Unlike expr_meta.Parsed, defn_meta.Group does not parse the definition form, because the form normally needs to be parsed within a definition context. This syntax class can be used to distinguish definitions from other forms that need to be treated differently in some context.

Provided as meta.

Syntax class that matches only groups that starts with an identifier that is bound as a definition-sequence form.

function

fun defn_meta.pack_s_exp(tree :: Any) :: Syntax

Provided as meta.

Similar to expr_meta.pack_s_exp, but for definitions.