programdemos/rosetta/rosetta.xtl

source · imports cm: demos/rosetta/Comparison.xtl; st: demos/rosetta/Stone.xtl; v: lib/Svg.xtl

data : Char

value · line 16
data ← "demos/rosetta/data.toml"

idioms : Box Char

value · line 17
idioms ← data ⎕L̲IST "idioms"
Used in: ᵘw̲here, start

idiomNames : Box Char

value · line 18
idiomNames ← data ⎕L̲IST "idiom_names"
Used in: ᵘs̲tone

languages : Box Char

value · line 19
languages ← data ⎕L̲IST "languages"

languageNames : Box Char

value · line 20
languageNames ← data ⎕L̲IST "language_names"
Used in: ᵘf̲ace

ᵘt̲able : Char -> Box Char

function · line 21
ᵘt̲able ← { name → ((e̲nclose data) c̲at e̲nclose name) ⎕T̲ABLE "idioms" "languages" }
Used in: source, output, notes, spans

source : Box Char

value · line 22
source ← ᵘt̲able "source"
Used in: ᵘf̲ace

output : Box Char

value · line 23
output ← ᵘt̲able "output"
Used in: ᵘf̲ace

notes : Box Char

value · line 24
notes ← ᵘt̲able "notes"
Used in: ᵘf̲ace

spans : Box Char

value · line 25
spans ← ᵘt̲able "spans"
Used in: ᵘc̲olored

ᵘc̲olor : Char -> Char

function · line 30
ᵘc̲olor ← { class →
  class m̲atch "builtin" ? "#1d4ed8"
  class m̲atch "userfunc" ? "#7c3aed"
  class m̲atch "libfunc" ? "#7c3aed"
  class m̲atch "macro" ? "#7c3aed"
  class m̲atch "number" ? "#b45309"
  class m̲atch "string" ? "#15803d"
  class m̲atch "symbol" ? "#111827"
  class m̲atch "comment" ? "#6b7280"
  class m̲atch "lambdaarg" ? "#0f766e"
  class m̲atch "keyword" ? "#1d4ed8"
  class m̲atch "function" ? "#1d4ed8"
  class m̲atch "operator" ? "#111827"
  class m̲atch "name" ? "#000000"
  "#000000"
}
Used in: ᵘr̲uns

ᵘr̲uns : Box Char -> Char

