From 5a477b9cfe4653cd33a67ac5004eeb3565018e25 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 4 May 2021 20:42:32 -0400 Subject: Support namespace creation in the compiler --- dc.bqn | 7 ++++--- src/c.bqn | 36 +++++++++++++++++++----------------- src/cjs.bqn | 2 +- test/cases/namespace.bqn | 13 +++++++++++++ test/cases/syntax.bqn | 8 -------- 5 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 test/cases/namespace.bqn diff --git a/dc.bqn b/dc.bqn index 6c286e82..60e8fd55 100755 --- a/dc.bqn +++ b/dc.bqn @@ -9,11 +9,12 @@ compile ← glyphs •Import "src/c.bqn" special←<˘∘‿2⥊"𝕤𝕩𝕨𝕣𝕗𝕘" DCompile⇐{ - ⟨bc,o,fblk,ind⟩ ← 4 ↑ (𝕨 •BQN∘⥊¨∘∾⊘⊣ glyphs) Compile 𝕩 + ⟨bc,o,fblk,ind,tok⟩ ← 5 ↑ (𝕨 •BQN∘⥊¨∘∾⊘⊣ glyphs) Compile 𝕩 + name ← ⊑2⊑tok blk‿bdy ← <˘⍉>(↕≠fblk){ - ⟨t,i,l,n⟩←𝕩 + ⟨t,i,l,n,d,e⟩←𝕩 s←(3×i)↓(t⊑3‿5‿6)↑special - ⟨⟨t,i,𝕨,𝕨⟩ ⋄ ⟨l, s∾⥊¨'a'+↕n-≠s⟩⟩ + ⟨⟨t,i,𝕨,𝕨⟩ ⋄ ⟨l, s∾d⊏name, (0¨s)∾e⟩⟩ }¨fblk ⟨bc,o,blk,⊑blk,bdy,⊑ind,1⊑ind,𝕩⟩ } diff --git a/src/c.bqn b/src/c.bqn index b5f3772e..17c12df2 100644 --- a/src/c.bqn +++ b/src/c.bqn @@ -67,12 +67,12 @@ Tokenize←{ ir←(us/˜«⊸us)×+`w>n # Identifier groups {⟨is⊏˜𝕩/𝕨,"Numbers can't start with underscores"⟩!0}⍟(∨´⊢)⟜(ws<(⊑bA)>⊏⟜t)/(¯1»⌈`)⊸¯1»⌈`)⊸/¨˜↩j←⊐¨k # IDs j into uniques k - k↩(𝕎⊏⟜charSet¨)⌾(1⊸⊑)k # System value lookup + k↩𝕎⌾(1⊸⊑)k # System value lookup wf←¬l∨t M bW⋄ie/˜↩wf∨>⟜«l # Index management for... t↩(w∨wf)Fs(∾j++`vi»kk←≠¨k)⌾(ki⊸⊏)t # Add IDs; remove words/whitespace t-↩t(M×-⟜⊑)bS # Separators are equivalent @@ -144,8 +144,7 @@ Parse ← {r‿vn‿i‿e←𝕨⋄nv←≠vn sn←(0‿3‿4‿5+5+⊑bI)⍋𝕩⋄sp←/𝕩 M bI # Special name index fx←1 H sn⋄fr←(fx∨0⊸<)⊸+ft←(2⊸H⌈2×3⊸H)sn # Block immediacy ¬fx and type ft "Special name outside of any block"_err_(/{(0=fi)∧𝕩 M bI}∘𝕩) 0<⊑fr - nf←(⊑bG)H𝕩 # Block has exports (is namespace) - "Can't export from a non-immediate block"_err_(/{(fi⊏0≠fr)∧𝕩=⊑bG}∘𝕩) ∨´nf>fr=0 + nf←(⊑bG)H𝕩 # Namespace blocks fsc←(ft⊏0‿2‿3)+3×fx # Special name count # Propagate roles through parentheses @@ -159,7 +158,7 @@ Parse ← {r‿vn‿i‿e←𝕨⋄nv←≠vn ir(×⟜¬-⊢)↩nr # Include nothingness (doesn't handle 𝕎) # Prep for lexical resolution before reordering 𝕩 - idx←𝕩⊏˜id←/idm←(0⊸≤∧<⟜nv)xv←𝕩-vi + idv←𝕩⊏˜id←/idm←(0⊸≤∧<⟜nv)xv←𝕩-vi # Parsing part 1 p-↩br⋄g↩⍋+`p⋄gr←g⊏rev # Order by non-brace bracket depth @@ -168,9 +167,10 @@ Parse ← {r‿vn‿i‿e←𝕨⋄nv←≠vn {i↩(𝕨⊏i)⌾(𝕩⊸⊏)i⋄e↩(𝕩⊏e)⌾(𝕨⊸⊏)e}´bp # Highlight all contents of a pair when error reporting g⊏˜↩gs←⍋gr⊏sl⋄gr↩g⊏rev⋄gi←⍋g # Send strand prefixes *‿ to the end 𝕩⊏˜↩g⋄r⊏˜↩gr⋄ir⊏˜↩gr⋄l↩(l⊏⍋gs)∾/gr⊏sr>sl - ni←1+(1↓nf)/bk←c⊏gi # Indices of module assignment (import) arrows - "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+nf/¯1∾bk←c⊏gi # Indices of import arrows + nm←(nf/fr=0)∧(¯3=ni⊏r)∧0=(ni+1)⊏r # Namespace is module: immediately assigned, + nm∧↩(⊏⟜idm≤⊏⟜rev⊏sr˙)(ni+1)⊏g # ...and destructured + nf∧↩nf+`⊸⊏0∾¬nm # Don't treat module as namespace a←r≤¯3⋄ps←aaa # Lexical resolution (independent of parsing part 2 below) - ip←id⊏ac⋄d←ip M 2‿4 # Assignment classes; which are definitions + ip←id⊏ac⋄d←ip M 2‿4⋄xm←ip M 1‿3 # Assignment classes; which are definitions, exports + idn←(idg←(d/id⊏+`b)∾≠fsc)⊔vi-˜d/idv # Numbers for each identifier, per-block zic←0¨ic←id/˜icf←2(<∧|)ip⋄II←∾⟜(icf⊸/)∘id # Imports have class 3, 5, or 7 il←ic⊏+`br<0⋄ex←il⊏(/∾≠)b # Block for each import and its end index # Order every referenced identifier, an extraction for each import, and an undeclaration for each declaration ixf←(((1=ip)+⊏⟜(¯1∾b/gf))∾(il∾d⊸/)⊏((≠𝕩)∾c⊏gf)˜)id⊏fi # First order by block index, open for real and closed for virtual - d∾↩zic⋄idx∾↩icf/idx # Add in imports - ig←(⍋⊏⟜(ixx←∾⟜(d⊸/)idx))⊸⊏⍋ixf # Then order by name + d∾↩zic⋄idv∾↩icf/idv # Add in imports + ig←(⍋⊏⟜(ixx←∾⟜(d⊸/)idv))⊸⊏⍋ixf # Then order by name {"Redefinition"_err_(𝕩⊏·∾⟜(d⊸/)II) ¬ixx∨○(»⊸≠𝕩⊸⊏)ixf} (≠d)⊸≤⊸/ig ig↩<⟜(≠d)⊸/(⍋+`∘⊏⟜(d∾-/˜d))⊸⊏ig # Last order by declaration depth - d⊏˜↩ig⋄idx⊏˜↩ig⋄II↩ig⊏II - ("Undefined identifier"Pl/)_err_(II) d<»⊸≠idx - {"Imports must have been exported"_err_(f/II) 2≤-⟜»(f←d∨×𝕩)/𝕩} ig⊏(ip M 1‿3)∾2¨ic + d⊏˜↩ig⋄idv⊏˜↩ig⋄II↩ig⊏II + ("Undefined identifier"Pl/)_err_(II) d<»⊸≠idv + idx←idg⊔(⍋d/ig)⊏d(/≥1↓PN)0