3.5 Assignment Macros
definition | |
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)
'block:
$name)
$set(v)
v'
)
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 expression for a function of zero arguments that acts an accessor;
an expression for a function of one argument that acts a mutator; and
an identifier that reflects the target’s name, which is useful for inferring a name for certain kinds of values (such as functions) for the right-hand side.
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.
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 | |||||
|
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 function of zero arguments, an expression to produce a mutator function 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.
expr.macro 'refcar $(a :: assign_meta.AssignParsed(
'fun () : List.first(reflist)',
'fun (v): reflist := [v]',
'refelem'
)) $()':