sourcelibs/Bits/src/Bits.xtlm
1⍝│ Bits: its macro library -- f_ields<, named bit fields.
2⍝│ Imported with the functions, under one alias: "b:" u_se< "Bits".
3⍝│ A macro is a function from the source text written left and right of
4⍝│ its call to the source that replaces the call, run before the program
5⍝│ is compiled (X_eTaL MC10). Names with m: are macros; those under h: are
6⍝│ private to this file.
7
8"t:" u̲se< "Strings"
9"pa:" u̲se< "Parsing"
10
11lowers ← "abcdefghijklmnopqrstuvwxyz"
12uppers ← ⎕A
13
14⍝━━ Parsing fields
15
16⍝│ A field's name and width, from "name:width": the colon's position
17⍝│ (Parsing's pa:a_t, as any library finds a separator) cuts it.
18ʰc̲olon ← { f → ":" ᵖᵃa̲t f }
19ʰf̲ieldName ← { f → ((ʰc̲olon f) − 1) t̲ake f }
20ʰf̲ieldWidth ← { f → f̲loor f̲irst n̲umbers (ʰc̲olon f) d̲rop f }
21
22⍝│ Whether a field is "name:width": a lowercase name of letters and
23⍝│ digits (Parsing's pa:n_ame?), a whole width; name and width split
24⍝│ the field completely, so nothing between them is ever dropped.
25ʰf̲ield? ← { f →
26 1 ≠ t̲ally w̲here f = f̲irst ":" ? 0 = 1
27 n ← ʰf̲ieldName f
28 w ← (ʰc̲olon f) d̲rop f
29 (ᵖᵃn̲ame? n) ∧ (0 < t̲ally w) ∧ '∧ r̲/ w m̲ember? ⎕D
30}
31
32⍝━━ The macro
33
34⍝│ The function names for a field: m_ode and s_etMode for "mode".
35ʰg̲etter ← { n → "u:" c̲at (1 t̲ake n) c̲at "_" c̲at 1 d̲rop n }
36ʰs̲etter ← { n → up ← f̲irst (lowers i̲ndexOf 1 t̲ake n) s̲elect uppers◆ "u:s_et" c̲at up c̲at 1 d̲rop n }
37
38⍝│ @ b:f_ields< "flag:1 mode:3 count:12": named fields of a packed whole
39⍝│ number, lowest bits first. When the program is compiled it writes,
40⍝│ for each field, a getter (u:m_ode x: the field of x) and a setter
41⍝│ (v u:s_etMode x: x with the field set to v), their offsets and sizes
42⍝│ worked out as plain numbers, so nothing is computed about the layout
43⍝│ at run time. A malformed field, or more than 62 bits, stops the
44⍝│ compiler: error[bad-fields]. Nothing goes on the left (@).
45⍝│ takes: @ text
46⍝│ uses: parsed
47ᵐf̲ields< ← { @ t →
48 fs ← ᵗw̲ords t
49 0 = t̲ally fs ? "bad-fields right" ⎕R̲EJECT "no fields: write them name:width, lowest bits first"
50 bad ← (n̲ot '{ ʰf̲ield? d̲isclose ⍵ } e̲ach fs) r̲eplicate fs
51 0 < t̲ally bad ? "bad-fields right" ⎕R̲EJECT (d̲isclose f̲irst bad) c̲at " is not a field: write name:width (a lowercase name, a whole width)"
52 ws ← '{ ʰf̲ieldWidth d̲isclose ⍵ } e̲ach fs
53 (0 < t̲ally w̲here ws = 0) ∨ 62 < '+ r̲/ ws ? "bad-fields right" ⎕R̲EJECT "the fields take " c̲at (f̲ormat '+ r̲/ ws) c̲at " bits: each 1 or more, 62 in all at most"
54 offs ← (0 c̲at -1 d̲rop '+ s̲\ ws)
55 "\n" ᵗj̲oin '{ i →
56 f ← d̲isclose i s̲elect fs
57 n ← ʰf̲ieldName f
58 div ← f̲ormat 2 ^ f̲irst i s̲elect offs
59 size ← f̲ormat 2 ^ f̲irst i s̲elect ws
60 (ʰg̲etter n) c̲at " := { x -> (x d_iv " c̲at div c̲at ") m_od " c̲at size c̲at " }\n" c̲at (ʰs̲etter n) c̲at " := { v x -> x + (" c̲at div c̲at " * v m_od " c̲at size c̲at ") - " c̲at div c̲at " * (x d_iv " c̲at div c̲at ") m_od " c̲at size c̲at " }"
61 } m̲ap r̲ange t̲ally fs
62}