lf←@+10 charSet‿cgl←(∾ ≍○< ≠¨)⟨ "+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕«»⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!" # Function "˙˜˘¨⌜⁼´˝`" # Modifier "∘○⊸⟜⌾⊘◶⎉⚇⍟" # Composition lf∾"⋄," # Separator "←↩" # Gets "(){}⟨⟩" # Bracket "‿" # Ligature "·" # nOthing # Use last character in case of UTF-16 (like dzaima/BQN) ¯1⊏˘10‿∘⥊"𝕊𝕏𝕎𝔽𝔾𝕤𝕩𝕨𝕗𝕘" # Input (𝕣 pending; ℝ not allowed) '0'+↕10 # Digit "π∞¯." # Numeric "_"∾˜⥊"aA"+⌜↕na←26 # Alphabetic (¯1↓"𝕨")∾" "∾@+9 # Whitespace (or special name prefix in UTF-16) "#'""@" # Preprocessed characters ⟩ bF‿bM‿bC‿bS‿bG‿bB‿bL‿bO‿bI‿bD‿bN‿bA‿bW‿bP←≍¨˜⟜(+`»)cgl M←1⊸⊑(0⊸≤∧>)-⟜⊑ # ∊ for an init,length pair 𝕩 as above sep←⊑bS # CharCode converts characters to numbers, mostly the same as tokens ErrUnknownChars←{ ⟨"Unknown character","s"/˜1<≠𝕩,": ",𝕩⟩∾⊸!0 } CharCode←charSet{ Chk ← ⊢⊣ErrUnknownChars∘(≠/⊣)⍟≢⟜(⊏⟜𝕗) g←⍋𝕗 ⋄ ⊢ Chk g⊏˜1-˜1⌈(g⊏𝕗)⍋⊢ } vi←⊑bD # Start of identifier numbering charRole←∾⥊¨˜⟜(≠↑cgl˙)⟨1,2,3,¯1,¯1,¯1‿0,¯1,0,5/⌽↕2⟩ # For first vi chars T←⌈`× ⋄ IT←↕∘≠⊸T ⋄ I1T←(1+↕∘≠)⊸T # Source to ⟨tokens, roles, number of identifiers, literals⟩ # Identifiers then literal tokens are numbered starting at vi Tokenize←{ # Resolve comments and strings r←𝕩='#'⋄s←/0‿0⊸«⊸∧sm←𝕩='''⋄d←/dm←𝕩='"' g←⍋q←∾⟨ s⋄¯1↓d⋄/r⟩ ⋄q↩g⊏q # Open indices e← g⊏∾⟨2+s⋄ 1↓d⋄-⟜»∘⊏⟜(0∾+`r)⊸//(𝕩=lf)∾1⟩ # Matching close indices Se←{(⊏˜𝕨)Se 1¨⌾((𝕩/𝕨)⊸⊏)𝕩}⍟(0=¯1⊑⊢) # Mark reachable openings St←(≠𝕩)↑·/⁼((≠↑∾⟜≠Se 1∾0¨)q⍋e)⊸/ # All indices → reached mask a←St q⋄b←St e⋄f←¬≠`ab←a∨b # Open/close masks; filter "Unclosed quote"!¬∨´(sm∨dm)∧b↩qe←dm∧«a∧↩dm # Quote Escape "" str←1↓¨𝕩⊔˜1-˜(+`si←a>»qe)×≠`dm∧ab # Strings (indices /si) # Extract words: identifiers and numbers c←CharCode f/𝕩 w←»⊸n←l∧(+`w)⊏0∾tw<0 # Identifier/Number masks num←ReadNums n∨⟜«⊸/○(0⊸∾)c×l # Numbers ir←(us/˜«⊸us)×+`w>n)⊔c # Identifiers # Deduplicate literals and identifiers; other cleanup ki←((⍋⊏⟜n)⊸⊏/w)∾(ci∾/si)⊏+`»f # Indices in c k←id‿num‿chr‿str⋄k(⊢>¯1»⌈`)⊸/¨˜↩j←⊐¨k # IDs j into uniques k c↩(w∨¬l∨c M bW)/(∾j++`vi»≠¨k)⌾(ki⊸⊏)c # Add IDs; remove words/whitespace c-↩c(M×-⟜⊑)bS # Separators are equivalent c/˜↩¬(1»(c∊2‿4+⊑bB)∨⊢)⊸∧c=sep # Remove repeated and leading separators c/˜↩¬(1«c∊3‿5+⊑bB)∧c=sep # ...and trailing ones. In sequence for repeated trailing. cr←ir⌾((c M vi∾≠⊑k)⊸/)(vi⌊c)⊏charRole∾0 # Role c+↩5×c M⟨⊑bI,5⟩ # Case-insensitive special names ⟨c,cr,≠⊑k,∾1↓k⟩ } # 𝕩 is a list of tokens that contains the numeric literals, each # preceded by 0. Return the numbers. ErrNumericChars←{ ⟨"Letter","s"/˜1<≠𝕩," """,𝕩⊏charSet,""" not allowed in numbers"⟩∾⊸!0 } ReadNums←{ e‿p‿i‿n‿d←=⟜𝕩¨((⊑bA)+-´"ea")∾+⟜↕´bN # Masks for eπ∞¯. ErrNumericChars∘(/⟜𝕩)⍟(∨´) ¬e∨𝕩<⊑bA s←d∨c←e∨z←0=𝕩⋄m←¬n∨c "Negative sign in the middle of a number" ! ∧´n≤»c "Portion of a number is empty" ! ¬∨´(1«s)∧n∨s "Ill-formed decimal or exponent use" ! ∧´(0⊸=∨»⊸<)s/𝕩 "π and ∞ must occur alone" ! ∧´(p∨i)≤1(»∧(p∧«e)∨«)z∨n f←(17≥¬(⊢-T)+`)⊸∧(«≤(d<𝕩≠⊑bD)>○I1T¬)⊸∧m # No leading 0s; max 17 digits l←(¯1∾π‿1∾˜↕10)⊏˜(¬d)/f×𝕩-1-˜⊑bD # Digit lookup, with ∞ as 1 to avoid ∞×0 v←(>⟜«0≤l)/(0⊸≤××⟜10⊸+)`»«l # Numeric values—mantissas and exponents v×↩1‿¯1⊏˜(r←>⟜»m)/»n # Negate if ¯ vm←c/z # Mask of mantissas in l dp←vm/(»⊸<×-)⟜(⊏⟜(I1T d∾0))1+/>⟜«f # Decimal position t←10⋆|ee←dp-˜vm/«v׬vm # Power of 10 t÷˜⌾((0>ee)⊸/)t×⌾((0○≠◶⟨"opening"_mis_ 0, "closing"_mis_ 1˜⟩ 0 !˜ 𝕨 (Lcs<⌊○≠)◶⟨Msg,"Malformed bracket nesting"⟩ 𝕩 } ErrUndeclared←{ 0 !˜ "Undefined identifier"∾(1<≠𝕩)/"s" } Parse ← {r‿nv←𝕨 g←⍋pd←+`p←(¯1-2×r)×𝕩 M bB⋄br←p×𝕩M⟨2+⊑bB,2⟩ p(=⟜1⊸/ErrMismatchedBrackets⍟≢1-˜=⟜¯1⊸/)○(g⊸⊏)𝕩 "Swapped open and closed brackets" ! 0≤(⊑g)⊑pd sl←«⊸∨𝕩 M bL⋄sr←»⌾(g⊸⊏)sl⋄cp←𝕩=1+⊑bB ps←(𝕩=sep)∨sl∨p>0 g⊏˜↩⍋g⊏sl rev←⍋+`¯1↓(¯1∾g)(⊣⍋⊸⊏⊏˜⟜⍋¬⊏˜)⍋+`⊸+1∾g⊏ps gf←⍋fd←+`rev⊏br rev⊏˜↩gf⋄fd⊏˜↩gf 𝕩⊏˜↩rev⋄bv←rev⊏br⋄b←/bv<0⋄fi←+`c←bv>0 H←¬·1⊸«⊸/c(∨/⊣)= ft←(0∾1⊸H+2×2⊸H⌈2×3⊸H)(0‿3‿4‿5+5+⊑bI)⍋𝕩 fsc←ft⊏⥊3‿5‿6-⌜3‿0 pt←cp∧sn←¬sl∨sr r↩sl-˜sn×((1↓ft)⊏(1+2⊸≤)⊸/↕4)⌾((b⊏rev)⊸⊏)r r+↩pt(⊣∧≤=○I1T⊢)(»⌾(g⊸⊏)3=r)∨1≤r id←/idm←(0⊸≤∧<⟜nv)xv←𝕩-vi⋄idx←id⊏𝕩⋄sp←/𝕩 M bI ad←𝕩(=>○IT⊣=1+⊢)⊑bG g↩⍋+`rev⊏p-br⋄gr←g⊏rev⋄sll←1+2÷˜0(<-○/>)gr⊏sr-sl⋄l←/g⊏𝕩=5+⊑bB g⊏˜↩gs←⍋gr⊏sl⋄gr↩g⊏rev⋄gi←⍋g 𝕩⊏˜↩g⋄r⊏˜↩gr⋄ps⊏˜↩gr⋄si←/gr⊏sr>sl⋄l⊏↩⍋gs a←𝕩 M bG⋄ro←(r≥2)∨a<«r=3 tr←(IT»ps)⊏fe←ro∨r=1⋄ma←tr<(𝕩=1+⊑bG)∧«fe os←⌽↕∘≠⊸(⊣-T)⌽¬ro∨ma⋄at←1+⊏⟜os⊸+/a r(×⟜¬-⊢)↩aa←g⊏asn←»+`gi⊏«⊸-(≠𝕩)↑/⁼at ta←tr>(a≤○I1T ps)∧(⊢∧2(|<≤)ps(⊢-T)+`)¬ro oa←⌽/op←(«r≥0)∧r≥2 fa←/(ta∨(ro∧«op)∨«aa)<(r=1)∨<⟜»op dy←fa⊏«(𝕩≠⊑bO)∧(tr∧r≥0)∨ro