macro librarylibs/Tags/src/Tags.xtlm
Tags: macros only -- e_num< and p_arts<, names for choices and for the parts of a tuple, written when the program is compiled. Imported under one alias: "tg:" u_se< "Tags". 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.
Reading the declaration
ʰk̲ind : Char -> Char
The kind's name, before the colon of "Kind: a b c".
ʰk̲ind ← { t → ᵗt̲rim ((f̲irst w̲here t = f̲irst ":") − 1) t̲ake t }
ʰn̲ames : Char -> Box Char
The names after the colon, one per word.
ʰn̲ames ← { t → ᵗw̲ords (f̲irst w̲here t = f̲irst ":") d̲rop t }
ʰw̲ritten : Char -> Char
The declaration as it was read, "Kind:name name ...", with no blanks: the text it came from, or a rule dropped something.
ʰw̲ritten ← { t → (ʰk̲ind t) c̲at ":" c̲at "" ᵗj̲oin ʰn̲ames t }
ʰk̲ind? : Truthy a => Char -> a
Whether a kind is a capital letter, then letters or digits.
ʰk̲ind? ← { k → (0 < t̲ally k) ∧ ((f̲irst k) m̲ember? ⎕A) ∧ '∧ r̲/ k m̲ember? letters }
ʰn̲ame? : Truthy a => Char -> a
Whether a name is a lowercase letter, then lowercase letters or digits.
ʰn̲ame? ← { n → (0 < t̲ally n) ∧ ((f̲irst n) m̲ember? lowers) ∧ '∧ r̲/ n m̲ember? lowers c̲at ⎕D }
ʰp̲roblem : Char -> Char
The declaration's error, or "" when it is well formed: one colon, a kind, at least one name, every name well formed, none repeated.
ʰp̲roblem ← { t → 1 ≠ t̲ally w̲here t = f̲irst ":" ? "write Kind: name name ..." n̲ot ʰk̲ind? ʰk̲ind t ? (ʰk̲ind t) c̲at " is not a kind: a capital letter, then letters or digits" ns ← ʰn̲ames t 0 = t̲ally ns ? "no names after " c̲at (ʰk̲ind t) c̲at ":" bad ← (n̲ot '{ ʰn̲ame? d̲isclose ⍵ } e̲ach ns) r̲eplicate ns 0 < t̲ally bad ? (d̲isclose f̲irst bad) c̲at " is not a name: a lowercase letter, then lowercase letters or digits" (t̲ally ns) ≠ t̲ally u̲nique ns ? "a name is repeated" "" }
Spelling the names written
ʰc̲onstant : Char -> Char
A constant: the name in capitals (red -> RED, State -> STATE).
ʰc̲onstant ← { n → ᵗu̲pper n }
ʰf̲unction : Char -> Char -> Char
A function of the kind, the kind's first letter underlined: State "" -> u:s_tate; State "W" -> u:s_tateW.
ʰf̲unction ← { k rest → "u:" c̲at (ᵗl̲ower 1 t̲ake k) c̲at "_" c̲at (1 d̲rop k) c̲at rest }
ʰc̲apital : Char -> Char
A name with its first letter a capital (count -> Count).
ʰc̲apital ← { n → (ᵗu̲pper 1 t̲ake n) c̲at 1 d̲rop n }
ʰp̲attern : Char -> Box Char -> Char
The tagged tuple as a pattern, the tag named tg: "(g1:tag, w, m)".
ʰp̲attern ← { tg ns → "(" c̲at tg c̲at ", " c̲at (", " ᵗj̲oin ns) c̲at ")" }
ʰo̲nly : (Char, Char) -> (Int, Int) -> Char
n parts, all ignored but part i, named pt: "(g1:tag, _, g2:part, _)".
ʰo̲nly ← { (tg, pt) (i, n) → tg ʰp̲attern '{ j → j = i ? pt◆ "_" } m̲ap r̲ange n }
ʰc̲heck : Char -> Char -> Char
The check a getter or setter makes of its argument's tag, named tg.
ʰ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 "\"" }
The macros
ᵐe̲num< : Unit -> Char -> Char
@ tg:e_num< "Color: black red green": a constant per choice, its code the choice's position (BLACK := 1, RED := 2, GREEN := 3), and the kind in capitals naming the choices' names (COLOR := "black" "red" "green"), so RED s_elect COLOR is the name and a code selects from any table in the same order. A malformed declaration stops the compiler: error[bad-enum]. Nothing goes on the left (@).
ᵐe̲num< ← { @ t → p ← ʰp̲roblem t 0 < t̲ally p ? "bad-enum right" ⎕R̲EJECT p _ ← "bad-enum right" ᵖᵃw̲hole (t, ʰw̲ritten t) ns ← ʰn̲ames t codes ← '{ i → (ʰc̲onstant d̲isclose i s̲elect ns) c̲at " := " c̲at f̲ormat i } m̲ap r̲ange t̲ally ns names ← (ʰc̲onstant ʰk̲ind t) c̲at " := " c̲at " " ᵗj̲oin '{ n → "\"" c̲at (d̲isclose n) c̲at "\"" } m̲ap ns "\n" ᵗj̲oin codes c̲at e̲nclose names }
ʰg̲etter : (Char, Char, Char) -> (Int, Box Char) -> Char
Part i's getter: u:s_tateW := { (g1:tag, g2:part, _, _) -> check; g2:part }.
ʰg̲etter ← { (k, tg, pt) (i, ns) → name ← (k ʰf̲unction ʰc̲apital d̲isclose i s̲elect ns) c̲at " := { " 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}" }
ʰs̲etter : (Char, Char, Char) -> (Int, Box Char) -> Char
Part i's setter: u:s_etStateW := { g3:new (g1:tag, w, k) -> check; g3:new of the type of w; (g1:tag, g3:new, k) }.
ʰs̲etter ← { (k, tg, nw) (i, ns) → old ← d̲isclose i s̲elect ns parts ← '{ j → j = i ? nw◆ d̲isclose j s̲elect ns } m̲ap r̲ange t̲ally ns 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 " 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}" }
ᵐp̲arts< : Unit -> Char -> Char
@ tg:p_arts< "State: w m v k": a tuple whose first part is a tag naming its kind, and functions for it, so its parts are used by name: STATE := "State", the tag; u:s_tate (w, m, v, k), the tagged tuple (STATE, w, m, v, k); u:s_tate? s, whether s is tagged State; per part a getter, u:s_tateW s, and a setter, x u:s_etStateW s, giving s with that part x (of the part's type); and i u:s_tateRows s, the items i of every part, when the parts are the columns of a table. A getter or setter given another kind stops with error[wrong-kind] when it runs. A malformed declaration stops the compiler: error[bad-parts]. Nothing goes on the left (@).
ᵐp̲arts< ← { @ t → p ← ʰp̲roblem t 0 < t̲ally p ? "bad-parts right" ⎕R̲EJECT p _ ← "bad-parts right" ᵖᵃw̲hole (t, ʰw̲ritten t) k ← ʰk̲ind t ns ← ʰn̲ames t n ← t̲ally ns c ← ʰc̲onstant k tg ← ⎕G̲ENSYM "tag" pt ← ⎕G̲ENSYM "part" nw ← ⎕G̲ENSYM "new" wh ← ⎕G̲ENSYM "which" list ← ", " ᵗj̲oin ns arg ← ({ one → one ? list◆ "(" c̲at list c̲at ")" }) 1 = n ⍝ one part: no tuple tag ← c c̲at " := \"" c̲at k c̲at "\"" make ← (k ʰf̲unction "") c̲at " := { " c̲at arg c̲at " -> (" c̲at c c̲at ", " c̲at list c̲at ") }" 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 " }" gets ← '{ i → (k, tg, pt) ʰg̲etter (i, ns) } m̲ap r̲ange n sets ← '{ i → (k, tg, nw) ʰs̲etter (i, ns) } m̲ap r̲ange n cols ← ", " ᵗj̲oin '{ m → wh c̲at " s_elect " c̲at d̲isclose m } m̲ap ns 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 ") }" "\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 }