sourcelibs/Ranges/src/Ranges.xtlm

1⍝│ Ranges: its macro library -- f_or<, a list of stepped ranges, each 2⍝│ bound to a name: "for i:=0..10 by 2". No functions. 3⍝│ Imported with the functions, under one alias: "rg:" u_se< "Ranges". 4⍝│ A macro is a function from the source text written left and right of 5⍝│ its call to the source that replaces the call, run before the program 6⍝│ is compiled (X_eTaL MC10). Names with m: are macros; those under h: are 7⍝│ private to this file. libs/Parsing/docs/guide.md builds this exact 8⍝│ macro step by step, tool by tool, as a worked example of writing a 9⍝│ notation macro with libs/Parsing. 10 11"t:" u̲se< "Strings" 12"pa:" u̲se< "Parsing" 13 14words ← e̲nclose "for" 15 16⍝│ a when c holds, else b: a text already computed, picked by value 17⍝│ (not a guard), so it can be used inside a larger expression. 18ʰp̲ickText ← { c (a, b) → c ? a◆ b } 19 20⍝│ As h:p_ickText, picking one of two (text, text) pairs. 21ʰp̲ickPair ← { c (a, b) → c ? a◆ b } 22 23⍝│ A bound: a chain of +/- atoms (a leading minus negates the first), 24⍝│ each atom a number, a parenthesized group (recurse), len(name) (an 25⍝│ array's length), A[i] (one of its elements) or a bare name; an atom 26⍝│ alone skips the chain. 27ʰb̲ound ← { t → 28 0 = '+ r̲/ 0 + "+-" ᵖᵃt̲op t ? ʰa̲tom t 29 (parts, os, neg) ← ("bad-range right", "+-") ᵖᵃp̲arts t 30 first ← ʰa̲tom d̲isclose f̲irst parts 31 acc ← neg ʰp̲ickText ("(0 - " c̲at first c̲at ")", first) 32 ʰf̲old (acc, os, 1 d̲rop parts) 33} 34ʰf̲old ← { (acc, os, parts) → 35 0 = t̲ally os ? acc 36 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 ")" 37 ʰf̲old (next, 1 d̲rop os, 1 d̲rop parts) 38} 39ʰa̲tom ← { t → 40 ᵖᵃg̲roup? t ? ʰb̲ound ᵖᵃi̲nner t 41 ᵖᵃa̲pplied? t ? ʰf̲n t 42 0 < "[" ᵖᵃa̲t t ? ʰr̲ef t 43 t 44} 45ʰf̲n ← { t → 46 (name, args) ← "bad-range right" ᵖᵃa̲pplied t 47 _ ← (("len" m̲atch name) ∧ 1 = t̲ally args) ᵖᵃr̲equire ("bad-range right", "a bound's function is len(name): " c̲at t) 48 "(t_ally " c̲at (d̲isclose f̲irst args) c̲at ")" 49} 50ʰr̲ef ← { t → 51 (name, ix) ← "bad-range right" ᵖᵃr̲eference t 52 "((1 + " c̲at (d̲isclose f̲irst ix) c̲at ") s_elect " c̲at name c̲at ")" 53} 54 55⍝│ One "for i:=lo..hi [by step]" piece: a canonical reprint (for the 56⍝│ whole-text check) paired with the statement it binds. 57ʰp̲iece ← { t → 58 at ← " by " ᵖᵃa̲t t 59 (core, stepText) ← (0 = at) ʰp̲ickPair ((t, "1"), (ᵗt̲rim (at − 1) t̲ake t, ᵗt̲rim (at + 3) d̲rop t)) 60 _ ← (ᵖᵃn̲umber? stepText) ᵖᵃr̲equire ("bad-range right", "a step is a whole number: " c̲at stepText) 61 (word, ix, lo, hi) ← ("bad-range right", "a loop variable (for)", words) ᵖᵃb̲inding core 62 low ← ʰb̲ound lo 63 high ← ʰb̲ound hi 64 byPart ← (0 = at) ʰp̲ickText ("", "by" c̲at stepText) 65 reprint ← word c̲at ix c̲at ":=" c̲at lo c̲at ".." c̲at hi c̲at byPart 66 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" 67 (reprint, stmt) 68} 69 70⍝━━ The macro 71 72⍝│ for i:=0..10 by 2, for j:=0..len(A)-1: a comma-separated list of 73⍝│ stepped ranges, lowest to highest inclusive, each bound to its own 74⍝│ name (i and j here). The step (by 2) is optional, 1 when it is not 75⍝│ written. A bound is a number, a parenthesized chain of them, an 76⍝│ array's length (len(A)) or one of its elements (A[0]). When the 77⍝│ program is compiled, each binding becomes a plain array built from 78⍝│ r_ange, so nothing about the range is computed again at run time 79⍝│ beyond what the program itself asks for. A malformed range, an 80⍝│ unknown loop word, a non-whole step, or any text left over once 81⍝│ every piece is read, stops the compiler: error[bad-range]. It 82⍝│ stands as a statement of its own. 83⍝│ takes: @ text 84⍝│ uses: parsed 85ᵐf̲or< ← { @ text → 86 pieces ← '{ p → ʰp̲iece d̲isclose p } m̲ap "," ᵖᵃs̲plit ᵗt̲rim text 87 reprint ← "," ᵗj̲oin '{ b → (rp, _) ← d̲isclose b◆ rp } m̲ap pieces 88 _ ← "bad-range right" ᵖᵃw̲hole (text, reprint) 89 "" ᵗj̲oin '{ b → (_, st) ← d̲isclose b◆ st } m̲ap pieces 90}