aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/c.bqn132
-rwxr-xr-xsrc/cjs.bqn15
-rwxr-xr-xsrc/pr.bqn47
3 files changed, 194 insertions, 0 deletions
diff --git a/src/c.bqn b/src/c.bqn
new file mode 100644
index 00000000..eed5f843
--- /dev/null
+++ b/src/c.bqn
@@ -0,0 +1,132 @@
+#! ./dzref
+
+tab←' '⋄nl←'
+'
+dig ← "0123456789"
+alph ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+alow ← "abcdefghijklmnopqrstuvwxyz"
+charSet←∾charGroups←⟨
+ chF←"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!" # Function
+ "˜˘¨⌜⁼´˝`" # Modifier
+ "∘○⊸⟜⌾⊘◶⎉⚇⍟" # Composition
+ nl∾"⋄," # Separator
+ "←↩→" # Gets
+ "(){}⟨⟩" # Bracket
+ "‿" # Ligature
+ "·" # nOthing
+ "𝕊𝕏𝕎𝔽𝔾𝕤𝕩𝕨𝕗𝕘" # Input (𝕣 pending; ℝ not allowed)
+ dig # Digit
+ "¯.π∞" # Numeric
+ alow∾alph∾"_" # Alphabetic
+ " "∾tab # Whitespace
+# #'" eliminated during tokenization
+⟩
+bF‿bM‿bC‿bS‿bG‿bB‿bL‿bO‿bI‿bD‿bN‿bA‿bW←⊔/≠¨charGroups
+vi←+´≠¨9↑charGroups
+charRole←((vi-≠bI)↑/0∾≠¨3↑charGroups)∾(5/⌽↕2)∾0
+spc←⥊3‿5‿6-⌜3‿0
+
+Tokenize←{
+ r←𝕩='#'⋄s←/(≠↑2⊸↓)⊸∧𝕩='''⋄d←/𝕩='"'
+ g←⍋q←∾⟨ s⋄¯1↓d⋄/r⟩ ⋄q↩g⊏q
+ e← g⊏∾⟨2+s⋄ 1↓d⋄(⊢-¯1↓0∾⊢)∘⊏⟜(0∾+`r)⊸//(𝕩=nl)∾1⟩
+ Se←{(⊏˜𝕨)Se 1¨⌾((𝕩/𝕨)⊸⊏)𝕩}⍟{0=⊑⌽𝕩}
+ st←¯1↓Se⟜(1↑˜≠)∾⟜≠q⍋e⋄b←st/q∾˘e
+ ToI←¯1↓·/⁼(≠𝕩)∾˜⥊⋄f←¬≠`ToI b
+ cb←(¬(st/q)⊏r)/b
+ lu←⍷lit←𝕩⊔˜1-˜(+`ToI⊑˘cb)×≠`ToI cb
+ cl←f/ToI⊑∘⌽˘cb
+
+ c←charSet⊐f/𝕩
+ w←(≠↑0∾⊢)⊸<l←c∊∾bD‿bN‿bA
+ i←(1-˜l×+`w)⊔c
+ na←≠alph⋄us←¯1⊑bA
+ in←na(⊢-⊣×+⟜(⊑bA)⊸≤)us⊸≠⊸/¨i
+ id←⍷(bA∊˜w/c)⍒⊸⊏in⋄nv←+´bA∊˜⊑¨id
+
+ c↩(w∨¬l∨c∊bW)/(vi+id⊐in)⌾(w⊸/)(vi+(≠id)+lu⊐lit)⌾(cl⊸/)c
+ c/˜↩¬(≠↑1∾(c∊2‿4⊏bB)∨⊢)⊸∧c∊bS⋄c/˜↩¬(1↓1∾˜c∊3‿5⊏bB)∧c∊bS
+ ti←(us=¯1⊸⊑¨i)(⊢+∧⟜(2⊸=))0⌈na⌊∘÷˜(⊑bA)-˜⊑¨i
+ ic←(0⊸≤∧<⟜(≠id))c-vi⋄t←ti⌾(ic⊸/)(vi⌊c)⊏charRole
+ c+↩5×c∊5↑bI
+ ⟨c,t,nv,(nv↓id)∾lu⟩
+}
+
+ReadNum←{
+ n‿d‿p‿i←bN # ¯.π∞
+ Nat←+⟜(10⊸×)´∘⌽ -⟜(⊑bD)
+ Int←(n=⊑)◶⟨Nat,-·Nat 1⊸↓⟩
+ Dec←⊐⟜(<d)⊸(Nat∘↑ + ·(0<≠)◶⟨0,Nat÷10⋆≠⟩+⟜1⊸↓)
+ Pos←(⊑p‿i⊐⊏)◶⟨π,∞,Dec⟩
+ e←⊑𝕩⊐charSet⊐<'e'
+ m←(n=⊑)◶⟨Pos,-·Pos 1⊸↓⟩e↑𝕩
+ 𝕩×⟜(10⋆·Int(e+1)↓⊣)˜⍟(e<≠𝕩)m
+}
+LitVal←(⊑("'"∾'"')⊐⊏)◶⟨1⊸⊑,1⊸↓,ReadNum⟩
+
+
+LEBv ← {
+ b←128
+ l←⌊b⋆⁼1⌈𝕩
+ o←⍋⍋⊒/1+l
+ o⊏l{f←×𝕨⋄(𝕨-1)(b⊸(×⟜f+|)∾𝕊⟜(⌊÷⟜b)○(f⊸/))⍟(∨´f)𝕩}𝕩
+}
+
+Parse ← {nv‿r←𝕨
+ g←⍋+`p←bB(⊣(≠⊸>ׯ1⋆2|⊢)⊐)𝕩⋄br←p×𝕩∊2‿3⊏bB
+ sl←1⊸⌽⊸∨𝕩∊bL⋄sr←¯1⊸⌽⌾(g⊸⊏)sl⋄sa←sl∨sr
+ g⊏˜↩⍋g⊏sl⋄r×↩¬sa⋄o←p>0
+ r+↩(sa<𝕩=1⊑bB)(⊣∧¬⊸∨=○(⌈`(1+↕∘≠)⊸×)⊢)(¯1⊸⌽⌾(g⊸⊏)3=r)∨1≤r
+ rev←⍋+`1+¯1↓g((¯1∾⊣)(⊣⍋⊸⊏⊏˜-⊏˜⟜⍋)⟜⍋1(+`∘∾-∾˜)⊏)o∨sl∨𝕩∊bS
+
+ gf←⍋fd←+`rev⊏br
+ rev⊏˜↩gf⋄fd⊏˜↩gf
+ 𝕩⊏˜↩rev⋄bv←rev⊏br⋄b←/bv<0⋄fi←+`c←bv>0
+
+ FC←-⟜(≠↑0∾⊢)(c∾1)/·+`∾⟜0
+ H←0<1↓·FC𝕩∊(5⊑bI)+⊢
+ ft←0∾(H↕3)+2×(H⥊3)⌈2×H⥊4
+ r↩((1↓ft)⊏(1+2⊸≤)⊸/↕4)⌾(b⊸⊏)rev⊏r
+
+ id←/idm←(0⊸≤∧<⟜nv)xv←𝕩-vi⋄idx←id⊏𝕩⋄sp←/𝕩∊bI
+ ad←(⌈`↕∘≠⊸×𝕩∊bG)⊏𝕩=⊑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⋄𝕩⊏˜↩g⋄r⊏˜↩g⋄o⊏˜↩gr⋄si←/gr⊏sr>sl
+ gi←⍋g⋄l⊏↩⍋gs
+ s←𝕩∊bS⋄ps←s∨o∨gr⊏sl⋄a←𝕩∊2↑bG
+ r-↩ps∨a⋄op←r≥2⋄fe←(r≥1)∨1⌽r=3
+ tr←(⌈`↕∘≠⊸ׯ1⌽ps)⊏fe
+ ma←tr<(𝕩=1⊑bG)∧1⌽fe
+ ro←op∨a<1⌽r=3⋄os←⌽↕∘≠⊸(⊣-⌈`∘×)⌽¬ro∨ma⋄at←(⊢+1+⊏⟜os)/a
+ aa←g⊏asn←¯1⌽+`gi⊏-⟜(≠↑0∾⊢)(↕≠𝕩)∊at-1⋄r↩¯1¨⌾(aa⊸/)r
+ tf←(a≤○(⌈`(1+↕∘≠)⊸×)ps)∧(⊢∧2(|<≤)ps(⊢-⌈`∘×)+`)¬ro
+ opa←op>1↓0∾˜ps∨a
+ oa←⌽/opa⋄fa←/(1⌽aa)<(tf∨¬tr)∧(ro∧1⌽opa)<(r=1)∨op<¯1⌽opa
+ dy←fa⊏1↓0∾˜(𝕩≠⊑bO)∧(tr∧r≥0)∨ro<r=0
+ n←𝕩≥vi+nv⋄cn←/n∨𝕩≤¯1⊑bC⋄u←∧⍷ob←cn⊏𝕩
+ lo←(o/𝕩)=4⊑bB⋄ll←1+lo/1(↓--⊸↓)(o∾1)/+`(s∾0)-(1∾o)∧ps∾1
+ dr←/s>o+`⊸⊏0∾lo⋄rt←/𝕩=2⊑bB
+
+ d←id⊏dec←idm∧ad∧asn⋄lc←FC dec
+ ig←⍋(⊏⟜(¯1∾c/gf)∾d⊸/⊏((≠𝕩)∾b⊏gf)˜)id⊏fi
+ ig↩<⟜(≠id)⊸/(⍋+`∘⊏⟜(d∾-/˜d))⊸⊏(⍋⊏⟜(∾⟜(d⊸/)idx))⊸⊏ig
+ ui←d(⌈`↕∘≠⊸×)⊸⊏⌾(ig⊸⊏)id
+ ii←(ui⊏(fi⊏ft⊏spc)-(⌈`c⊸×)⊸¬+`dec)∾((sp⊏fi)⊏3×2|ft)++⟜(0⊸≤)2+sp⊏xv
+ idor←∾3⥊<is←id∾sp
+ idbc←⟨21+is⊏asn,(id-○(⊏⟜fd)ui)∾0¨sp,ii⟩
+
+ b⊏↩gi
+ or←⍋idor∾g⊏˜∾⟨cn,cn,b,b,rt,dr,l,l,si,si,at,oa+1⌈oa⊏os,(dy×⊏⟜os)⊸+fa+dy⟩
+ bc0←∾idbc∾⟨0¨cn,u⊐ob,15¨b,1+↕≠b⟩
+ bc←or⊏bc0∾∾⟨25¨rt,14¨dr,3+l⊏aa,ll,3+si⊏aa,sll,(11-⊑bG)+a/ma+𝕩,5+oa⊏r,(⊢+9×10⊸=)16+dy-7×fa⊏tr⟩
+ fs←(ft⊏⥊3≍⌜⟜⌽○↕2)∾¨((+`0∾1+⌊128⋆⁼1⌈bc)⊏˜/1∾or∊bc0+⟜↕○≠rt)≍○<¨lc+ft⊏spc
+ ⟨LEBv bc∾25,u,fs⟩
+}
+
+Compile←{
+ ⟨t,r,nVar,lits⟩←Tokenize𝕩
+ ⟨bc,u,blk⟩←nVar‿r Parse t
+ o←(u-(vi+nVar-≠𝕨)×u≥vi)⊏𝕨∾LitVal¨lits
+ ⟨bc,o,blk⟩
+}
diff --git a/src/cjs.bqn b/src/cjs.bqn
new file mode 100755
index 00000000..cb60b7df
--- /dev/null
+++ b/src/cjs.bqn
@@ -0,0 +1,15 @@
+#! /usr/bin/env dbqn
+
+compile ← ⟨•path∾"../c.bqn"⋄"DGenFn"⟩ •EX •path∾"../dzref"
+Symcomp ← ((<"runtime["∾⍕∾"]"˜)¨↕59)⊸Compile
+FP ← ∞⊸=◶⟨⍕,"Infinity"⟩
+Str ← "str("""∾∾⟜""")"
+Char ← "'"(∾∾⊣)(⟨"\t","\n","\'"⟩∾<)⊑˜("'"∾˜•UCS 9‿10)⊑∘⊐⊢
+Fmt ← ≡◶⟨(⊑⍋)∘(∾⟜∞)◶⟨0⊸≤◶⟨"-"∾FP∘|,FP⟩,Char⟩⋄Str⋄⊑⟩
+L ← "["∾"]"∾˜1↓·∾","⊸∾⟜⍕¨
+G ← ⟨L∾⟜25,L Fmt¨,L⟨⊑"fmd"⊸⊐,⊢,⊢,≠⟩⊸(L{𝕎𝕩}¨)¨⟩ {𝕎𝕩}¨ 0‿1‿3 ⊏ Symcomp
+Gen ← L G
+GenLong ← ∾ (1‿2/⟨" new Uint8Array("∾∾⟜")"⋄((•UCS 10)∾" ,")⊸∾⟩) {𝕎𝕩}¨ G
+• ← ∾∾⟜(•UCS 10)¨ Gen¨ •args
+#• ← GenLong •EX •path∾"pr.bqn"
+#• ← GenLong ∾∾⟜(•UCS 10)¨ (•LNS •path∾"c.bqn")∾<"Compile"
diff --git a/src/pr.bqn b/src/pr.bqn
new file mode 100755
index 00000000..d68b3453
--- /dev/null
+++ b/src/pr.bqn
@@ -0,0 +1,47 @@
+#!/usr/bin/env dbqn
+
+# Process BQN runtime
+
+impl ← •LNS •path∾"../impl.bqn"
+
+chrs←⟨
+ "+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!"
+ "˜˘¨⌜⁼´˝`"
+ "∘○⊸⟜⌾⊘◶⎉⚇⍟"
+⟩
+nc ← ≠¨chrs
+chr ← ∾chrs
+itr ← 0⥊˜≠chr
+
+init ← " "⊸∾¨(/⟜"_"¨nc/0‿1‿1)∾¨(nc/"FMD")∾¨(nc+´⊸↑⥊"ABC"∾⌜•a)
+post ← ∾⟜" "¨/⟜"_"¨nc/0‿0‿1
+names ← init∾¨(•UCS 48)∾¨post
+
+Inc ← {
+ i←⊑chr⊐𝕩
+ n←0 ⋄ itr↩{n↩1+𝕩}⌾(i⊑⊢)itr
+ names↩((i⊑init)∾(•UCS 48+n)∾i⊑post)⌾(i⊑⊢)names
+}
+
+# Required functionality passed in as an argument
+pnam ← ⟨"IsArray", "Type", "Log"⟩∾names⊏˜chr⊐"!+-×÷⋆⌊=≤≢⥊⊑↕⌜`"
+
+
+E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩
+E_proc ← {
+ l←≠chr
+ q←≠`𝕩∊"""'" ⋄ f←¬∨`q¬⊸∧𝕩='#'
+ ∾ (((l×f/q)+chr⊸⊐) (≥⟜l)◶⟨⊑⟜names,⥊∘⊢⟩¨ ⊢) f/𝕩
+}
+E_redef ← { # handles [fmd] [←↩]
+ tail ← E_proc 3↓𝕩 # must use old def
+ Inc ⊑𝕩
+ (E_proc 1↑𝕩) ∾ "←" ∾ tail
+}
+
+ref ← ∾ ∾⟜(•UCS 10)¨ ∾⟨
+ ⟨"{"⟩
+ ⟨"←𝕩"∾˜"⟨"∾"⟩"∾˜1↓∾","⊸∾¨pnam⟩
+ E_isdef◶E_proc‿E_redef¨ impl∾<"⟨"∾"⟩"∾˜1↓⥊","⊸∾˘∾chrs
+ ⟨"}"⟩
+⟩