macro librarylibs/Ranges/src/Ranges.xtlm
Ranges: its macro library -- f_or<, a list of stepped ranges, each bound to a name: "for i:=0..10 by 2". No functions. Imported with the functions, under one alias: "rg:" u_se< "Ranges". A macro is a function from the source text written left and right of its call to the source that replaces the call, run before the program is compiled (X_eTaL MC10). Names with m: are macros; those under h: are private to this file. libs/Parsing/docs/guide.md builds this exact macro step by step, tool by tool, as a worked example of writing a notation macro with libs/Parsing.
ʰp̲ickText : (Truthy a, Any b) => a -> (b, b) -> b
a when c holds, else b: a text already computed, picked by value (not a guard), so it can be used inside a larger expression.
ʰp̲ickText ← { c (a, b) → c ? a◆ b }
ʰp̲ickPair : (Truthy a, Any b) => a -> (b, b) -> b
As h:p_ickText, picking one of two (text, text) pairs.
ʰp̲ickPair ← { c (a, b) → c ? a◆ b }
ʰb̲ound : Char -> Char
A bound: a chain of +/- atoms (a leading minus negates the first), each atom a number, a parenthesized group (recurse), len(name) (an array's length), A[i] (one of its elements) or a bare name; an atom alone skips the chain.
ʰb̲ound ← { t → 0 = '+ r̲/ 0 + "+-" ᵖᵃt̲op t ? ʰa̲tom t (parts, os, neg) ← ("bad-range right", "+-") ᵖᵃp̲arts t first ← ʰa̲tom d̲isclose f̲irst parts acc ← neg ʰp̲ickText ("(0 - " c̲at first c̲at ")", first) ʰf̲old (acc, os, 1 d̲rop parts) }
ʰf̲old : (Char, Char, Box Char) -> Char
ʰf̲old ← { (acc, os, parts) → 0 = t̲ally os ? acc next ← "(" c̲at acc c̲at " " c̲at (1 t̲ake os) c̲at " " c̲at (ʰa̲tom d̲isclose f̲irst parts) c̲at ")" ʰf̲old (next, 1 d̲rop os, 1 d̲rop parts) }
ʰa̲tom : Char -> Char
ʰa̲tom ← { t → ᵖᵃg̲roup? t ? ʰb̲ound ᵖᵃi̲nner t ᵖᵃa̲pplied? t ? ʰf̲n t 0 < "[" ᵖᵃa̲t t ? ʰr̲ef t t }
ʰf̲n : Char -> Char
ʰf̲n ← { t → (name, args) ← "bad-range right" ᵖᵃa̲pplied t _ ← (("len" m̲atch name) ∧ 1 = t̲ally args) ᵖᵃr̲equire ("bad-range right", "a bound's function is len(name): " c̲at t) "(t_ally " c̲at (d̲isclose f̲irst args) c̲at ")" }
ʰr̲ef : Char -> Char
ʰr̲ef ← { t → (name, ix) ← "bad-range right" ᵖᵃr̲eference t "((1 + " c̲at (d̲isclose f̲irst ix) c̲at ") s_elect " c̲at name c̲at ")" }
ʰp̲iece : Char -> (Char, Char)
One "for i:=lo..hi [by step]" piece: a canonical reprint (for the whole-text check) paired with the statement it binds.
ʰp̲iece ← { t → at ← " by " ᵖᵃa̲t t (core, stepText) ← (0 = at) ʰp̲ickPair ((t, "1"), (ᵗt̲rim (at − 1) t̲ake t, ᵗt̲rim (at + 3) d̲rop t)) _ ← (ᵖᵃn̲umber? stepText) ᵖᵃr̲equire ("bad-range right", "a step is a whole number: " c̲at stepText) (word, ix, lo, hi) ← ("bad-range right", "a loop variable (for)", words) ᵖᵃb̲inding core low ← ʰb̲ound lo high ← ʰb̲ound hi byPart ← (0 = at) ʰp̲ickText ("", "by" c̲at stepText) reprint ← word c̲at ix c̲at ":=" c̲at lo c̲at ".." c̲at hi c̲at byPart stmt ← ix c̲at " := " c̲at low c̲at " + (" c̲at stepText c̲at " * ((r_ange (1 + f_loor ((" c̲at high c̲at " - " c̲at low c̲at ") / " c̲at stepText c̲at ")))" c̲at " - 1))\n" (reprint, stmt) }
The macro
ᵐf̲or< : Unit -> Char -> Char
for i:=0..10 by 2, for j:=0..len(A)-1: a comma-separated list of stepped ranges, lowest to highest inclusive, each bound to its own name (i and j here). The step (by 2) is optional, 1 when it is not written. A bound is a number, a parenthesized chain of them, an array's length (len(A)) or one of its elements (A[0]). When the program is compiled, each binding becomes a plain array built from r_ange, so nothing about the range is computed again at run time beyond what the program itself asks for. A malformed range, an unknown loop word, a non-whole step, or any text left over once every piece is read, stops the compiler: error[bad-range]. It stands as a statement of its own.
ᵐf̲or< ← { @ text → pieces ← '{ p → ʰp̲iece d̲isclose p } m̲ap "," ᵖᵃs̲plit ᵗt̲rim text reprint ← "," ᵗj̲oin '{ b → (rp, _) ← d̲isclose b◆ rp } m̲ap pieces _ ← "bad-range right" ᵖᵃw̲hole (text, reprint) "" ᵗj̲oin '{ b → (_, st) ← d̲isclose b◆ st } m̲ap pieces }