Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

lib/macro-value.x

Macros as values that build and recognize code.

Primary API

FunctionSummary
Macro_applyApplies a macro value to code values.
Macro_case_capture_atRecognizes code built by t for the case whose site is site, which may be NULL, and publishes the captures under names.
Macro_case_patternThe pattern a macro-valued case compiles to; the compiler lowers a call of this to Macro_case_capture_at over the match subject.
Macro_closeRecords the Macro values an anonymous macro captured where it was created, so applying it later applies the same children.
Macro_patternDerives the Match pattern that recognizes code this macro builds, capturing each parameter under the given binder.
Macro.subjectReturns the table Macro.use_subject last set, or void.
Macro.use_subjectSets the (SPELLING BINDING) rows for global references and the (source-spelling BINDING SPELLING) rows for renamed local bindings in a compile-time call’s syntax arguments.

Functions

Macro_apply

List Macro_apply(Macro t, List values)

Applies a macro value to code values. The result is a pending invocation; inserting it into a program expands and binds it there.

Source: lib/macro-value.x:70

Macro_case_capture_at

int Macro_case_capture_at( MacroCaseSite *site, List code, Macro t, List names, MatchCaptureBuffer *published)

Recognizes code built by t for the case whose site is site, which may be NULL, and publishes the captures under names. A pattern that does not depend on the current call’s subject is prepared once and kept in the site; generated match code calls this for a macro-valued case.

Source: lib/macro-value.x:339

Macro_case_pattern

List Macro_case_pattern(Macro t, List names)

The pattern a macro-valued case compiles to; the compiler lowers a call of this to Macro_case_capture_at over the match subject.

Source: lib/macro-value.x:332

Macro_close

Macro Macro_close(Macro value, List captures)

Records the Macro values an anonymous macro captured where it was created, so applying it later applies the same children.

Source: lib/macro-value.x:65

Macro_pattern

List Macro_pattern(Macro t, List names)

Derives the Match pattern that recognizes code this macro builds, capturing each parameter under the given binder.

Source: lib/macro-value.x:131

Macro

Macro.subject

Var Macro.subject(void)

Returns the table Macro.use_subject last set, or void.

Source: lib/macro-value.x:101

Macro.use_subject

void Macro.use_subject(Var rows)

Sets the (SPELLING BINDING) rows for global references and the (source-spelling BINDING SPELLING) rows for renamed local bindings in a compile-time call’s syntax arguments. A macro value’s free reference recognizes only the recorded global binding; with void it recognizes any binding of its spelling. The compiler sets these rows for each meta call and carries them through the helper.

Source: lib/macro-value.x:109

Runtime-internal callables

These callables connect runtime translation units. They are documented for source readers but are not supported as user API.

FunctionSummary
Macro.binderReturns the binder that captures one projection of the template hole whose binder is binder.

Macro

Macro.binder

Atom Macro.binder(Var binder, String projection, int sequence)

Returns the binder that captures one projection of the template hole whose binder is binder. Splice and construction projections are always sequences; return, declarator, and member projections never are; source, value, and expression follow the hole’s sequence.

Source: lib/macro-value.x:177

Public types

TypeKindSummary
MacroaliasA macro as a value: called to build code, or used in a Match case to recognize code and capture its parameters.
MacroCaseSitestructHolds one macro-valued case site’s prepared recognition for the process: the plan Match keeps, the slots of the macro’s fixed locals, and where each binder reads its capture.
MacroFixedSlotsstructRecords fixed-local slots for distinct-identity checks and Name slots for member-spelling comparisons during recognition.
MacroPublishingstructRecords where each of a case‘s binders reads its capture: the slot of its internal binder in the pattern that captured, and its own slot in the case, which need not share the parameters’ order.

Macro

typedef List Macro

A macro as a value: called to build code, or used in a Match case to recognize code and capture its parameters.

Source: lib/macro-value.x:20

MacroCaseSite

typedef struct MacroCaseSite { MatchCaptureSite match; MacroFixedSlots policy; MacroPublishing route; int ready; } MacroCaseSite

Holds one macro-valued case site’s prepared recognition for the process: the plan Match keeps, the slots of the macro’s fixed locals, and where each binder reads its capture. The compiler emits one zero-initialized static site per case.

Source: lib/macro-value.x:45

MacroFixedSlots

typedef struct MacroFixedSlots { int count, slots[MACHINE_BINDER_MAX]; int names, name_slots[MACHINE_BINDER_MAX]; } MacroFixedSlots

Records fixed-local slots for distinct-identity checks and Name slots for member-spelling comparisons during recognition.

Source: lib/macro-value.x:24

MacroPublishing

typedef struct MacroPublishing { int from[MACHINE_BINDER_MAX], fallback[MACHINE_BINDER_MAX]; int to[MACHINE_BINDER_MAX]; int count, binders, complete; unsigned long definite; } MacroPublishing

Records where each of a case‘s binders reads its capture: the slot of its internal binder in the pattern that captured, and its own slot in the case, which need not share the parameters’ order.

Source: lib/macro-value.x:33

Design notes

A Macro is the canonical macrodef record of a definition. $name selects one and macro Kind(...) => ... creates one. Applying it returns a pending invocation the compiler expands and binds at the insertion site; naming it in a case derives a Match pattern from the same body. Generated code calls these operations by name, so the module is part of the prelude.

Tests and examples

make verify (macro-values, macro-sequence-case, and macro-slot-recognition compiler fixtures).