sourcelib/Macros.xtlm
1⍝│ A small macro library, an example of macros of your own (MC10). A
2⍝│ macro is an ordinary function of the source text written on each
3⍝│ side of its call; the text it gives replaces the call, then is read
4⍝│ as code. Import it with an alias of your own, "x:" u_se< "Macros",
5⍝│ and see what a program becomes with xetal expand FILE. The contract
6⍝│ lines above a macro (MC31) say the shape of call it takes (checked
7⍝│ before it runs), whether it copies its text (every name and number
8⍝│ must reach the expansion) or parses it, and the names it binds for
9⍝│ your text.
10
11⍝━━ Statements and definitions
12
13⍝│ Run the statements on the right when the condition on the left
14⍝│ holds; the value is @ either way (the other way round from the
15⍝│ system macro u_nless<).
16⍝│ takes: code code
17⍝│ >> "x:" u_se< "Macros"
18⍝│ >> c"1 < 2" x:w_hen< c"p_rint! 7"
19⍝│ 7
20⍝│ @
21ᵐw̲hen< ← { cond body → "{ @ -> (n_ot " c̲at cond c̲at ") ? @; " c̲at body c̲at "; @ } @" }
22
23⍝│ Define the function named on the left (in u:) as the lambda body on
24⍝│ the right, its name written as text.
25⍝│ takes: text code
26⍝│ >> "x:" u_se< "Macros"
27⍝│ >> "s_quare" x:d_ef< c"_r * _r"
28⍝│ >> u:s_quare 7
29⍝│ 49
30ᵐd̲ef< ← { name body → "u:" c̲at name c̲at " := { " c̲at body c̲at " }" }
31
32⍝━━ Checks
33
34⍝│ The line "ok: label" when the condition on the left holds, else
35⍝│ "FAIL: label (cond)", the condition shown as it was written, which a
36⍝│ function, given only the condition's value, cannot do.
37⍝│ takes: code text
38⍝│ >> "x:" u_se< "Macros"
39⍝│ >> c"5 = 2 * 2" x:t_est< "doubling"
40⍝│ FAIL: doubling (5 = 2 * 2)
41ᵐ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 ")\" } @" }
42
43⍝━━ Names a macro binds
44
45⍝│ The expression on the right with x as 3: the macro binds x around
46⍝│ your expression and writes it as it is, so your x is its x. A name a
47⍝│ macro writes stays as written; that is how an anaphor works, and a
48⍝│ macro that wants a name of its own asks for one (t_wice< below).
49⍝│ binds: x
50⍝│ takes: @ code
51⍝│ >> "x:" u_se< "Macros"
52⍝│ >> x := 10
53⍝│ >> @ x:b_inds< c"x + 1"
54⍝│ 4
55ᵐb̲inds< ← { @ e → "{ @ -> x := 3; " c̲at e c̲at " } @" }
56
57⍝│ Twice the expression on the right: the macro binds a local of its
58⍝│ own around your expression, a fresh name from []G_ENSYM (g1:t, say),
59⍝│ so a t of your own is still yours.
60⍝│ takes: @ code
61⍝│ >> "x:" u_se< "Macros"
62⍝│ >> t := 5
63⍝│ >> @ x:t_wice< c"t + 1"
64⍝│ 12
65ᵐt̲wice< ← { @ e →
66 t ← ⎕G̲ENSYM "t"
67 "{ @ -> " c̲at t c̲at " := 2; " c̲at t c̲at " * (" c̲at e c̲at ") } @"
68}
69
70⍝│ The text on the right with it bound to the value of the expression
71⍝│ on the left (an anaphoric macro: it is meant for your text, declared
72⍝│ below, so your text cannot bind an it of its own).
73⍝│ binds: it
74⍝│ takes: code code
75⍝│ >> "x:" u_se< "Macros"
76⍝│ >> c"3 + 4" x:w_ith< c"it * it"
77⍝│ 49
78ᵐw̲ith< ← { e body → "{ @ -> it := (" c̲at e c̲at "); " c̲at body c̲at " } @" }