On this page:
assign.macro
assign_  meta.unpack_  left
assign_  meta.pack_  assignment
assign_  meta.Assign  Parsed
8.12

7.7 Assignment Macros🔗ℹ

Like expr.macro, but defines an infix assignment operator, similar to :=. An assignment operator is used in combination with mutable variables and forms like . and like [] via #%index. The macro_pattern is constrained to an infix-operator pattern, since the cooperating forms always expect an infix use. When the macro_pattern would imply an already-parsed right-hand expression for macro or expr.macro, it corresponds to a parsed expression for assign.macro, too.

The left-hand input to the macro is not a parsed expression, but instead a parsed value that encapsulates both a mutator and accessor for the target mutable component. The expansion of the macro can use another assignment operator, or it can use assign_meta.unpack_left to extract functions for the target and assemble them into an expression that is packed with assign_meta.pack_assignment.

assign.macro '$left += $right':

  ~weaker_than: ~other

  let (ref, set, name) = assign_meta.unpack_left(left)

  assign_meta.pack_assignment(

    'block:

       def v = $ref() + (block:

                           let $name = $right

                           $name)

       $set(v)

       v'

  )

> def mutable x = 12

> x += 4

16

> x

16

Provided as meta.

Takes a syntax object that represents the parse left-hand side of an assignment operator, returning three pieces of information about the mutable target:

An assignment macro defined with assign.macro can uses these pieces to construct an expression, and then wrap the expression via assign_meta.pack_assignment to serve as its result.

Provided as meta.

Converts a syntax object, which can be a multi-term syntax object, into an parsed term that represents an expression to implement an assignment operator’s expansion.

See assign.macro for an example.

syntax class

syntax_class assign_meta.AssignParsed(ref, set, name):

  kind: ~group

  fields:

    group

    [tail, ...]

Provided as meta.

A syntax class that matches by parsing an assignment, where the input starts with an assignment operator and continues as the operator expects (typically with a right-hand expression). The syntax-class arguments ref, set, and name must be an expression to produce an accessor procedure of arguments, an expression to produce a mutator procedure of one argument, and an identifier to use as the inferred name (if needed) for the right-hand value.

The value of a binding using assign_meta.AssignParsed is an opaque syntax object that represents the parsed assignment as an expression, while the group field holds a syntax object for the original terms that were parsed. The result also has a tail field that contains the remaining unparsed input.

def mutable reflist = [0]

expr.macro 'refcar $(a :: assign_meta.AssignParsed(

                       'fun () : List.first(reflist)',

                       'fun (v): reflist := [v]',

                       'refelem'

                     )) $()':

  values(a, '$a.tail ...')

> refcar := 11

> reflist

[11]

> refcar := fun (x): x

> reflist

[#<function:fun>]