macro librarylib/Macros.xtlm

A small macro library, an example of macros of your own (MC10). A macro is an ordinary function of the source text written on each side of its call; the text it gives replaces the call, then is read as code. Import it with an alias of your own, "x:" u_se< "Macros", and see what a program becomes with xetal expand FILE. The contract lines above a macro (MC31) say the shape of call it takes (checked before it runs), whether it copies its text (every name and number must reach the expansion) or parses it, and the names it binds for your text.

source

Statements and definitions

ᵐw̲hen< : Char -> Char -> Char

macro · line 21

Run the statements on the right when the condition on the left holds; the value is @ either way (the other way round from the system macro u_nless<).

      "x:" u̲se< "Macros"
      c"1 < 2" ˣw̲hen< c"p̲rint! 7"
7
@
ᵐw̲hen< ← { cond body → "{ @ -> (n_ot " c̲at cond c̲at ") ? @; " c̲at body c̲at "; @ } @" }

ᵐd̲ef< : Char -> Char -> Char

macro · line 30

Define the function named on the left (in u:) as the lambda body on the right, its name written as text.

      "x:" u̲se< "Macros"
      "s_quare" ˣd̲ef< c"⍵ × ⍵"
      ᵘs̲quare 7
49
ᵐd̲ef< ← { name body → "u:" c̲at name c̲at " := { " c̲at body c̲at " }" }
Used in: ᵘs̲quare

Checks

ᵐt̲est< : Char -> Char -> Char

macro · line 41

The line "ok: label" when the condition on the left holds, else "FAIL: label (cond)", the condition shown as it was written, which a function, given only the condition's value, cannot do.

      "x:" u̲se< "Macros"
      c"5 = 2 × 2" ˣt̲est< "doubling"
FAIL: doubling (5 = 2 * 2)
ᵐt̲est< ← { cond label → "{ @ -> (" c̲at cond c̲at ") ? \"ok: " c̲at label c̲at "\"; \"FAIL: " c̲at label c̲at " (" c̲at cond c̲at ")\" } @" }

Names a macro binds

ᵐb̲inds< : Unit -> Char -> Char

macro · line 55 · binds x

The expression on the right with x as 3: the macro binds x around your expression and writes it as it is, so your x is its x. A name a macro writes stays as written; that is how an anaphor works, and a macro that wants a name of its own asks for one (t_wice< below).

      "x:" u̲se< "Macros"
      x ← 10
      @ ˣb̲inds< c"x + 1"
4
ᵐb̲inds< ← { @ e → "{ @ -> x := 3; " c̲at e c̲at " } @" }

ᵐt̲wice< : Unit -> Char -> Char

macro · line 65

Twice the expression on the right: the macro binds a local of its own around your expression, a fresh name from []G_ENSYM (g1:t, say), so a t of your own is still yours.

      "x:" u̲se< "Macros"
      t ← 5
      @ ˣt̲wice< c"t + 1"
12
ᵐt̲wice< ← { @ e →
  t ← ⎕G̲ENSYM "t"
  "{ @ -> " c̲at t c̲at " := 2; " c̲at t c̲at " * (" c̲at e c̲at ") } @"
}

ᵐw̲ith< : Char -> Char -> Char

macro · line 78 · binds it

The text on the right with it bound to the value of the expression on the left (an anaphoric macro: it is meant for your text, declared below, so your text cannot bind an it of its own).

      "x:" u̲se< "Macros"
      c"3 + 4" ˣw̲ith< c"it × it"
49
ᵐw̲ith< ← { e body → "{ @ -> it := (" c̲at e c̲at "); " c̲at body c̲at " } @" }