From 357a9ab4e25a0101121761c503e14ba38d32093c Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 17 Jul 2020 07:28:21 -0400 Subject: Move some files around --- README.md | 2 +- c.bqn | 160 +++++++++++++++++++++++++++++++---------------- cshim.bqn | 3 + dc.bqn | 177 ---------------------------------------------------- dcshim.bqn | 3 - doc/gen | 2 +- doc/md.bqn | 2 +- dzref | 205 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ nc.bqn | 124 +++++++++++++++++++++++++++++++++++++ running.md | 2 +- spec/dzref | 205 ------------------------------------------------------------- test/bt | 2 +- test/dt.js | 26 -------- test/nt.js | 19 ++++++ test/t.js | 29 +++++---- 15 files changed, 482 insertions(+), 479 deletions(-) create mode 100755 cshim.bqn delete mode 100755 dc.bqn delete mode 100755 dcshim.bqn create mode 100755 dzref create mode 100755 nc.bqn delete mode 100755 spec/dzref delete mode 100755 test/dt.js create mode 100755 test/nt.js diff --git a/README.md b/README.md index 9e1064b5..a9021a67 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ Rather strange, most likely: ⊑+`∘⌽⍟12↕2 # The 12th Fibonacci number -For longer samples, you can [gaze into the abyss](dc.bqn) that is the (incomplete) self-hosted compiler, or take a look at [some translations](examples/fifty.bqn) from ["A History of APL in 50 Functions"](https://www.jsoftware.com/papers/50/). While it's certainly possible to write BQN in a more spread-out and better commented style, nobody has done that to date. +For longer samples, you can [gaze into the abyss](c.bqn) that is the (incomplete) self-hosted compiler, or take a look at [some translations](examples/fifty.bqn) from ["A History of APL in 50 Functions"](https://www.jsoftware.com/papers/50/). While it's certainly possible to write BQN in a more spread-out and better commented style, nobody has done that to date. ## Array model diff --git a/c.bqn b/c.bqn index 8125d3f6..13252655 100755 --- a/c.bqn +++ b/c.bqn @@ -1,71 +1,85 @@ -#!/usr/bin/env bqn +#! ./dzref -reduce←{r←𝕩⋄{(>𝔽¨_r)⍠(><⊸(𝔽¨_r))}}⊑⟨´⟩ -´↩_reduce -⊔↩((↕1+⌈´)=¨<)∘⊣ /¨⟜< ↕∘≠⍠⊢ -⍷↩∪ - -Base←{+⟜(𝕨⊸×)´⌽𝕩} -Enc2←{𝕨⥊2(|∾˜(𝕨-1)Enc2⌊∘÷˜)⍟(0<𝕨)𝕩} - -lf‿tab←•UCS 10‿9 +nl‿tab←•UCS 10‿9 charSet←∾charGroups←⟨ - chF←"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔" ⍝ Function - "˜˘¨⌜⁼´`" ⍝ Modifier - "∘○⊸⟜⌾⊘◶⎉⚇⍟" ⍝ Composition - "𝕨𝕎𝕩𝕏𝕗𝔽𝕘𝔾" ⍝ Input - lf∾"⋄," ⍝ Separator - "←↩→" ⍝ Gets - "(){}⟨⟩" ⍝ Bracket - "‿" ⍝ Ligature - •d ⍝ +⟜(↕10)⌾•UCS'0' ⍝ Digit - "¯.π∞" ⍝ Numeric - "_"∾' '(⊢∾+)⌾•UCS•a ⍝ Alphabetic - " "∾tab ⍝ Whitespace -⍝ #'" eliminated during tokenization + chF←"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!" # Function + "˜˘¨⌜⁼´`" # Modifier + "∘○⊸⟜⌾⊘◶⎉⚇⍟" # Composition + ¯1⊏˘10‿2⥊"𝕨𝕎𝕩𝕏𝕗𝔽𝕘𝔾𝕤𝕊" # Input (𝕣 pending; ℝ not allowed) + nl∾"⋄," # Separator + "←↩→" # Gets + "(){}⟨⟩" # Bracket + "‿" # Ligature + "·" # nOthing + •d # +⟜(↕10)⌾•UCS'0' # Digit + "¯.π∞" # Numeric + "_"∾˜' '(+∾⊢)⌾•UCS•a # Alphabetic + 1↑"𝕨" # Prefix for input (hack around UTF-16) + " "∾tab # Whitespace +# #'" eliminated during tokenization ⟩ -bF‿bM‿bC‿bI‿bS‿bG‿bB‿bL‿bD‿bN‿bA‿bW←⊔/≠¨charGroups -vi←+´≠¨8↑charGroups +bF‿bM‿bC‿bI‿bS‿bG‿bB‿bL‿bO‿bD‿bN‿bA‿bP‿bW←⊔/≠¨charGroups +vi←+´≠¨9↑charGroups +charRole←(vi+1)↑(/0∾≠¨3↑charGroups)∾10⥊↕2 Tokenize←{ - r←𝕩='#'⋄s←/(≠↑2⊸↓)⊸∧𝕩=⊑"'"⋄d←/𝕩='"' + r←𝕩='#'⋄s←/(≠↑2⊸↓)⊸∧𝕩='''⋄d←/𝕩='"' g←⍋q←∾⟨ s⋄¯1↓d⋄/r⟩ ⋄q↩g⊏q - e← g⊏∾⟨2+s⋄ 1↓d⋄(⊢-¯1↓0∾⊢)∘⊏⟜(0∾+`r)⊸//(𝕩=lf)∾1⟩ + 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←(≠𝕩)↑/⁼∘⥊⋄f←¬≠`ToI b + ToI←¯1↓·/⁼(≠𝕩)∾˜⥊⋄f←¬≠`ToI b cb←(¬(st/q)⊏r)/b - nl←≠lu←⍷lit←𝕩⊔˜1-˜(+`ToI⊑˘cb)×≠`ToI cb + lu←⍷lit←𝕩⊔˜1-˜(+`ToI⊑˘cb)×≠`ToI cb cl←f/ToI⊑∘⌽˘cb + args←charSet⊸⊐¨𝕨⋄c←charSet⊐f/𝕩 - w←(<´≠↕0∾⊢)l←c∊∾bD‿bN‿bA - id←⍷args∾(bA∊˜w/c)⍒⊸⊏i←(1-˜l×+`w)⊔c - nv←+´bA∊˜⊑¨id - c↩(w∨¬l∨c∊bW)/(vi+id⊐i)⌾(w⊸/)(vi+(≠id)+lu⊐lit)⌾(cl⊸/)c - c(/˜)↩¬(1⌾⊑1⌽1⌾⊑)⊸∧c∊bS - ⟨c,nv,nv-≠args,(nv↓id)∾lu⟩ + w←(≠↑0∾⊢)⊸ׯ1⋆2|⊢)⊐)𝕩 + 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 + g↩⍋+`rev⊏p⋄gr←g⊏rev⋄sll←1+2÷˜0(<-○/>)gr⊏sr-sl + g⊏˜↩⍋gr⊏sl⋄gr↩g⊏rev⋄𝕩⊏˜↩gr⋄r⊏˜↩gr⋄o⊏˜↩gr⋄si←/gr⊏sr>sl + s←𝕩∊bS⋄ps←s∨o∨gr⊏sl⋄a←𝕩∊2↑bG + r-↩ps∨a⋄op←r≥2⋄ro←op∨1⌽r=3 + tr←(⌈`↕∘≠⊸ׯ1⌽ps)⊏ro∨r≥1 + tf←(a≤○(⌈`(1+↕∘≠)⊸×)ps)∧(⊢∧2(|<≤)ps(⊢-⌈`∘×)+`)¬ro + opa←op>1↓0∾˜ps∨a⋄os←⌽↕∘≠⊸(⊣-⌈`∘×)⌽¬ro + oa←⌽/opa⋄fa←/(tf∨¬tr)∧(ro∧1⌽opa)<(r=1)∨op<¯1⌽opa + dy←fa⊏1↓0∾˜(𝕩≠⊑bO)∧(tr∧r≥0)∨r=0 + n←𝕩≥vi+nv⋄id←/n<𝕩≥vi⋄cn←/n∨𝕩≤¯1⊑bC⋄u←⍷ob←cn⊏𝕩 + lo←(o/𝕩)=4⊑bB⋄l←/𝕩=5⊑bB⋄ll←1+lo/1(↓--⊸↓)(o∾1)/+`(s∾0)-(1∾o)∧ps∾1 + dr←/s>o+`⊸⊏0∾lo + or←⍋g⊏˜∾⟨cn,cn,id,id,1+/a,dr,l,l,si,si,oa+1⌈oa⊏os,(dy×⊏⟜os)⊸+fa+dy⟩ + bc←or⊏∾⟨0¨cn,u⊐ob,1+(id-1)⊏a,vi-˜id⊏𝕩,(11-⊑bG)+a/𝕩 + 14¨dr,3¨l,ll,3¨si,sll,5+oa⊏r,5+dy+4×fa⊏tr⟩ + bc‿u +} + +prims←⟨ + +,-,×,÷,⋆,√,⌊,⌈,|,¬,∧,∨,<,>,≠,=,≤,≥,≡,≢,⊣,⊢,⥊,∾,≍,↑,↓,↕,⌽,⍉,/,⍋,⍒,⊏,⊑,⊐,⊒,∊,⍷,⊔,! + ˜,˘,¨,⌜,⁼,´,` + ∘,○,⊸,⟜,⌾,⊘,◶,⎉,⚇,⍟ +⟩ +DGenFn←{ + ⟨t,r,nVar,nLoc,lits⟩←⟨⟩Tokenize𝕩 + ⟨bc,u⟩←nVar‿r DParse t + o←(u-(vi+nVar-≠prims)×u≥vi)⊏prims∾LitVal¨lits + ⟨bc,o,nVar↑•a,⟨⟩⟩ +} +DRun←{ + 'a' •COMP DGenFn 𝕩 } diff --git a/cshim.bqn b/cshim.bqn new file mode 100755 index 00000000..18396541 --- /dev/null +++ b/cshim.bqn @@ -0,0 +1,3 @@ +#! /usr/bin/env dbqn + +((<•path∾"c.bqn")∾•args) •EX •path∾"dzref" diff --git a/dc.bqn b/dc.bqn deleted file mode 100755 index 2660f63d..00000000 --- a/dc.bqn +++ /dev/null @@ -1,177 +0,0 @@ -#! ./spec/dzref - -nl‿tab←•UCS 10‿9 -charSet←∾charGroups←⟨ - chF←"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔!" # Function - "˜˘¨⌜⁼´`" # Modifier - "∘○⊸⟜⌾⊘◶⎉⚇⍟" # Composition - ¯1⊏˘10‿2⥊"𝕨𝕎𝕩𝕏𝕗𝔽𝕘𝔾𝕤𝕊" # Input (𝕣 pending; ℝ not allowed) - nl∾"⋄," # Separator - "←↩→" # Gets - "(){}⟨⟩" # Bracket - "‿" # Ligature - "·" # nOthing - •d # +⟜(↕10)⌾•UCS'0' # Digit - "¯.π∞" # Numeric - "_"∾˜' '(+∾⊢)⌾•UCS•a # Alphabetic - 1↑"𝕨" # Prefix for input (hack around UTF-16) - " "∾tab # Whitespace -# #'" eliminated during tokenization -⟩ -bF‿bM‿bC‿bI‿bS‿bG‿bB‿bL‿bO‿bD‿bN‿bA‿bP‿bW←⊔/≠¨charGroups -vi←+´≠¨9↑charGroups -charRole←(vi+1)↑(/0∾≠¨3↑charGroups)∾10⥊↕2 - -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 - - args←charSet⊸⊐¨𝕨⋄c←charSet⊐f/𝕩 - w←(≠↑0∾⊢)⊸ׯ1⋆2|⊢)⊐)𝕩 - 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 - g↩⍋+`rev⊏p⋄gr←g⊏rev⋄sll←1+2÷˜0(<-○/>)gr⊏sr-sl - g⊏˜↩⍋gr⊏sl⋄gr↩g⊏rev⋄𝕩⊏˜↩gr⋄r⊏˜↩gr⋄o⊏˜↩gr⋄si←/gr⊏sr>sl - s←𝕩∊bS⋄ps←s∨o∨gr⊏sl⋄a←𝕩∊2↑bG - r-↩ps∨a⋄op←r≥2⋄ro←op∨1⌽r=3 - tr←(⌈`↕∘≠⊸ׯ1⌽ps)⊏ro∨r≥1 - tf←(a≤○(⌈`(1+↕∘≠)⊸×)ps)∧(⊢∧2(|<≤)ps(⊢-⌈`∘×)+`)¬ro - opa←op>1↓0∾˜ps∨a⋄os←⌽↕∘≠⊸(⊣-⌈`∘×)⌽¬ro - oa←⌽/opa⋄fa←/(tf∨¬tr)∧(ro∧1⌽opa)<(r=1)∨op<¯1⌽opa - dy←fa⊏1↓0∾˜(𝕩≠⊑bO)∧(tr∧r≥0)∨r=0 - n←𝕩≥vi+nv⋄id←/n<𝕩≥vi⋄cn←/n∨𝕩≤¯1⊑bC⋄u←⍷ob←cn⊏𝕩 - lo←(o/𝕩)=4⊑bB⋄l←/𝕩=5⊑bB⋄ll←1+lo/1(↓--⊸↓)(o∾1)/+`(s∾0)-(1∾o)∧ps∾1 - dr←/s>o+`⊸⊏0∾lo - or←⍋g⊏˜∾⟨cn,cn,id,id,1+/a,dr,l,l,si,si,oa+1⌈oa⊏os,(dy×⊏⟜os)⊸+fa+dy⟩ - bc←or⊏∾⟨0¨cn,u⊐ob,1+(id-1)⊏a,vi-˜id⊏𝕩,(11-⊑bG)+a/𝕩 - 14¨dr,3¨l,ll,3¨si,sll,5+oa⊏r,5+dy+4×fa⊏tr⟩ - bc‿u -} - -prims←⟨ - +,-,×,÷,⋆,√,⌊,⌈,|,¬,∧,∨,<,>,≠,=,≤,≥,≡,≢,⊣,⊢,⥊,∾,≍,↑,↓,↕,⌽,⍉,/,⍋,⍒,⊏,⊑,⊐,⊒,∊,⍷,⊔,! - ˜,˘,¨,⌜,⁼,´,` - ∘,○,⊸,⟜,⌾,⊘,◶,⎉,⚇,⍟ -⟩ -DGenFn←{ - ⟨t,r,nVar,nLoc,lits⟩←⟨⟩Tokenize𝕩 - ⟨bc,u⟩←nVar‿r DParse t - o←(u-(vi+nVar-≠prims)×u≥vi)⊏prims∾LitVal¨lits - ⟨bc,o,nVar↑•a,⟨⟩⟩ -} -DRun←{ - 'a' •COMP DGenFn 𝕩 -} diff --git a/dcshim.bqn b/dcshim.bqn deleted file mode 100755 index 1762c342..00000000 --- a/dcshim.bqn +++ /dev/null @@ -1,3 +0,0 @@ -#! /usr/bin/env dbqn - -((<•path∾"dc.bqn")∾•args) •EX •path∾"spec/dzref" diff --git a/doc/gen b/doc/gen index 8733a52f..8a70dfdd 100755 --- a/doc/gen +++ b/doc/gen @@ -1,3 +1,3 @@ #! /usr/bin/env bash -for f in *.md; do ../spec/dzref md.bqn "•←ConvertFile \"$PWD/$f\"" > ../doc/${f%md}html; done +for f in *.md; do ../dzref md.bqn "•←ConvertFile \"$PWD/$f\"" > ../docs/${f%md}html; done diff --git a/doc/md.bqn b/doc/md.bqn index acb264ba..61f84ce7 100644 --- a/doc/md.bqn +++ b/doc/md.bqn @@ -254,7 +254,7 @@ Markdown ← { # since Github doesn't seem to have published theirs TestSections ← { doHighlight ↩ 0 - tests ← ¯2 ↓˘ 8⊸(÷˜⟜≠∾⊣)⊸⥊2↓•LNS •path∾"../spec.json" + tests ← ¯2 ↓˘ 8⊸(÷˜⟜≠∾⊣)⊸⥊2↓•LNS •path∾"spec.json" tests ↩ ((⊑2+⊐⟜':')¨∘⊏ ((-','=¯1⊑⊢)↓↓)¨⎉1 ⊢) tests testSection ← (1↓¯1↓⊢)¨ 5⊏˘tests UnEsc ← { diff --git a/dzref b/dzref new file mode 100755 index 00000000..eabf24cf --- /dev/null +++ b/dzref @@ -0,0 +1,205 @@ +#!/usr/bin/env dbqn + +impl ← " +#⌜ +# LAYER 3: Remove other limits +# Now all implementations are full but ↕ is monadic only + +Int←IsArray◶⟨⌊⊸=,0⟩ +Nat←IsArray◶⟨0⊸≤∧⌊⊸=,0⟩ + + +#⌜ +# LAYER 4: Operators + + +Cell ← ↓⟜≢ + +_ranks ← {⟨2⟩⊘⟨1,0⟩((⊣-1+|)˜⟜≠⊑¨<∘⊢)⥊∘𝔽} +_depthOp_←{ + neg←0>n←𝕨𝔾_ranks𝕩 ⋄ F←𝔽 + _d←{ + R←(𝕗+neg)_d + 𝕨(2⥊(neg∧𝕗≥0)∨(0⌈𝕗)≥≍○<○≡)◶(⟨R¨⋄R⟜𝕩¨∘⊣⟩≍⟨(𝕨R⊢)¨∘⊢⋄F⟩)𝕩 + } + 𝕨 n _d 𝕩 +} +⚇ ← _depthOp_ + + +#⌜ +# LAYER 5: Structural functions + +_onAxes_←{ + F←𝔽 + (𝔾<≡)∘⊣◶{ # One axis + ! 1≤≠≢𝕩 + 𝕨F𝕩 + }‿{ # Multiple axes + ! 1≥≠≢𝕨 + ! 𝕨≤○≠≢𝕩 + R←{(⊑𝕨)F(1↓𝕨)⊸R˘𝕩}⍟{0<≠𝕨} + 𝕨R𝕩 + } +} + +Windows←{ + ! IsArray 𝕩 + ! 1≥≠≢𝕨 + ! 𝕨≤○≠≢𝕩 + ! ∧´Nat¨⥊𝕨 + s←(≠𝕨)↑≢𝕩 + ! ∧´𝕨≤1+s + 𝕨{(∾⟜(𝕨≠⊸↓≢𝕩)∘≢⥊>)<¨⊸⊏⟜𝕩¨s(¬+⌜○↕⊢)⥊𝕨}⍟(0<≠𝕨)𝕩 +} + +Rotate ← ⌽ _onAxes_ 0 + +↕ ↩ ↕ ⊘ Windows +⌽ ↩ ⌽ ⊘ Rotate + + +#⌜ +# LAYER 6: Everything else + + +Join←{ + C←(<⟨⟩)⥊⊸∾⌜´⊢ # Cartesian array product + ! IsArray 𝕩 + s←≢¨𝕩 + d←≠⊑s + ! ∧´⥊d=≠¨s + ! d≥≠≢𝕩 + l←(≢𝕩){(𝕩⊑⟜≢a⊑˜(j=𝕩)⊸×)¨↕𝕨}¨j←↕r←≠≢a←𝕩 + ! (r↑¨s)≡C l + i←C{p←+´¨↑𝕩⋄(↕⊑⌽p)-𝕩/¯1↓p}¨l + >i<¨⊸⊏¨l/𝕩 +}⍟(0<≠∘⥊) + +∾ ↩ Join ⊘ ∾ + +# Searching +IndexOf←(1<⌈○(≠≢))◶⊐‿{ + c←1-˜≠≢𝕨 + ! 0≤c + 𝕨 (0<≠𝕨)◶⟨0⎉c∘⊢,((+´<˘)∧`)≢⎉c⎉c‿∞⟩ 𝕩 +} +UniqueMask←{ + ! 1≤≠≢𝕩 + u←0↑𝕩 + {(≠u)>⊑u IndexOf 𝕩}◶{u↩u∾𝕩⋄1}‿0˘𝕩 +} +Find←{ + r←≠s←≢𝕨 + ! r≤≠≢𝕩 + 𝕨 ≡⎉r s ↕⎉r 𝕩 +} + +⊐ ← !∘0 ⊘ IndexOf +∊ ← UniqueMask ⊘ (⊐˜<≠∘⊢) +⍷ ← ∊⊸/ ⊘ Find + +ReorderAxes←{ + 𝕩↩<⍟(0=≡)𝕩 + ! 1≥≠≢𝕨 + 𝕨↩⥊𝕨 + ! 𝕨≤○≠≢𝕩 + ! ∧´Nat¨⥊𝕨 + r←(≠≢𝕩)-+´¬∊𝕨 + ! ∧´𝕨-<)𝕩 # Assume they're numbers +}‿{ # At least one array + e←𝕨-˜○(0∨´0=≢)𝕩 + 𝕨(e=0)◶e‿{ + c←𝕨×∘-○(IsArray+≠∘≢)𝕩 + s←≢𝕨 ⋄ t←≢𝕩 ⋄ r←s⌊○≠t + l←s{i←+´∧`𝕨=𝕩⋄m←1×´i↑𝕨⋄{c↩×-´𝕩⋄m↩m×⌊´𝕩}∘(⊑¨⟜𝕨‿𝕩)⍟(r⊸>)i⋄m}○(r↑⌽)t + a←⥊𝕨⋄b←⥊𝕩 + Trav←(=⟜l)◶{Trav∘(1+𝕩)⍟(0⊸=)a Cmp○(𝕩⊸⊑)b}‿c + Trav 0 + }𝕩 +} + +_bins←{ + c←1-˜≠≢𝕨 + ! 0≤c + LE←𝔽⎉c≤0˜ + ! (0<≠)◶⟨1,∧´·LE´˘2↕<˘⟩𝕨 + 𝕨 (0<≠𝕨)◶⟨0⎉c∘⊢,(+´<˘)LE⎉¯1‿∞⟩ 𝕩 +} + +OccurrenceCount ← ⊐˜(⊢-⊏)⍋∘⍋ +ProgressiveIndexOf ← {𝕨⊐○(≍˘⟜OccurrenceCount𝕨⊸⊐)𝕩} + +⍋ ↩ ⍋ ⊘ ( Cmp _bins) +⍒ ↩ ⍒ ⊘ (-∘Cmp _bins) +⊒ ← OccurrenceCount⊘ ProgressiveIndexOf +" + +X←Raw←{≤4} +{ + chrs←⟨ + "!∾↕⌽⍉⍋⍒⊐⊒∊⍷" + "" + "⚇" + ⟩ + nc ← ≠¨chrs + chr ← ∾chrs + itr ← 0⥊˜≠chr + + init ← " "⊸∾¨(/⟜"_"¨nc/0‿1‿1)∾¨(nc/"FMD")∾¨(nc+´⊸↑⥊"AB"∾⌜•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 + } + + # starting built-ins + inps←⟨"𝕨 ","𝕨,𝕗","𝕨,𝕘"⟩ + ⍎¨names∾¨(nc/("←{⟨"∾∾⟜"⟩ ⋄ ⍎""Using undefined built-in ")¨inps)∾¨∾⟜"""}"¨chr + + # built-in assumptions + Mod ← ⍎{𝔽 ((⊑chr⊐𝕨)⊑names) ∾ " ↩ " ∾ 𝕩} + + ⍎"IsArray ← 0≠≡" + ⍎"Type ← ⟨⟩⥊0⊸⥊" + + '!' Mod "{𝕩 ⋄ ≤1}⍟¬" + Mod⟜⥊¨ "↕∾⌽⊐⍋⍒" + + + # checks if line is a builtin redefinition + E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩ + + # removes comments and replaces built-ins with names + 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 + } + + lf ← •UCS 10 + pre ← E_isdef◶E_proc‿E_redef¨ lf((⊢-˜¬×+`)∘=⊔⊢)impl + Raw↩⍎ + ExecFile←{Raw ∾ ∾⟜lf¨ E_proc¨ •LNS 𝕩} + X↩Raw∘E_proc + ⍎ ∾ ∾⟜lf¨ pre + ≠◶X‿{ExecFile ⊑𝕩}‿{ExecFile ⊑𝕩 ⋄ X 1⊑𝕩} •args +}0 diff --git a/nc.bqn b/nc.bqn new file mode 100755 index 00000000..9d5dc316 --- /dev/null +++ b/nc.bqn @@ -0,0 +1,124 @@ +#!/usr/bin/env bqn + +⍝ This version of the compiler uses the old BQN prototype and is no +⍝ longer in development. See c.bqn instead. + +reduce←{r←𝕩⋄{(>𝔽¨_r)⍠(><⊸(𝔽¨_r))}}⊑⟨´⟩ +´↩_reduce +⊔↩((↕1+⌈´)=¨<)∘⊣ /¨⟜< ↕∘≠⍠⊢ +⍷↩∪ + +Base←{+⟜(𝕨⊸×)´⌽𝕩} +Enc2←{𝕨⥊2(|∾˜(𝕨-1)Enc2⌊∘÷˜)⍟(0<𝕨)𝕩} + +lf‿tab←•UCS 10‿9 +charSet←∾charGroups←⟨ + chF←"+-×÷⋆√⌊⌈|¬∧∨<>≠=≤≥≡≢⊣⊢⥊∾≍↑↓↕⌽⍉/⍋⍒⊏⊑⊐⊒∊⍷⊔" ⍝ Function + "˜˘¨⌜⁼´`" ⍝ Modifier + "∘○⊸⟜⌾⊘◶⎉⚇⍟" ⍝ Composition + "𝕨𝕎𝕩𝕏𝕗𝔽𝕘𝔾" ⍝ Input + lf∾"⋄," ⍝ Separator + "←↩→" ⍝ Gets + "(){}⟨⟩" ⍝ Bracket + "‿" ⍝ Ligature + •d ⍝ +⟜(↕10)⌾•UCS'0' ⍝ Digit + "¯.π∞" ⍝ Numeric + "_"∾' '(⊢∾+)⌾•UCS•a ⍝ Alphabetic + " "∾tab ⍝ Whitespace +⍝ #'" eliminated during tokenization +⟩ +bF‿bM‿bC‿bI‿bS‿bG‿bB‿bL‿bD‿bN‿bA‿bW←⊔/≠¨charGroups +vi←+´≠¨8↑charGroups + +Tokenize←{ + r←𝕩='#'⋄s←/(≠↑2⊸↓)⊸∧𝕩=⊑"'"⋄d←/𝕩='"' + g←⍋q←∾⟨ s⋄¯1↓d⋄/r⟩ ⋄q↩g⊏q + e← g⊏∾⟨2+s⋄ 1↓d⋄(⊢-¯1↓0∾⊢)∘⊏⟜(0∾+`r)⊸//(𝕩=lf)∾1⟩ + Se←{(⊏˜𝕨)Se 1¨⌾((𝕩/𝕨)⊸⊏)𝕩}⍟{0=⊑⌽𝕩} + st←¯1↓Se⟜(1↑˜≠)∾⟜≠q⍋e⋄b←st/q∾˘e + ToI←(≠𝕩)↑/⁼∘⥊⋄f←¬≠`ToI b + cb←(¬(st/q)⊏r)/b + nl←≠lu←⍷lit←𝕩⊔˜1-˜(+`ToI⊑˘cb)×≠`ToI cb + cl←f/ToI⊑∘⌽˘cb + args←charSet⊸⊐¨𝕨⋄c←charSet⊐f/𝕩 + w←(<´≠↕0∾⊢)l←c∊∾bD‿bN‿bA + id←⍷args∾(bA∊˜w/c)⍒⊸⊏i←(1-˜l×+`w)⊔c + nv←+´bA∊˜⊑¨id + c↩(w∨¬l∨c∊bW)/(vi+id⊐i)⌾(w⊸/)(vi+(≠id)+lu⊐lit)⌾(cl⊸/)c + c(/˜)↩¬(1⌾⊑1⌽1⌾⊑)⊸∧c∊bS + ⟨c,nv,nv-≠args,(nv↓id)∾lu⟩ +} + +Parse←{ + a←𝕩∊(2↑bG)⋄at←1⌽a⋄𝕩(/˜)↩¬a⋄a(/˜)↩¬at + l←≠𝕩⋄sep←𝕩∊bS⋄𝕩↩(bF⊑˜chF⊐<'⊣')¨⌾(sep⊸/)𝕩⋄sep∨↩𝕩=2⊑bB + o←𝕩=0⊑bB⋄c←𝕩=1⊑bB⋄v←a-˜𝕩≥vi⋄f←¬o∨c∨v∨sep + na←(2×sep)+f×1+l↑0∾c∨v + d←+`o-c⋄fe←(+`⌾((⍋d)⊸⊏)o)⊏l∾(⍋⊏⟜d)⊸⊏/c + fe⌊↩l-l↑(⌈`↕∘≠⊸×)⌾⌽1∾˜sep + sel←¬∘⊏⟜(o∨c)⊸/⍋((f×fe)⌈↕l)-+`f-l↑/⁼∧f/fe + sel⊸⊏¨𝕩‿na +} + +Hex←16 Base (•d∾"ABCDEF")⊸⊐ + +ReadNum←10 Base bD⊸⊐ +GenF64←{ + 𝕩=0:8⥊0 + l←2(⌊⋆⁼)𝕩 + (Hex"44")∾2 Base˘⌽8‿8(⊣⥊×´⊸↑)∾⟨⥊0,11 Enc2 1023+l,(0⌈l)Enc2𝕩⟩ +} +MakeTab←{{(≠chF)↑⊑¨(chF⊐𝕨)⊔𝕩}○∾⟜(⥊¨∘⥊¨)´<˘⍉(2(÷˜∾⊣)≢)⊸⥊𝕩} +tab1←MakeTab⟨ + "⊣⊢" , 2⥊<⟨⟩ + "|-⌈⌊√" , (Hex"99")+(↕4)∾6 + "÷" , <(Hex"10")∾0 + "¬" , <∾⥊¨⟨Hex"9A",GenF64 1,Hex"A0"⟩ +⟩ +tab2←MakeTab⟨ + "⊣" , Hex"1A" + "+-×÷⌊⌈∧", (Hex"A0")+(↕6)∾2 + "¬" , <∾⥊¨⟨Hex"A1",GenF64 1,Hex"A0"⟩ + "∨" , <(Hex"10")∾1 +⟩ +fntab←⍉¯3↑tab1≍tab2 +f64←127-3 + +GenFn←{ + ⟨⟩GenFn𝕩; + ⟨t,nVar,nLoc,lits⟩←𝕨Tokenize𝕩 + t(⊏˜)↩⍋+`-´<˘(2‿3⊏bB)=⌜t + nd←+´c←t=3⊑bB + t↩((vi+nVar+≠lits)+↕∘≠)⌾(c⊸/)t + ⟨a,na⟩←Parse t + ops←⥊∾⟨ + vi↑fntab + ⍉3↑(Hex¨"20"‿"22")∾⌜↕nVar + ⊣⌜⟜(↕3)(GenF64∘ReadNum¨lits)∾(Hex"10")∾¨3+↕nd + ⟩ + (((≠∾∾)⟨nLoc∾f64⟩)∾(Hex"0B")∾˜∾)¨((⊢-˜¬×+`)a=2⊑bB)⊔(na+3×a)⊏ops +} + +Gen←{ + LEB←{0≡𝕩:⥊0⋄128⊸+⌾(¯1⊸↓) 2 Base ⌽⍉ (∨`⌾⌽∨´˘)⊸/ 10‿7(⊣⥊×´⊸↑) ⌽64 Enc2 𝕩} + C←LEB∘≠⊸∾ + S←∾⟜C + V←≠∾∾ + I←C∘•UCS + t‿n‿b←𝕩 + ∾⟨ + 0∾(•UCS"asm")∾4↑1 + 1 S V (96∾⟜∾C¨)¨t + 3 S V ⥊¨↕≠b + 7 S V ⥊<"fn"I⊸(⊣∾0∾⊢)n + 10 S V C¨ b + ⟩ +} + +Compile←{ + body←GenFn 𝕩 + rcp←⟨"x"⟩ GenFn "1÷x" + or←⟨"w","x"⟩ GenFn "(w+x)-w∧x" + f←⟨rcp,or,body⟩ + Gen ⟨(≠¨f)/⟨1‿1,2‿1,0‿1⟩/¨¨f64 ⋄ 2 ⋄ ∾f⟩ +} diff --git a/running.md b/running.md index 7472e89e..987d149d 100644 --- a/running.md +++ b/running.md @@ -26,6 +26,6 @@ The left argument for `•EX` or the shell arguments can contain up to two argum ### BQN -This repository contains the beginnings of a self-hosted compiler for BQN, which is not yet complete enough to do any real programming with. There are currently several versions of the compiler: [c.bqn](c.bqn) is run with BQN2NGN, while [dc.bqn](dc.bqn) is run with dzaima+reference. Both compilers have a backend targetting [WebAssembly](https://en.wikipedia.org/wiki/WebAssembly), and dc.bqn additionally has a backend that targets dzaima/BQN's own bytecode, so that the compiler uses only BQN, but the runtime uses the Java implementations of BQN primitives from dzaima/BQN. +This repository contains the beginnings of a self-hosted compiler for BQN, which is not yet complete enough to do any real programming with. There are currently several versions of the compiler: [nc.bqn](nc.bqn) is run with BQN2NGN, while [c.bqn](c.bqn) is run with dzaima+reference. Both compilers have a backend targetting [WebAssembly](https://en.wikipedia.org/wiki/WebAssembly), and c.bqn additionally has a backend that targets dzaima/BQN's own bytecode, so that the compiler uses only BQN, but the runtime uses the Java implementations of BQN primitives from dzaima/BQN. All versions have automated tests in the [test](test/) directory, with the WebAssembly versions tested with Javascript using Node ([test/t.js](test/t.js) and [test/dt.js](test/dt.js) for BQN2NGN and dzaima/BQN respectively) and the dzaima/BQN backend tested with BQN itself ([test/bt](test/bt)). diff --git a/spec/dzref b/spec/dzref deleted file mode 100755 index eabf24cf..00000000 --- a/spec/dzref +++ /dev/null @@ -1,205 +0,0 @@ -#!/usr/bin/env dbqn - -impl ← " -#⌜ -# LAYER 3: Remove other limits -# Now all implementations are full but ↕ is monadic only - -Int←IsArray◶⟨⌊⊸=,0⟩ -Nat←IsArray◶⟨0⊸≤∧⌊⊸=,0⟩ - - -#⌜ -# LAYER 4: Operators - - -Cell ← ↓⟜≢ - -_ranks ← {⟨2⟩⊘⟨1,0⟩((⊣-1+|)˜⟜≠⊑¨<∘⊢)⥊∘𝔽} -_depthOp_←{ - neg←0>n←𝕨𝔾_ranks𝕩 ⋄ F←𝔽 - _d←{ - R←(𝕗+neg)_d - 𝕨(2⥊(neg∧𝕗≥0)∨(0⌈𝕗)≥≍○<○≡)◶(⟨R¨⋄R⟜𝕩¨∘⊣⟩≍⟨(𝕨R⊢)¨∘⊢⋄F⟩)𝕩 - } - 𝕨 n _d 𝕩 -} -⚇ ← _depthOp_ - - -#⌜ -# LAYER 5: Structural functions - -_onAxes_←{ - F←𝔽 - (𝔾<≡)∘⊣◶{ # One axis - ! 1≤≠≢𝕩 - 𝕨F𝕩 - }‿{ # Multiple axes - ! 1≥≠≢𝕨 - ! 𝕨≤○≠≢𝕩 - R←{(⊑𝕨)F(1↓𝕨)⊸R˘𝕩}⍟{0<≠𝕨} - 𝕨R𝕩 - } -} - -Windows←{ - ! IsArray 𝕩 - ! 1≥≠≢𝕨 - ! 𝕨≤○≠≢𝕩 - ! ∧´Nat¨⥊𝕨 - s←(≠𝕨)↑≢𝕩 - ! ∧´𝕨≤1+s - 𝕨{(∾⟜(𝕨≠⊸↓≢𝕩)∘≢⥊>)<¨⊸⊏⟜𝕩¨s(¬+⌜○↕⊢)⥊𝕨}⍟(0<≠𝕨)𝕩 -} - -Rotate ← ⌽ _onAxes_ 0 - -↕ ↩ ↕ ⊘ Windows -⌽ ↩ ⌽ ⊘ Rotate - - -#⌜ -# LAYER 6: Everything else - - -Join←{ - C←(<⟨⟩)⥊⊸∾⌜´⊢ # Cartesian array product - ! IsArray 𝕩 - s←≢¨𝕩 - d←≠⊑s - ! ∧´⥊d=≠¨s - ! d≥≠≢𝕩 - l←(≢𝕩){(𝕩⊑⟜≢a⊑˜(j=𝕩)⊸×)¨↕𝕨}¨j←↕r←≠≢a←𝕩 - ! (r↑¨s)≡C l - i←C{p←+´¨↑𝕩⋄(↕⊑⌽p)-𝕩/¯1↓p}¨l - >i<¨⊸⊏¨l/𝕩 -}⍟(0<≠∘⥊) - -∾ ↩ Join ⊘ ∾ - -# Searching -IndexOf←(1<⌈○(≠≢))◶⊐‿{ - c←1-˜≠≢𝕨 - ! 0≤c - 𝕨 (0<≠𝕨)◶⟨0⎉c∘⊢,((+´<˘)∧`)≢⎉c⎉c‿∞⟩ 𝕩 -} -UniqueMask←{ - ! 1≤≠≢𝕩 - u←0↑𝕩 - {(≠u)>⊑u IndexOf 𝕩}◶{u↩u∾𝕩⋄1}‿0˘𝕩 -} -Find←{ - r←≠s←≢𝕨 - ! r≤≠≢𝕩 - 𝕨 ≡⎉r s ↕⎉r 𝕩 -} - -⊐ ← !∘0 ⊘ IndexOf -∊ ← UniqueMask ⊘ (⊐˜<≠∘⊢) -⍷ ← ∊⊸/ ⊘ Find - -ReorderAxes←{ - 𝕩↩<⍟(0=≡)𝕩 - ! 1≥≠≢𝕨 - 𝕨↩⥊𝕨 - ! 𝕨≤○≠≢𝕩 - ! ∧´Nat¨⥊𝕨 - r←(≠≢𝕩)-+´¬∊𝕨 - ! ∧´𝕨-<)𝕩 # Assume they're numbers -}‿{ # At least one array - e←𝕨-˜○(0∨´0=≢)𝕩 - 𝕨(e=0)◶e‿{ - c←𝕨×∘-○(IsArray+≠∘≢)𝕩 - s←≢𝕨 ⋄ t←≢𝕩 ⋄ r←s⌊○≠t - l←s{i←+´∧`𝕨=𝕩⋄m←1×´i↑𝕨⋄{c↩×-´𝕩⋄m↩m×⌊´𝕩}∘(⊑¨⟜𝕨‿𝕩)⍟(r⊸>)i⋄m}○(r↑⌽)t - a←⥊𝕨⋄b←⥊𝕩 - Trav←(=⟜l)◶{Trav∘(1+𝕩)⍟(0⊸=)a Cmp○(𝕩⊸⊑)b}‿c - Trav 0 - }𝕩 -} - -_bins←{ - c←1-˜≠≢𝕨 - ! 0≤c - LE←𝔽⎉c≤0˜ - ! (0<≠)◶⟨1,∧´·LE´˘2↕<˘⟩𝕨 - 𝕨 (0<≠𝕨)◶⟨0⎉c∘⊢,(+´<˘)LE⎉¯1‿∞⟩ 𝕩 -} - -OccurrenceCount ← ⊐˜(⊢-⊏)⍋∘⍋ -ProgressiveIndexOf ← {𝕨⊐○(≍˘⟜OccurrenceCount𝕨⊸⊐)𝕩} - -⍋ ↩ ⍋ ⊘ ( Cmp _bins) -⍒ ↩ ⍒ ⊘ (-∘Cmp _bins) -⊒ ← OccurrenceCount⊘ ProgressiveIndexOf -" - -X←Raw←{≤4} -{ - chrs←⟨ - "!∾↕⌽⍉⍋⍒⊐⊒∊⍷" - "" - "⚇" - ⟩ - nc ← ≠¨chrs - chr ← ∾chrs - itr ← 0⥊˜≠chr - - init ← " "⊸∾¨(/⟜"_"¨nc/0‿1‿1)∾¨(nc/"FMD")∾¨(nc+´⊸↑⥊"AB"∾⌜•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 - } - - # starting built-ins - inps←⟨"𝕨 ","𝕨,𝕗","𝕨,𝕘"⟩ - ⍎¨names∾¨(nc/("←{⟨"∾∾⟜"⟩ ⋄ ⍎""Using undefined built-in ")¨inps)∾¨∾⟜"""}"¨chr - - # built-in assumptions - Mod ← ⍎{𝔽 ((⊑chr⊐𝕨)⊑names) ∾ " ↩ " ∾ 𝕩} - - ⍎"IsArray ← 0≠≡" - ⍎"Type ← ⟨⟩⥊0⊸⥊" - - '!' Mod "{𝕩 ⋄ ≤1}⍟¬" - Mod⟜⥊¨ "↕∾⌽⊐⍋⍒" - - - # checks if line is a builtin redefinition - E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩ - - # removes comments and replaces built-ins with names - 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 - } - - lf ← •UCS 10 - pre ← E_isdef◶E_proc‿E_redef¨ lf((⊢-˜¬×+`)∘=⊔⊢)impl - Raw↩⍎ - ExecFile←{Raw ∾ ∾⟜lf¨ E_proc¨ •LNS 𝕩} - X↩Raw∘E_proc - ⍎ ∾ ∾⟜lf¨ pre - ≠◶X‿{ExecFile ⊑𝕩}‿{ExecFile ⊑𝕩 ⋄ X 1⊑𝕩} •args -}0 diff --git a/test/bt b/test/bt index 5679f686..29c1e1e5 100755 --- a/test/bt +++ b/test/bt @@ -1,6 +1,6 @@ #! /usr/bin/env dbqn -dRun ← ⟨"dRun"⟩ •EX •path∾"../dcshim.bqn" +dRun ← ⟨"dRun"⟩ •EX •path∾"../cshim.bqn" l ← ∾(•LNS •path∾∾⟜"cases.bqn")¨""‿"b" { l ∾↩ "1 %"⊸∾¨ (3≤≠)◶0‿(('#'≠2⊸⊑)∧" "≡2⊸↑)¨⊸/ •LNS •path∾"testref.bqn" }⍟⊢(<"-ref")∊•args c ← (¬"{"⊑∘∊⊢)¨⊸/ l diff --git a/test/dt.js b/test/dt.js deleted file mode 100755 index 49636a66..00000000 --- a/test/dt.js +++ /dev/null @@ -1,26 +0,0 @@ -//usr/bin/env node "$0" $@;exit $? -const execFile = require('child_process').execFile - , load=f=>require('fs').readFileSync(__dirname+'/'+f,'utf8') - , runWasm=w=>new WebAssembly.Instance(new WebAssembly.Module(Uint8Array.from(w))) - .exports.fn() - -const t=load('cases.bqn').split('\n').filter(x=>x).map(x=>x.split(' % ')) - , test=t.map(e=>'"'+e[1]+'"').join('\n') - , expt=t.map(e=>+e[0]) - -var compiler = execFile( - __dirname+'/../dcshim.bqn', - [ '{•←Compile𝕩}¨⟨'+test+'⟩' ], - function (error, stdout, stderr) { - const rslt=stdout.split('\n').filter(a=>a.length) - .map(a=>runWasm(a.split("‿").map(n=>+n))) - , pass=rslt.map((r,i)=>r===expt[i]) - , fail=pass.map((p,i)=>p?-1:i).filter(i=>i>=0) - console.log( - fail.length - ? fail.map(i=>'"'+t[i][1]+'": expected '+expt[i]+' but received '+rslt[i]) - : "All passed!" - ) - process.exit(+(fail.length>0)) - } -); diff --git a/test/nt.js b/test/nt.js new file mode 100755 index 00000000..5cd38556 --- /dev/null +++ b/test/nt.js @@ -0,0 +1,19 @@ +//usr/bin/env node "$0" $@;exit $? +const bqn=require(require('os').homedir+'/bin/bqn') + , load=f=>require('fs').readFileSync(__dirname+'/'+f,'utf8') + , runWasm=w=>new WebAssembly.Instance(new WebAssembly.Module(Uint8Array.from(w))) + .exports.fn() + +const t=load('cases.bqn').split('\n').filter(x=>x).map(x=>x.split(' % ')) + , test=t.map(e=>'"'+e[1]+'"').join('\n') + , expt=t.map(e=>+e[0]) + , rslt=bqn(load('../nc.bqn','utf8').concat('Compile¨⟨'+test+'⟩')) + .a.map((a,i)=>runWasm(a.a)) + , pass=rslt.map((r,i)=>r===expt[i]) + , fail=pass.map((p,i)=>p?-1:i).filter(i=>i>=0) +console.log( + fail.length + ? fail.map(i=>'"'+t[i][1]+'": expected '+expt[i]+' but received '+rslt[i]) + : "All passed!" +) +process.exit(+(fail.length>0)) diff --git a/test/t.js b/test/t.js index f958bbb3..88310720 100755 --- a/test/t.js +++ b/test/t.js @@ -1,5 +1,5 @@ //usr/bin/env node "$0" $@;exit $? -const bqn=require(require('os').homedir+'/bin/bqn') +const execFile = require('child_process').execFile , load=f=>require('fs').readFileSync(__dirname+'/'+f,'utf8') , runWasm=w=>new WebAssembly.Instance(new WebAssembly.Module(Uint8Array.from(w))) .exports.fn() @@ -7,13 +7,20 @@ const bqn=require(require('os').homedir+'/bin/bqn') const t=load('cases.bqn').split('\n').filter(x=>x).map(x=>x.split(' % ')) , test=t.map(e=>'"'+e[1]+'"').join('\n') , expt=t.map(e=>+e[0]) - , rslt=bqn(load('../c.bqn','utf8').concat('Compile¨⟨'+test+'⟩')) - .a.map((a,i)=>runWasm(a.a)) - , pass=rslt.map((r,i)=>r===expt[i]) - , fail=pass.map((p,i)=>p?-1:i).filter(i=>i>=0) -console.log( - fail.length - ? fail.map(i=>'"'+t[i][1]+'": expected '+expt[i]+' but received '+rslt[i]) - : "All passed!" -) -process.exit(+(fail.length>0)) + +var compiler = execFile( + __dirname+'/../cshim.bqn', + [ '{•←Compile𝕩}¨⟨'+test+'⟩' ], + function (error, stdout, stderr) { + const rslt=stdout.split('\n').filter(a=>a.length) + .map(a=>runWasm(a.split("‿").map(n=>+n))) + , pass=rslt.map((r,i)=>r===expt[i]) + , fail=pass.map((p,i)=>p?-1:i).filter(i=>i>=0) + console.log( + fail.length + ? fail.map(i=>'"'+t[i][1]+'": expected '+expt[i]+' but received '+rslt[i]) + : "All passed!" + ) + process.exit(+(fail.length>0)) + } +); -- cgit v1.2.3