macro librarylibs/Extrusion/src/Extrusion.xtlm

Extrusion: named contours and letter solids validated at compile time. Imported with the functions: "ex:" u_se< "Extrusion". One statement macro creates ordinary numeric bindings; no caller helper alias, imports, renderer calls or run-time parser appear in its expansion.

source · imports ex: libs/Extrusion/src/Extrusion.xtl; pa: libs/Parsing/src/Parsing.xtl; t: libs/Strings/src/Strings.xtl

ʰb̲ad : Any a => Char -> a

function (private) · line 11

Refuse an invalid geometry declaration, locating it at the right text.

ʰb̲ad ← { message → "bad-extrusion" ⎕S̲IGNAL message }

ʰu̲nsigned : Char -> Char

function (private) · line 14

Remove one optional leading sign from a numeric component.

ʰu̲nsigned ← { text →
  0 = t̲ally text ? text
  (f̲irst text) m̲ember? "+-" ? 1 d̲rop text
  text
}
Used in: ʰn̲umber

ʰd̲ecimal? : Truthy a => Char -> a

function (private) · line 21

Whether unsigned text is a decimal; Parsing checks its padded form.

ʰd̲ecimal? ← { text →
  0 = t̲ally w̲here text m̲ember? ⎕D ? 0 = 1
  0 = t̲ally text ? 0 = 1
  a ← { @ → (f̲irst text) = f̲irst "." ? "0" c̲at text◆ text } @
  b ← { @ → (f̲irst -1 t̲ake a) = f̲irst "." ? a c̲at "0"◆ a } @
  ᵖᵃn̲umber? b
}
Used in: ʰn̲umber

ʰn̲umber : Char -> Float

function (private) · line 30

Parse exactly one finite signed decimal, with optional signed exponent.

ʰn̲umber ← { text →
  b ← ʰu̲nsigned text
  at ← w̲here b m̲ember? "eE"
  (t̲ally at) > 1 ? ʰb̲ad "invalid number: " c̲at text
  mantissa ← { @ → 0 = t̲ally at ? b◆ ((f̲irst at) − 1) t̲ake b } @
  n̲ot ʰd̲ecimal? mantissa ? ʰb̲ad "invalid number: " c̲at text
  exponent ← { @ → 0 = t̲ally at ? "0"◆ ʰu̲nsigned (f̲irst at) d̲rop b } @
  n̲ot (0 < t̲ally exponent) ∧ '∧ r̲/ exponent m̲ember? ⎕D ? ʰb̲ad "invalid exponent: " c̲at text
  nums ← n̲umbers text
  n̲ot 1 = t̲ally nums ? ʰb̲ad "expected one number: " c̲at text
  x ← f̲irst nums
  n̲ot (x − x) = 0.0 ? ʰb̲ad "number must be finite: " c̲at text
  x
}

ʰa̲rray : a -> Char

function (private) · line 46

Serialize a finite ordinary numeric array as self-contained source.

ʰa̲rray ← { a →
  (f̲ormat s̲hape a) c̲at " r_eshape " c̲at f̲ormat r̲avel a
}

ʰc̲ontour : Num a => Char -> a -> Char -> Char

function (private) · line 51

Contour mode: all xy points parsed, then ordinary extrusion validates.

ʰc̲ontour ← { name depth text →
  parts ← ";" ᵖᵃs̲plit text
  n̲ot (t̲ally parts) = 1 + t̲ally w̲here text = f̲irst ";" ? ʰb̲ad "contour has an empty point or trailing semicolon"
  rows ← '{ p →
    point ← d̲isclose p
    words ← ᵗw̲ords point
    n̲ot 2 = t̲ally words ? ʰb̲ad "a contour point needs exactly x y: " c̲at point
    (ʰn̲umber d̲isclose 1 s̲elect words) c̲at ʰn̲umber d̲isclose 2 s̲elect words
  } m̲ap parts
  flat ← d̲isclose '{ e̲nclose (d̲isclose ⍺) c̲at d̲isclose ⍵ } r̲/ rows
  contour ← o̲\ ((t̲ally parts) c̲at 2) r̲eshape flat
  mesh ← depth ᵉˣe̲xtrude contour
  name c̲at "contour := " c̲at (ʰa̲rray contour) c̲at "\n" c̲at name c̲at "triangles := " c̲at ʰa̲rray mesh
}
Used in: ᵐe̲xtrude<

ʰl̲etters : Num a => Char -> a -> Char -> Char

function (private) · line 67

Letter mode: one nonempty supported word, deterministic advance array.

ʰl̲etters ← { name depth text →
  words ← ᵗw̲ords text
  n̲ot 1 = t̲ally words ? ʰb̲ad "letters needs one nonempty word: " c̲at text
  letters ← d̲isclose f̲irst words
  mesh ← depth ᵉˣl̲etters letters
  advances ← '{ ch → ᵉˣa̲dvance 1 r̲eshape ch } e̲ach letters
  name c̲at "advances := " c̲at (ʰa̲rray advances) c̲at "\n" c̲at name c̲at "triangles := " c̲at ʰa̲rray mesh
}
Used in: ᵐe̲xtrude<

ᵐe̲xtrude< : Char -> Char -> Char

macro · line 82

"tile" ex:e_xtrude< "depth 0.5 contour 0 0; 2 0; 2 1; 0 1" creates tiledepth, tilecontour and tiletriangles. With "depth .3 letters X_eTaL" it creates the prefix's depth, advances and triangles. Names are bindings, not an expression result; the ordinary functions work without the macro.

ᵐe̲xtrude< ← { name text →
  n̲ot ᵖᵃn̲ame? name ? "bad-extrusion left" ⎕R̲EJECT "geometry prefix must be a lowercase variable name: " c̲at name
  ⍝ Catch ordinary geometry refusals and report their same reason at the call.
  '{ @ →
    words ← ᵗw̲ords text
    (t̲ally words) < 4 ? ʰb̲ad "expected depth NUMBER contour POINTS or depth NUMBER letters TEXT"
    n̲ot "depth" m̲atch d̲isclose 1 s̲elect words ? ʰb̲ad "declaration must start with depth: " c̲at text
    dtext ← d̲isclose 2 s̲elect words
    depth ← ʰn̲umber dtext
    mode ← d̲isclose 3 s̲elect words
    ⍝ Take the remaining source by consuming the three header words.
    first ← ᵗt̲rim (t̲ally d̲isclose 1 s̲elect words) d̲rop ᵗt̲rim text
    second ← ᵗt̲rim (t̲ally dtext) d̲rop first
    body ← ᵗt̲rim (t̲ally mode) d̲rop second
    prefix ← name c̲at "depth := " c̲at (f̲ormat depth) c̲at "\n"
    mode m̲atch "contour" ? prefix c̲at ((name ʰc̲ontour depth) body)
    mode m̲atch "letters" ? prefix c̲at ((name ʰl̲etters depth) body)
    ʰb̲ad "unknown geometry mode: " c̲at mode
  } ⎕T̲RAP '{ e → ⎕R̲ECOVER "bad-extrusion right" ⎕R̲EJECT ⎕E̲MESSAGE e }
}