sourcelibs/Tags/src/Tags.xtlm

1⍝│ Tags: macros only -- e_num< and p_arts<, names for choices and for 2⍝│ the parts of a tuple, written when the program is compiled. 3⍝│ Imported under one alias: "tg:" u_se< "Tags". 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. 8 9"t:" u̲se< "Strings" 10"pa:" u̲se< "Parsing" 11 12⍝│ The letters and digits a declaration may use. 13lowers ← "abcdefghijklmnopqrstuvwxyz" 14letters ← lowers c̲at ⎕A c̲at ⎕D 15 16⍝━━ Reading the declaration 17 18⍝│ The kind's name, before the colon of "Kind: a b c". 19ʰk̲ind ← { t → ᵗt̲rim ((f̲irst w̲here t = f̲irst ":") − 1) t̲ake t } 20 21⍝│ The names after the colon, one per word. 22ʰn̲ames ← { t → ᵗw̲ords (f̲irst w̲here t = f̲irst ":") d̲rop t } 23 24⍝│ The declaration as it was read, "Kind:name name ...", with no 25⍝│ blanks: the text it came from, or a rule dropped something. 26ʰw̲ritten ← { t → (ʰk̲ind t) c̲at ":" c̲at "" ᵗj̲oin ʰn̲ames t } 27 28⍝│ Whether a kind is a capital letter, then letters or digits. 29ʰk̲ind? ← { k → (0 < t̲ally k) ∧ ((f̲irst k) m̲ember? ⎕A) ∧ '∧ r̲/ k m̲ember? letters } 30 31⍝│ Whether a name is a lowercase letter, then lowercase letters or digits. 32ʰn̲ame? ← { n → (0 < t̲ally n) ∧ ((f̲irst n) m̲ember? lowers) ∧ '∧ r̲/ n m̲ember? lowers c̲at ⎕D } 33 34⍝│ The declaration's error, or "" when it is well formed: one colon, 35⍝│ a kind, at least one name, every name well formed, none repeated. 36ʰp̲roblem ← { t → 37 1 ≠ t̲ally w̲here t = f̲irst ":" ? "write Kind: name name ..." 38 n̲ot ʰk̲ind? ʰk̲ind t ? (ʰk̲ind t) c̲at " is not a kind: a capital letter, then letters or digits" 39 ns ← ʰn̲ames t 40 0 = t̲ally ns ? "no names after " c̲at (ʰk̲ind t) c̲at ":" 41 bad ← (n̲ot '{ ʰn̲ame? d̲isclose ⍵ } e̲ach ns) r̲eplicate ns 42 0 < t̲ally bad ? (d̲isclose f̲irst bad) c̲at " is not a name: a lowercase letter, then lowercase letters or digits" 43 (t̲ally ns) ≠ t̲ally u̲nique ns ? "a name is repeated" 44 "" 45} 46 47⍝━━ Spelling the names written 48 49⍝│ A constant: the name in capitals (red -> RED, State -> STATE). 50ʰc̲onstant ← { n → ᵗu̲pper n } 51 52⍝│ A function of the kind, the kind's first letter underlined: 53⍝│ State "" -> u:s_tate; State "W" -> u:s_tateW. 54ʰf̲unction ← { k rest → "u:" c̲at (ᵗl̲ower 1 t̲ake k) c̲at "_" c̲at (1 d̲rop k) c̲at rest } 55 56⍝│ A name with its first letter a capital (count -> Count). 57ʰc̲apital ← { n → (ᵗu̲pper 1 t̲ake n) c̲at 1 d̲rop n } 58 59⍝│ The functions written use the parts' own names, and fresh names of 60⍝│ their own (from []G_ENSYM: g1:tag, g2:part, ...) for the tag, the 61⍝│ part a getter gives, a setter's new value and the rows taken, so a 62⍝│ part may be named anything, tag included. 63 64⍝│ The tagged tuple as a pattern, the tag named tg: "(g1:tag, w, m)". 65ʰp̲attern ← { tg ns → "(" c̲at tg c̲at ", " c̲at (", " ᵗj̲oin ns) c̲at ")" } 66 67⍝│ n parts, all ignored but part i, named pt: "(g1:tag, _, g2:part, _)". 68ʰo̲nly ← { (tg, pt) (i, n) → tg ʰp̲attern '{ j → j = i ? pt◆ "_" } m̲ap r̲ange n } 69 70⍝│ The check a getter or setter makes of its argument's tag, named tg. 71ʰc̲heck ← { tg k → "n_ot " c̲at tg c̲at " m_atch " c̲at (ʰc̲onstant k) c̲at " ? \"wrong-kind\" []S_IGNAL \"expected a " c̲at k c̲at "\"" } 72 73⍝━━ The macros 74 75⍝│ @ tg:e_num< "Color: black red green": a constant per choice, its code 76⍝│ the choice's position (BLACK := 1, RED := 2, GREEN := 3), and the 77⍝│ kind in capitals naming the choices' names (COLOR := "black" "red" 78⍝│ "green"), so RED s_elect COLOR is the name and a code selects from 79⍝│ any table in the same order. A malformed declaration stops the 80⍝│ compiler: error[bad-enum]. Nothing goes on the left (@). 81⍝│ takes: @ text 82⍝│ uses: parsed 83ᵐe̲num< ← { @ t → 84 p ← ʰp̲roblem t 85 0 < t̲ally p ? "bad-enum right" ⎕R̲EJECT p 86 _ ← "bad-enum right" ᵖᵃw̲hole (t, ʰw̲ritten t) 87 ns ← ʰn̲ames t 88 codes ← '{ i → (ʰc̲onstant d̲isclose i s̲elect ns) c̲at " := " c̲at f̲ormat i } m̲ap r̲ange t̲ally ns 89 names ← (ʰc̲onstant ʰk̲ind t) c̲at " := " c̲at " " ᵗj̲oin '{ n → "\"" c̲at (d̲isclose n) c̲at "\"" } m̲ap ns 90 "\n" ᵗj̲oin codes c̲at e̲nclose names 91} 92 93⍝│ Part i's getter: u:s_tateW := { (g1:tag, g2:part, _, _) -> check; g2:part }. 94ʰg̲etter ← { (k, tg, pt) (i, ns) → 95 name ← (k ʰf̲unction ʰc̲apital d̲isclose i s̲elect ns) c̲at " := { " 96 name c̲at ((tg, pt) ʰo̲nly (i, t̲ally ns)) c̲at " ->\n " c̲at (tg ʰc̲heck k) c̲at "\n " c̲at pt c̲at "\n}" 97} 98 99⍝│ Part i's setter: u:s_etStateW := { g3:new (g1:tag, w, k) -> check; 100⍝│ g3:new of the type of w; (g1:tag, g3:new, k) }. 101ʰs̲etter ← { (k, tg, nw) (i, ns) → 102 old ← d̲isclose i s̲elect ns 103 parts ← '{ j → j = i ? nw◆ d̲isclose j s̲elect ns } m̲ap r̲ange t̲ally ns 104 name ← "u:s_et" c̲at k c̲at (ʰc̲apital old) c̲at " := { " c̲at nw c̲at " " c̲at (tg ʰp̲attern ns) c̲at " ->\n " 105 name c̲at (tg ʰc̲heck k) c̲at "\n _ := " c̲at nw c̲at " m_atch " c̲at old c̲at "\n " c̲at (tg ʰp̲attern parts) c̲at "\n}" 106} 107 108⍝│ @ tg:p_arts< "State: w m v k": a tuple whose first part is a tag 109⍝│ naming its kind, and functions for it, so its parts are used by name: 110⍝│ STATE := "State", the tag; u:s_tate (w, m, v, k), the tagged tuple 111⍝│ (STATE, w, m, v, k); u:s_tate? s, whether s is tagged State; per part 112⍝│ a getter, u:s_tateW s, and a setter, x u:s_etStateW s, giving s with 113⍝│ that part x (of the part's type); and i u:s_tateRows s, the items i of 114⍝│ every part, when the parts are the columns of a table. A getter or 115⍝│ setter given another kind stops with error[wrong-kind] when it runs. 116⍝│ A malformed declaration stops the compiler: error[bad-parts]. 117⍝│ Nothing goes on the left (@). 118⍝│ takes: @ text 119⍝│ uses: parsed 120ᵐp̲arts< ← { @ t → 121 p ← ʰp̲roblem t 122 0 < t̲ally p ? "bad-parts right" ⎕R̲EJECT p 123 _ ← "bad-parts right" ᵖᵃw̲hole (t, ʰw̲ritten t) 124 k ← ʰk̲ind t 125 ns ← ʰn̲ames t 126 n ← t̲ally ns 127 c ← ʰc̲onstant k 128 tg ← ⎕G̲ENSYM "tag" 129 pt ← ⎕G̲ENSYM "part" 130 nw ← ⎕G̲ENSYM "new" 131 wh ← ⎕G̲ENSYM "which" 132 list ← ", " ᵗj̲oin ns 133 arg ← ({ one → one ? list◆ "(" c̲at list c̲at ")" }) 1 = n ⍝ one part: no tuple 134 tag ← c c̲at " := \"" c̲at k c̲at "\"" 135 make ← (k ʰf̲unction "") c̲at " := { " c̲at arg c̲at " -> (" c̲at c c̲at ", " c̲at list c̲at ") }" 136 is ← (k ʰf̲unction "?") c̲at " := { " c̲at (tg ʰp̲attern n r̲eshape e̲nclose "_") c̲at " -> " c̲at tg c̲at " m_atch " c̲at c c̲at " }" 137 gets ← '{ i → (k, tg, pt) ʰg̲etter (i, ns) } m̲ap r̲ange n 138 sets ← '{ i → (k, tg, nw) ʰs̲etter (i, ns) } m̲ap r̲ange n 139 cols ← ", " ᵗj̲oin '{ m → wh c̲at " s_elect " c̲at d̲isclose m } m̲ap ns 140 rows ← (k ʰf̲unction "Rows") c̲at " := { " c̲at wh c̲at " " c̲at (tg ʰp̲attern ns) c̲at " -> (" c̲at tg c̲at ", " c̲at cols c̲at ") }" 141 "\n" ᵗj̲oin (e̲nclose tag) c̲at (e̲nclose make) c̲at (e̲nclose is) c̲at gets c̲at sets c̲at e̲nclose rows 142}