macro librarylib/Ecs.xtlm
Ecs: its macro library -- c_omponents<, a world's layout written from its list of components. Imported with the functions, under one alias: "ec:" u_se< "Ecs". A macro is a function from the source text on each side of its call to the source that replaces the call, run before the program is compiled (X_eTaL MC10); this one creates names, which a function cannot. Names with m: are macros; those under h: are private to this file.
Reading the list
ʰw̲ords : Char -> Box Char
The words of a text.
ʰw̲ords ← { t → (n̲ot t m̲ember? " \n\t") p̲artition t }
ʰn̲ame : Char -> Char
A component's name: a word up to its colon.
ʰn̲ame ← { w → (n̲ot '∨ s̲\ w = f̲irst ":") r̲eplicate w }
ʰw̲idth : Char -> Int
A component's width: the number after its colon, else 1.
ʰw̲idth ← { w → n ← n̲umbers ('∨ s̲\ 0 c̲at -1 d̲rop w = f̲irst ":") r̲eplicate w 0 = t̲ally n ? 1 f̲loor f̲irst n }
ʰc̲omponent? : Truthy a => Char -> a
Whether a word is a component: a lowercase name of letters, an optional :width.
ʰc̲omponent? ← { w → n ← ʰn̲ame w (0 < t̲ally n) ∧ '∧ r̲/ n m̲ember? lowers }
ʰg̲etter : Char -> Char
The function names for a component: p_os, h_asPos, s_etPos, d_ropPos for pos.
ʰg̲etter ← { n → (1 t̲ake n) c̲at "_" c̲at 1 d̲rop n }
ʰt̲rim : Char -> Char
A text without spaces at its ends.
ʰt̲rim ← { t → (n̲ot (r̲ev '∧ s̲\ r̲ev t = f̲irst " ") ∨ '∧ s̲\ t = f̲irst " ") r̲eplicate t }
ʰt̲itle : Char -> Char
ʰt̲itle ← { n → ((lowers i̲ndexOf 1 t̲ake n) s̲elect ⎕A) c̲at 1 d̲rop n }
The macro
ᵐc̲omponents< : Char -> Char -> Char
"u" ec:c_omponents< "pos:2 vel:2 hp": a world's layout from its components, each a name and a width (1 when left out). Column 1 says whether a slot is alive; each component takes a column saying whether an entity has it, then its fields. When the program is compiled it writes, for each component, a getter (u:p_os W: the column, or the columns when wider than 1), a mask (u:h_asPos W), a setter (W u:s_etPos (ids, values): the values written and the component marked present) and a remover (W u:d_ropPos ids), and u:w_orld n, a world of n slots: every column number worked out as a plain number. A malformed component stops the compiler: error[bad-components]. The left side is the namespace of the names it writes: "u" in a program, "h" (private) or "l" (exported) in a library. It reads its text as a list of components rather than copying it (uses: parsed).
"ec:" u̲se< "Ecs"
"u" ᵉᶜc̲omponents< "pos:2 hp"
W ← ᵘw̲orld 3
s̲hape W 3 6
healed ← W ᵘs̲etHp (2, 7.0)
ᵘh̲p healed 0.0 7.0 0.0
ᵘh̲asHp healed 0 1 0
ᵐc̲omponents< ← { ns t → n ← ʰt̲rim ns n̲ot (n m̲atch "u") ∨ (n m̲atch "h") ∨ n m̲atch "l" ? "bad-components left" ⎕R̲EJECT "the names' namespace: \"u\" in a program, \"h\" or \"l\" in a library" ws ← ʰw̲ords t 0 = t̲ally ws ? "bad-components right" ⎕R̲EJECT "no components: write them name or name:width" bad ← (n̲ot '{ ʰc̲omponent? d̲isclose ⍵ } e̲ach ws) r̲eplicate ws 0 < t̲ally bad ? "bad-components right" ⎕R̲EJECT (d̲isclose f̲irst bad) c̲at " is not a component: write a lowercase name, then :width when wider than 1" k ← '{ ʰw̲idth d̲isclose ⍵ } e̲ach ws p ← 2 + 0 c̲at -1 d̲rop '+ s̲\ 1 + k cols ← 1 + '+ r̲/ 1 + k world ← n c̲at ":w_orld := { n -> (n c_at " c̲at (f̲ormat cols) c̲at ") r_eshape 0.0 }\n" ᵗj̲oin (e̲nclose world) c̲at '{ i → ʰd̲efs (d̲isclose i s̲elect ws, i s̲elect p, i s̲elect k, cols, n) } m̲ap r̲ange t̲ally ws }
ʰd̲efs : (Char, Int, Int, Int, Char) -> Char
The four definitions for one component: its word, its presence column p, its width k, the world's number of columns c, and the namespace s.
ʰd̲efs ← { (w, p, k, c, s) → n ← ʰn̲ame w T ← ʰt̲itle n P ← f̲ormat p F ← f̲ormat p + r̲ange k PF ← f̲ormat p c̲at p + r̲ange k get ← s c̲at ":" c̲at (ʰg̲etter n) c̲at " := { W -> (" c̲at F c̲at ") s_elect_2 W }\n" has ← s c̲at ":h_as" c̲at T c̲at " := { W -> 0 < " c̲at P c̲at " s_elect_2 W }\n" set1 ← s c̲at ":s_et" c̲at T c̲at " := { W (ids, v) ->\n i := r_avel ids\n V := (((t_ally i) c_at 1) r_eshape 1.0) c_at_2 ((t_ally i) c_at " c̲at (f̲ormat k) c̲at ") r_eshape v\n" set2 ← " R := f_loat (r_ange t_ally W) '= t_able i\n C := f_loat (" c̲at PF c̲at ") '= t_able r_ange " c̲at (f̲ormat c) c̲at "\n" set3 ← " M := 0 < R '+ '* i_nner (((t_ally i) c_at " c̲at (f̲ormat 1 + k) c̲at ") r_eshape 1.0) '+ '* i_nner C\n (W * f_loat n_ot M) + R '+ '* i_nner V '+ '* i_nner C\n}\n" drop ← s c̲at ":d_rop" c̲at T c̲at " := { W ids -> W * f_loat n_ot ((r_ange t_ally W) m_ember? r_avel ids) '* t_able " c̲at P c̲at " = r_ange " c̲at (f̲ormat c) c̲at " }\n" get c̲at has c̲at set1 c̲at set2 c̲at set3 c̲at drop }