sourcelibs/Extrusion/src/Extrusion.xtlm

1⍝│ Extrusion: named contours and letter solids validated at compile time. 2⍝│ Imported with the functions: "ex:" u_se< "Extrusion". 3⍝│ One statement macro creates ordinary numeric bindings; no caller helper 4⍝│ alias, imports, renderer calls or run-time parser appear in its expansion. 5 6"ex:" u̲se< "Extrusion.xtl" 7"pa:" u̲se< "Parsing" 8"t:" u̲se< "Strings" 9 10⍝│ Refuse an invalid geometry declaration, locating it at the right text. 11ʰb̲ad ← { message → "bad-extrusion" ⎕S̲IGNAL message } 12 13⍝│ Remove one optional leading sign from a numeric component. 14ʰu̲nsigned ← { text → 15 0 = t̲ally text ? text 16 (f̲irst text) m̲ember? "+-" ? 1 d̲rop text 17 text 18} 19 20⍝│ Whether unsigned text is a decimal; Parsing checks its padded form. 21ʰd̲ecimal? ← { text → 22 0 = t̲ally w̲here text m̲ember? ⎕D ? 0 = 1 23 0 = t̲ally text ? 0 = 1 24 a ← { @ → (f̲irst text) = f̲irst "." ? "0" c̲at text◆ text } @ 25 b ← { @ → (f̲irst -1 t̲ake a) = f̲irst "." ? a c̲at "0"◆ a } @ 26 ᵖᵃn̲umber? b 27} 28 29⍝│ Parse exactly one finite signed decimal, with optional signed exponent. 30ʰn̲umber ← { text → 31 b ← ʰu̲nsigned text 32 at ← w̲here b m̲ember? "eE" 33 (t̲ally at) > 1 ? ʰb̲ad "invalid number: " c̲at text 34 mantissa ← { @ → 0 = t̲ally at ? b◆ ((f̲irst at) − 1) t̲ake b } @ 35 n̲ot ʰd̲ecimal? mantissa ? ʰb̲ad "invalid number: " c̲at text 36 exponent ← { @ → 0 = t̲ally at ? "0"◆ ʰu̲nsigned (f̲irst at) d̲rop b } @ 37 n̲ot (0 < t̲ally exponent) ∧ '∧ r̲/ exponent m̲ember? ⎕D ? ʰb̲ad "invalid exponent: " c̲at text 38 nums ← n̲umbers text 39 n̲ot 1 = t̲ally nums ? ʰb̲ad "expected one number: " c̲at text 40 x ← f̲irst nums 41 n̲ot (x − x) = 0.0 ? ʰb̲ad "number must be finite: " c̲at text 42 x 43} 44 45⍝│ Serialize a finite ordinary numeric array as self-contained source. 46ʰa̲rray ← { a → 47 (f̲ormat s̲hape a) c̲at " r_eshape " c̲at f̲ormat r̲avel a 48} 49 50⍝│ Contour mode: all xy points parsed, then ordinary extrusion validates. 51ʰc̲ontour ← { name depth text → 52 parts ← ";" ᵖᵃs̲plit text 53 n̲ot (t̲ally parts) = 1 + t̲ally w̲here text = f̲irst ";" ? ʰb̲ad "contour has an empty point or trailing semicolon" 54 rows ← '{ p → 55 point ← d̲isclose p 56 words ← ᵗw̲ords point 57 n̲ot 2 = t̲ally words ? ʰb̲ad "a contour point needs exactly x y: " c̲at point 58 (ʰn̲umber d̲isclose 1 s̲elect words) c̲at ʰn̲umber d̲isclose 2 s̲elect words 59 } m̲ap parts 60 flat ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ rows 61 contour ← o̲\ ((t̲ally parts) c̲at 2) r̲eshape flat 62 mesh ← depth ᵉˣe̲xtrude contour 63 name c̲at "contour := " c̲at (ʰa̲rray contour) c̲at "\n" c̲at name c̲at "triangles := " c̲at ʰa̲rray mesh 64} 65 66⍝│ Letter mode: one nonempty supported word, deterministic advance array. 67ʰl̲etters ← { name depth text → 68 words ← ᵗw̲ords text 69 n̲ot 1 = t̲ally words ? ʰb̲ad "letters needs one nonempty word: " c̲at text 70 letters ← d̲isclose f̲irst words 71 mesh ← depth ᵉˣl̲etters letters 72 advances ← '{ ch → ᵉˣa̲dvance 1 r̲eshape ch } e̲ach letters 73 name c̲at "advances := " c̲at (ʰa̲rray advances) c̲at "\n" c̲at name c̲at "triangles := " c̲at ʰa̲rray mesh 74} 75 76⍝│ "tile" ex:e_xtrude< "depth 0.5 contour 0 0; 2 0; 2 1; 0 1" creates 77⍝│ tiledepth, tilecontour and tiletriangles. With "depth .3 letters X_eTaL" 78⍝│ it creates the prefix's depth, advances and triangles. Names are bindings, 79⍝│ not an expression result; the ordinary functions work without the macro. 80⍝│ takes: text text 81⍝│ uses: parsed 82ᵐe̲xtrude< ← { name text → 83 n̲ot ᵖᵃn̲ame? name ? "bad-extrusion left" ⎕R̲EJECT "geometry prefix must be a lowercase variable name: " c̲at name 84 ⍝ Catch ordinary geometry refusals and report their same reason at the call. 85 '{ @ → 86 words ← ᵗw̲ords text 87 (t̲ally words) < 4 ? ʰb̲ad "expected depth NUMBER contour POINTS or depth NUMBER letters TEXT" 88 n̲ot "depth" m̲atch d̲isclose 1 s̲elect words ? ʰb̲ad "declaration must start with depth: " c̲at text 89 dtext ← d̲isclose 2 s̲elect words 90 depth ← ʰn̲umber dtext 91 mode ← d̲isclose 3 s̲elect words 92 ⍝ Take the remaining source by consuming the three header words. 93 first ← ᵗt̲rim (t̲ally d̲isclose 1 s̲elect words) d̲rop ᵗt̲rim text 94 second ← ᵗt̲rim (t̲ally dtext) d̲rop first 95 body ← ᵗt̲rim (t̲ally mode) d̲rop second 96 prefix ← name c̲at "depth := " c̲at (f̲ormat depth) c̲at "\n" 97 mode m̲atch "contour" ? prefix c̲at ((name ʰc̲ontour depth) body) 98 mode m̲atch "letters" ? prefix c̲at ((name ʰl̲etters depth) body) 99 ʰb̲ad "unknown geometry mode: " c̲at mode 100 } ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-extrusion right" ⎕R̲EJECT ⎕E̲MESSAGE e } 101}