function · line 49
ᵘr̲uns ← { m → ᵘj̲oin '{ k → (ᵘc̲olor d̲isclose 2 s̲elect k s̲elect m) ᵛs̲pan d̲isclose 1 s̲elect k s̲elect m } m̲ap r̲ange 1 s̲elect s̲hape m }
Used in: ᵘc̲olored

ᵘg̲uess : Char -> Box Char

function · line 57
ᵘg̲uess ← { src →
  cls ← (src m̲ember? "0123456789") + (2 × src m̲ember? "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_") + 3 × src = f̲irst " "
  starts ← 1 c̲at (1 d̲rop cls) ≠ -1 d̲rop cls
  runs ← ('+ s̲\ starts) p̲artition src
  kinds ← (e̲nclose "builtin") c̲at (e̲nclose "number") c̲at (e̲nclose "name") c̲at e̲nclose "plain"
  classes ← (1 + (w̲here starts) s̲elect cls) s̲elect kinds
  o̲\ (2 c̲at t̲ally runs) r̲eshape runs c̲at classes
}
Used in: ᵘs̲pans

ᵘs̲pans : Char -> Char -> Box Char

function · line 68
ᵘs̲pans ← { src spans →
  0 = t̲ally spans ? ᵘg̲uess src
  pieces ← (n̲ot spans = f̲irst "|") p̲artition spans
  o̲ne ← { p → t ← d̲isclose p◆ cut ← t i̲ndexOf f̲irst " "◆ (e̲nclose cut d̲rop t) c̲at e̲nclose (cut − 1) t̲ake t }
  ((t̲ally pieces) c̲at 2) r̲eshape d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ 'o̲ne m̲ap pieces
}
Used in: ᵘc̲olored

ᵘc̲olored : Int -> Char -> Char

function · line 78
ᵘc̲olored ← { il src →
  i ← 1 s̲elect il
  l ← 2 s̲elect il
  (d̲isclose l s̲elect languages) m̲atch "xetal" ? ᵘr̲uns ⎕V̲IEW src
  ᵘr̲uns src ᵘs̲pans d̲isclose l s̲elect i s̲elect spans
}
Used in: ᵘf̲ace

fits : Int

value · line 88
fits ← 15

smallest : Float

value · line 89
smallest ← 0.05
Used in: ᵘf̲itted

ᵘf̲itted : Int -> Char -> Char

function · line 93
ᵘf̲itted ← { n markup →
  n ≤ fits ? markup
  size ← 0.1 × (f̲loat fits) ÷ f̲loat n
  (size m̲ax smallest) ᵛs̲ized markup
}
Used in: ᵘf̲ace

ᵘs̲plit : Char -> Box Char

function · line 102
ᵘs̲plit ← { src →
  n ← t̲ally src
  n ≤ fits ? e̲nclose src
  spaces ← w̲here src = f̲irst " "
  0 = t̲ally spaces ? e̲nclose src
  away ← a̲bs (2 × spaces) − n
  cut ← (away i̲ndexOf 'm̲in r̲/ away) s̲elect spaces
  (e̲nclose (cut − 1) t̲ake src) c̲at e̲nclose cut d̲rop src
}
Used in: ᵘf̲ace

ᵘl̲ines : Int -> Box Char -> Box Char

function · line 114
ᵘl̲ines ← { width ws →
  0 = t̲ally ws ? ws
  lens ← '{ b → t̲ally d̲isclose b } e̲ach ws
  room ← width ≥ (('+ s̲\ lens) + r̲ange t̲ally ws) − 1
  k ← 1 m̲ax '+ r̲/ room
  line ← d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at " " c̲at d̲isclose y } r̲/ k t̲ake ws
  (e̲nclose line) c̲at width ᵘl̲ines k d̲rop ws
}

ᵘw̲rap : Int -> Char -> Box Char

function · line 124
ᵘw̲rap ← { width t → width ᵘl̲ines (n̲ot t = f̲irst " ") p̲artition t }
Used in: ᵘf̲ace

ᵘo̲neLine : Char -> Char

function · line 128
ᵘo̲neLine ← { out → d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at " / " c̲at d̲isclose y } r̲/ (n̲ot out = f̲irst "\n") p̲artition out }
Used in: ᵘf̲ace

ᵘf̲ace : Int -> Int -> Box Char

function · line 134
ᵘf̲ace ← { i l →
  name ← ᵛe̲scape d̲isclose l s̲elect languageNames
  src ← d̲isclose l s̲elect i s̲elect source
  out ← d̲isclose l s̲elect i s̲elect output
  note ← d̲isclose l s̲elect i s̲elect notes
  missing ← "0 = t_ally note" i̲f< "\"(no concise idiom)\"; note"
  result ← "0 = t_ally out" i̲f< "\"\"; \"-> \" c_at u:o_neLine out"
  n̲oteLine ← { b → 0.07 ᵛs̲ized "#4b5563" ᵛs̲pan d̲isclose b }
  0 = t̲ally src ? (e̲nclose name) c̲at 'n̲oteLine m̲ap 18 ᵘw̲rap missing
  c̲odeLine ← { b → (t̲ally d̲isclose b) ᵘf̲itted (i c̲at l) ᵘc̲olored d̲isclose b }
  code ← 'c̲odeLine m̲ap ᵘs̲plit src
  (e̲nclose name) c̲at code c̲at e̲nclose (t̲ally result) ᵘf̲itted "#4b5563" ᵛs̲pan result
}
i̲f< expands to
({ @ → (0 = t̲ally note) ? "(no concise idiom)"◆ note } @)
i̲f< expands to
({ @ → (0 = t̲ally out) ? ""◆ "-> " c̲at ᵘo̲neLine out } @)
Used in: ᵘs̲tone

ᵘi̲tem : Float -> Float -> Int

function · line 150
ᵘi̲tem ← { ao state →
  axis ← 1 s̲elect ao
  n ← f̲loor 0.5 + (axis c̲at ᶜᵐCOUNT) ᶜᵐa̲t state
  n ᶜᵐw̲rap 1 + (f̲loor 2 s̲elect ao) + ˢᵗb̲ase (axis c̲at ᶜᵐANGLE) ᶜᵐa̲t state
}

ᵘs̲tone : Float -> Char

function · line 162
ᵘs̲tone ← { state →
  i ← (ᶜᵐIDIOM c̲at 0.0) ᵘi̲tem state
  roll ← (ᶜᵐIDIOM c̲at ᶜᵐANGLE) ᶜᵐa̲t state
  top ← 1 ˢᵗr̲ing ((ᶜᵐTOP c̲at ᶜᵐANGLE) ᶜᵐa̲t state) c̲at roll
  bottom ← -1 ˢᵗr̲ing ((ᶜᵐBOTTOM c̲at ᶜᵐANGLE) ᶜᵐa̲t state) c̲at roll
  all ← top c̲at bottom
  c̲apName ← { k → ᵛe̲scape d̲isclose ((ᶜᵐIDIOM c̲at f̲loat k s̲elect ˢᵗcapOffsets) ᵘi̲tem state) s̲elect idiomNames }
  c̲apText ← { k →
    k = 5 ? c̲apName 1
    k = 12 ? c̲apName 2
    ""
  }
  c̲ap ← { k → (e̲nclose "#e9e4db") c̲at (e̲nclose "") c̲at e̲nclose c̲apText k }
  t̲opPanel ← { k → (e̲nclose "#fdfcfa") c̲at i ᵘf̲ace (ᶜᵐTOP c̲at f̲loat k s̲elect ˢᵗringOffsets) ᵘi̲tem state }
  b̲ottomPanel ← { k → (e̲nclose "#f4f1ec") c̲at i ᵘf̲ace (ᶜᵐBOTTOM c̲at f̲loat k s̲elect ˢᵗringOffsets) ᵘi̲tem state }
  l̲ines ← { k →
    k ≤ 4 ? t̲opPanel k
    k ≤ 6 ? c̲ap k
    k ≤ 10 ? b̲ottomPanel k − 6
    c̲ap k
  }
  p̲anel ← { k → (l̲ines k) ˢᵗp̲anel k s̲elect all }
  painted ← ᵘj̲oin 'p̲anel m̲ap ˢᵗp̲ainting all
  caption ← ((ᵛa̲t (ˢᵗsize ÷ 2) c̲at 28) c̲at ("font-size" ᵛa̲ttr "18") c̲at ("text-anchor" ᵛa̲ttr "middle") c̲at (ᵛf̲ill "#374151") c̲at "font-family" ᵛa̲ttr "ui-sans-serif, system-ui, sans-serif") ᵛt̲ext d̲isclose i s̲elect idiomNames
  (ˢᵗsize c̲at ˢᵗsize) ᵛp̲icture caption c̲at painted
}
Used in: ᵘl̲oop

ᵘj̲oin : Box Char -> Char

function · line 189
ᵘj̲oin ← { b → d̲isclose '{ x y → e̲nclose (d̲isclose x) c̲at d̲isclose y } r̲/ b }

ᵘw̲here : Float -> Char

function · line 196
ᵘw̲here ← { s →
  i ← d̲isclose ((ᶜᵐIDIOM c̲at 0.0) ᵘi̲tem s) s̲elect idioms
  t ← d̲isclose ((ᶜᵐTOP c̲at 0.0) ᵘi̲tem s) s̲elect languages
  b ← d̲isclose ((ᶜᵐBOTTOM c̲at 0.0) ᵘi̲tem s) s̲elect languages
  "at " c̲at i c̲at " " c̲at t c̲at " " c̲at b
}
Used in: ᵘl̲oop, shown

ᵘl̲oop : Float -> Float

function · line 207
ᵘl̲oop ← { state →
  e ← ⎕E̲VENT @
  kind ← ⎕E̲KIND e
  kind m̲atch "end" ? state
  next ← state ᶜᵐu̲pdate e
  "(u:w_here state) m_atch u:w_here next" u̲nless< "p_rint! u:w_here next"
  "(cm:m_ode state) m_atch cm:m_ode next" u̲nless< "p_rint! \"mode \" c_at cm:m_ode next"
  "n_ot kind m_atch \"tick\"" u̲nless< "[]S_HOW u:s_tone next"
  ᵘl̲oop next
}
u̲nless< expands to
{ @ → ((ᵘw̲here state) m̲atch ᵘw̲here next) ? @◆ p̲rint! ᵘw̲here next◆ @ } @
u̲nless< expands to
{ @ → ((ᶜᵐm̲ode state) m̲atch ᶜᵐm̲ode next) ? @◆ p̲rint! "mode " c̲at ᶜᵐm̲ode next◆ @ } @
u̲nless< expands to
{ @ → (n̲ot kind m̲atch "tick") ? @◆ ⎕S̲HOW ᵘs̲tone next◆ @ } @

start : Float

value · line 221
start ← ᶜᵐr̲esume ᶜᵐs̲ettle (ᶜᵐBOTTOM c̲at 2.0) ᶜᵐc̲hoose (t̲ally idioms) ᶜᵐs̲tart t̲ally languages

shown : Char

value · line 222
shown ← p̲rint! ᵘw̲here start

mode : Char

value · line 223
mode ← p̲rint! "mode " c̲at ᶜᵐm̲ode start