From 8403d48cf5823f631c2a489c68c2e9cea3eba23a Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 17 Nov 2020 14:20:36 -0500 Subject: Move index variable out of _err_, allowing it to be reused --- src/c.bqn | 67 +++++++++++++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/c.bqn b/src/c.bqn index 1d317faf..1c1bcff8 100644 --- a/src/c.bqn +++ b/src/c.bqn @@ -32,6 +32,10 @@ vi←⊑bD # Start of identifier numbering charRole←∾⥊¨˜⟜(≠↑cgl˙)⟨1,2,3,¯1,¯3,¯1‿0,¯2,0,5/⌽↕2⟩ # For first vi chars T←⌈`× ⋄ IT←↕∘≠⊸T ⋄ I1T←(1+↕∘≠)⊸T +# Error reporting for mask indicating error positions +# 𝔽 is the message and 𝔾 returns indices corresponding to the mask +_err_←{(0!˜∧∘/⟜𝔾≍○<𝔽)⍟(∨´∘⊢)} + # Source to ⟨tokens, roles, number of identifiers, literals⟩ # Identifiers then literal tokens are numbered starting at vi Tokenize←{ @@ -79,15 +83,14 @@ Tokenize←{ # 𝕩 is a list of tokens that contains the numeric literals, each # preceded by 0. Return the numbers. ReadNums←{ - ind←𝕨 ⋄ _err_←{(0!˜/⟜𝔾⟜ind≍○<𝔽)⍟(∨´∘⊢)} EChars←⟨"Letter"⊸Pl," """,⊏⟜charSet,""" not allowed in numbers"⟩_tmpl e‿p‿i‿n‿d←=⟜𝕩¨((⊑bA)+-´"ea")∾+⟜↕´bN # Masks for eπ∞¯. - EChars∘(/⟜𝕩)_err_⊢ ¬e∨𝕩<⊑bA + EChars∘(/⟜𝕩)_err_𝕨 ¬e∨𝕩<⊑bA s←d∨c←e∨z←0=𝕩⋄m←¬n∨c - "Negative sign in the middle of a number"_err_⊢ n>»c - "Portion of a number is empty"_err_⊢ (1«s)∧n∨s - "Ill-formed decimal or exponent use"_err_(s⊸/) ¬(0⊸=∨»⊸<)s/𝕩 - "π and ∞ must occur alone"_err_⊢ (p∨i)>1(»∧(p∧«e)∨«)z∨n + "Negative sign in the middle of a number"_err_𝕨 n>»c + "Portion of a number is empty"_err_𝕨 (1«s)∧n∨s + "Ill-formed decimal or exponent use"_err_(s/𝕎) ¬(0⊸=∨»⊸<)s/𝕩 + "π and ∞ must occur alone"_err_𝕨 (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 @@ -99,35 +102,35 @@ ReadNums←{ } -Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn +Parse ← {r‿vn‿i‿e←𝕨⋄nv←≠vn ErrMismatchedBrackets←{ Lcs ← ¯1 ⊑ 0¨∘⊢ {𝕩⌈⌈`𝕨+»𝕩}˝ =⌜⟜⌽ _mis_ ← {"Missing "∾𝕗∾" "∾charSet⊏˜𝕘+⊢/˜≠∘⊣=⊒} Msg ← >○≠◶⟨"opening"_mis_ 0, "closing"_mis_ 1˜⟩ 0 !˜ 𝕨 (Lcs<⌊○≠)◶⟨Msg,"Malformed bracket nesting"⟩ 𝕩 } + GI←{𝕤⋄g⊏i} - g←⍋pd←+`p←(¯1-2×r)×𝕩 M bB⋄gp←g⊏p⋄gb←g⊏r=¯1⋄gx←g⊏𝕩 - gp(>⟜0⊸/ErrMismatchedBrackets⍟≢1-˜<⟜0⊸/)gx - _err_←{(0!˜∧∘/⟜𝔾⟜ind≍○<𝔽)⍟(∨´∘⊢)} + g←⍋pd←+`p←(¯1-2×r)×𝕩 M bB⋄gb←g⊏r=¯1⋄gx←g⊏𝕩 + (g⊏p)(>⟜0⊸/ErrMismatchedBrackets⍟≢1-˜<⟜0⊸/)gx "Swapped open and closed brackets" ! 0≤(⊑g)⊑pd - "Parentheses can't contain separators"_err_(gb/g⊸⊏) ((⊑bB)⊸=»⊸∧sep⊸=)gb/gx - {"Empty statement or expression"_err_(𝕩/0∾g⊸⊏) (4+⊑bB)≠𝕩/0∾gx}1⊸«⊸∧1∾gb + "Parentheses can't contain separators"_err_(gb/GI) ((⊑bB)⊸=»⊸∧sep⊸=)gb/gx + {"Empty statement or expression"_err_(𝕩/0∾GI) (4+⊑bB)≠𝕩/0∾gx}1⊸«⊸∧1∾gb r-↩(𝕩=⊑bG)>ec←«0≤r+p - "Invalid assignment or stranding use"_err_⊢ ((¯4⊸<∧≤⟜¯2)r)>ec∧»0≤r + "Invalid assignment or stranding use"_err_(I) ((¯4⊸<∧≤⟜¯2)r)>ec∧»0≤r sr←»⌾(g⊸⊏)sl←«⊸∨r=¯2⋄cp←𝕩=1+⊑bB g⊏˜↩⍋g⊏sl rev←⍋+`¯1↓(¯1∾g)(⊣⍋⊸⊏⊏˜⟜⍋¬⊏˜)⍋+`⊸+1∾g⊏sl∨r=¯1 gf←⍋fd←+`rev⊏br←p×𝕩M⟨2+⊑bB,2⟩ rev⊏˜↩gf⋄fd⊏˜↩gf - 𝕩⊏˜↩rev⋄ind⊏˜↩rev⋄bv←rev⊏br⋄b←/bv<0⋄fi←+`c←bv>0 + 𝕩⊏˜↩rev⋄i⊏˜↩rev⋄bv←rev⊏br⋄b←/bv<0⋄fi←+`c←bv>0 H←¬1(∾/∾˜)c(∨/⊣)= sn←(0‿3‿4‿5+5+⊑bI)⍋𝕩⋄fx←1 H sn⋄fr←(fx∨0⊸<)⊸+ft←(2⊸H⌈2×3⊸H)sn - {⟨ind/˜(0=fi)∧𝕩 M bI,"Special name outside of any block"⟩!0}⍟(0<⊑fr) 𝕩 + {⟨i/˜(0=fi)∧𝕩 M bI,"Special name outside of any block"⟩!0}⍟(0<⊑fr) 𝕩 nf←(⊑bG)H𝕩 - {⟨ind/˜(fi⊏0≠fr)∧𝕩=⊑bG,"Can't export from a non-immediate block"⟩!0}⍟(∨´nf>fr=0) 𝕩 + {⟨i/˜(fi⊏0≠fr)∧𝕩=⊑bG,"Can't export from a non-immediate block"⟩!0}⍟(∨´nf>fr=0) 𝕩 fsc←(ft⊏0‿2‿3)+3×fx pt←cp∧ns←¬sl∨sr r↩sl-˜ns×(1↓fr)⌾((b⊏rev)⊸⊏)r @@ -139,23 +142,23 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn idx←𝕩⊏˜id←/idm←(0⊸≤∧<⟜nv)xv←𝕩-vi⋄sp←/𝕩 M bI 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⊏˜↩gs←⍋gr⊏sl⋄gr↩g⊏rev⋄ginv←⍋g 𝕩⊏˜↩g⋄r⊏˜↩gr⋄ir⊏˜↩gr⋄l↩(l⊏⍋gs)∾/gr⊏sr>sl - ni←1+(1↓nf)/bk←b⊏gi - "Modules must be immediately assigned"_err_(ni⊏g⊸⊏)∘(∨˝¯3‿0‿¯1≠r∾⟜¯1‿1⊸(⊏˜)(↕3)+⌜⊢)⍟(0<≠)ni - "Modules must be destructured"_err_(ni⊏g⊸⊏) (⊏⟜idm>⊏⟜rev⊏sr˙)(ni+1)⊏g + ni←1+(1↓nf)/bk←b⊏ginv + "Modules must be immediately assigned"_err_(ni⊏GI)∘(∨˝¯3‿0‿¯1≠r∾⟜¯1‿1⊸(⊏˜)(↕3)+⌜⊢)⍟(0<≠)ni + "Modules must be destructured"_err_(ni⊏GI) (⊏⟜idm>⊏⟜rev⊏sr˙)(ni+1)⊏g a←r≤¯3⋄ps←a(«∧m2≤»)m2(«∧m2≤»)m2aa + "Function or modifier assignment to a non-name"_err_(at⊏GI) ¬(ar=0)∨at⊏aid + aa←×g⊏ac←»+`ginv⊏«⊸-(1+af+(ai∊ni)+2×(⊑bG)-˜ai⊏𝕩)⌾(at⊸⊏)0¨𝕩 + "Assignment target must be a name or list of targets"_err_(GI) (aid∨ps∨𝕩∊bB⊑⊸+1‿5)aa ip←id⊏ac⋄d←ip M 2‿4⋄zic←0¨ic←id/˜icf←2(<∧|)ip il←ic⊏+`bv<0⋄ex←il⊏(/∾≠)c @@ -165,7 +168,7 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn "Multiple definitions" ! ∧´ixx∨○(»⊸≠((≠d)⊸≤⊸/ig)⊸⊏)ixf ig↩<⟜(≠d)⊸/(⍋+`∘⊏⟜(d∾-/˜d))⊸⊏ig d⊏˜↩ig⋄idx⊏˜↩ig - ("Undefined identifier"Pl/)_err_(ig⊏id⊸⊏) d<»⊸≠idx + ("Undefined identifier"Pl/)_err_(ig⊏id⊏I) d<»⊸≠idx "Imports must have been exported" ! ∧´2>-⟜»(d∨×)⊸/ig⊏(ip M 1‿3)∾2¨ic ui←(IT d)⊸⊏⌾(ig⊸⊏)id∾ex lc←(1«c)/dn←c(⊢-T)+`idm∧ac M 2‿4 @@ -177,7 +180,7 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn ta←tr∧2(>∨|)ps(⊢-T)+`¬ro fa←/(fe←ta∨ro∨«⊸∨psfe∨ff + "Second-level parts of a train must be functions"_err_(GI) tr>fe∨ff dy←fa⊏«(𝕩=⊑bO)<(tr∧r≥0)∨roblk, indb, tx⟩ } -- cgit v1.2.3