sourcelib/Ecs.xtlm

1⍝│ Ecs: its macro library -- c_omponents<, a world's layout written from 2⍝│ its list of components. Imported with the functions, under one alias: 3⍝│ "ec:" u_se< "Ecs". A macro is a function from the source text on each 4⍝│ side of its call to the source that replaces the call, run before the 5⍝│ program is compiled (X_eTaL MC10); this one creates names, which a 6⍝│ function cannot. Names with m: are macros; those under h: are private 7⍝│ to this file. 8 9"t:" u̲se< "Text" 10 11lowers ← "abcdefghijklmnopqrstuvwxyz" 12 13⍝━━ Reading the list 14 15⍝│ The words of a text. 16ʰw̲ords ← { t → (n̲ot t m̲ember? " \n\t") p̲artition t } 17⍝│ A component's name: a word up to its colon. 18ʰn̲ame ← { w → (n̲ot '∨ s̲\ w = f̲irst ":") r̲eplicate w } 19⍝│ A component's width: the number after its colon, else 1. 20ʰw̲idth ← { w → 21 n ← n̲umbers ('∨ s̲\ 0 c̲at -1 d̲rop w = f̲irst ":") r̲eplicate w 22 0 = t̲ally n ? 1 23 f̲loor f̲irst n 24} 25⍝│ Whether a word is a component: a lowercase name of letters, an 26⍝│ optional :width. 27ʰc̲omponent? ← { w → 28 n ← ʰn̲ame w 29 (0 < t̲ally n) ∧ '∧ r̲/ n m̲ember? lowers 30} 31⍝│ The function names for a component: p_os, h_asPos, s_etPos, d_ropPos 32⍝│ for pos. 33ʰg̲etter ← { n → (1 t̲ake n) c̲at "_" c̲at 1 d̲rop n } 34⍝│ A text without spaces at its ends. 35ʰt̲rim ← { t → (n̲ot (r̲ev '∧ s̲\ r̲ev t = f̲irst " ") ∨ '∧ s̲\ t = f̲irst " ") r̲eplicate t } 36ʰt̲itle ← { n → ((lowers i̲ndexOf 1 t̲ake n) s̲elect ⎕A) c̲at 1 d̲rop n } 37 38⍝━━ The macro 39 40⍝│ "u" ec:c_omponents< "pos:2 vel:2 hp": a world's layout from its 41⍝│ components, each a name and a width (1 when left out). Column 1 says 42⍝│ whether a slot is alive; each component takes a column saying whether 43⍝│ an entity has it, then its fields. When the program is compiled it 44⍝│ writes, for each component, a getter (u:p_os W: the column, or the 45⍝│ columns when wider than 1), a mask (u:h_asPos W), a setter 46⍝│ (W u:s_etPos (ids, values): the values written and the component 47⍝│ marked present) and a remover (W u:d_ropPos ids), and u:w_orld n, a 48⍝│ world of n slots: every column number worked out as a plain number. 49⍝│ A malformed component stops the compiler: error[bad-components]. 50⍝│ The left side is the namespace of the names it writes: "u" in a 51⍝│ program, "h" (private) or "l" (exported) in a library. It reads its 52⍝│ text as a list of components rather than copying it (uses: parsed). 53⍝│ takes: text text 54⍝│ uses: parsed 55⍝│ >> "ec:" u_se< "Ecs" 56⍝│ >> "u" ec:c_omponents< "pos:2 hp" 57⍝│ >> W := u:w_orld 3 58⍝│ >> s_hape W 59⍝│ 3 6 60⍝│ >> healed := W u:s_etHp (2, 7.0) 61⍝│ >> u:h_p healed 62⍝│ 0.0 7.0 0.0 63⍝│ >> u:h_asHp healed 64⍝│ 0 1 0 65ᵐc̲omponents< ← { ns t → 66 n ← ʰt̲rim ns 67 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" 68 ws ← ʰw̲ords t 69 0 = t̲ally ws ? "bad-components right" ⎕R̲EJECT "no components: write them name or name:width" 70 bad ← (n̲ot '{ ʰc̲omponent? d̲isclose ⍵ } e̲ach ws) r̲eplicate ws 71 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" 72 k ← '{ ʰw̲idth d̲isclose ⍵ } e̲ach ws 73 p ← 2 + 0 c̲at -1 d̲rop '+ s̲\ 1 + k 74 cols ← 1 + '+ r̲/ 1 + k 75 world ← n c̲at ":w_orld := { n -> (n c_at " c̲at (f̲ormat cols) c̲at ") r_eshape 0.0 }\n" 76 ᵗ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 77} 78⍝│ The four definitions for one component: its word, its presence column 79⍝│ p, its width k, the world's number of columns c, and the namespace s. 80ʰd̲efs ← { (w, p, k, c, s) → 81 n ← ʰn̲ame w 82 T ← ʰt̲itle n 83 P ← f̲ormat p 84 F ← f̲ormat p + r̲ange k 85 PF ← f̲ormat p c̲at p + r̲ange k 86 get ← s c̲at ":" c̲at (ʰg̲etter n) c̲at " := { W -> (" c̲at F c̲at ") s_elect_2 W }\n" 87 has ← s c̲at ":h_as" c̲at T c̲at " := { W -> 0 < " c̲at P c̲at " s_elect_2 W }\n" 88 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" 89 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" 90 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" 91 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" 92 get c̲at has c̲at set1 c̲at set2 c̲at set3 c̲at drop 93}