From 4db82b44328828a3a0e72d8bd0c82eeefe8c1c5e Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 27 Nov 2020 16:09:02 -0500 Subject: Move fmt.bqn to f.bqn --- src/cjs.bqn | 4 +-- src/f.bqn | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/fmt.bqn | 84 ------------------------------------------------------------- 3 files changed, 86 insertions(+), 86 deletions(-) create mode 100644 src/f.bqn delete mode 100644 src/fmt.bqn (limited to 'src') diff --git a/src/cjs.bqn b/src/cjs.bqn index 8041b9bc..3fae00a8 100755 --- a/src/cjs.bqn +++ b/src/cjs.bqn @@ -13,10 +13,10 @@ J ← ∾∾⟜(@+10)¨ Fout ← (≠↑⟨⊢,Fconst,L,⊢⟩˙) {L𝕎¨𝕩}¨ ⊢ Frun ← ("provide.concat("∾∾⟜")")⌾(1⊸⊑) Fout Long ← ∾ (≠↑1‿3/⟨" "⊸∾⋄((@+10)∾" ,")⊸∾⟩˙) {𝕎𝕩}¨ ⊢ -•Out (⊑"r"‿"c"‿"fmt"‿"e"⊐⊏)◶⟨ +•Out (⊑"r"‿"c"‿"f"‿"e"⊐⊏)◶⟨ {𝕩⋄ref‿len←•Import"pr.bqn"⋄Long Frun len⊸↓⌾(1⊸⊑)Comp ref} {𝕩⋄Long Fout Comp J •LNS "c.bqn"} - {𝕩⋄Long Fout Comp J •LNS "fmt.bqn"} + {𝕩⋄Long Fout Comp J •LNS "f.bqn"} {𝕩⋄Long Fout Comp J ⟨"Modify←GetHighlights←⊢"⟩∾∾ •LNS∘∾⟜".bqn"¨ "../svg"‿"e"} ¯1 ↓ · J L∘Fout∘Comp¨ ⟩ args diff --git a/src/f.bqn b/src/f.bqn new file mode 100644 index 00000000..59721f6a --- /dev/null +++ b/src/f.bqn @@ -0,0 +1,84 @@ +# Format an array to a string including newlines +{ + F0←𝔽 # Format a number or operation to a string + FmtAtom ← ≍ <⟜@◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,F0⟩ + + # Vertical padding for arrays of rank greater than 2 + PadCount ← { + # Empty lines after each row: 1 if it's at the end of a 2-cell, plus + # 1 if it's at the end of a 2-cell and a 3-cell, and so on + # But none at the very end + 0⌾(¯1⊸⊑) ⥊ 𝕨 +⎉¯1‿∞´ ×⌜˜`⌾⌽ (-𝕩)↑¨1 + } + PadV ← { + # Leading shape and padding count + p ← PadCount 1 ⌈ ls ← ¯1↓≢𝕩 + # If 𝕩 has cells, pad by selection; if it's empty, there are only + # pads but selection would try to get cells (1⌈ above), so overtake. + Pad ← {(»⊸<⊸×/0∾1+𝕨) ⊏ ' '¨∘⊏⊸∾𝕩} + p (0<≠∘⊢)◶⟨+´⊸↑,Pad⟩ ((×´ls)∾¯1↑≢𝕩) ⥊ 𝕩 + }⍟(2 < =) + PadVMixed ← { + # PadV, but with 2-cells enclosed: they might have different lengths + ∾ (1 PadCount ≢𝕩) (⊢∾∾⟜(¯1⊑≢)⥊' '˙)¨ ⥊𝕩 + } + + # Horizontal padding: just some spaces on either side + PadH ← { s←⟨≠𝕩,𝕨⟩⥊' ' ⋄ ∾≍⟨s,𝕩,s⟩ } + + # Add a frame to padded data + Enframe ← ∨○(1⊸≠)⟜≠◶{∨´2=+`-˝"⟨⟩"=⌜⊏𝕩}‿1◶{ + # One-line version + ≍"⟨"∾(¯1↓1↓⊏𝕩)∾"⟩" + }‿{ + # General case + l ← ¯1 ⊑ ≢𝕩 + ∾ ⟨ + ≍l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,F0⟩𝕨⟩ + ((0⌈4⌊𝕨-1)⊑"·╵╎┆┊")⌾⊑ 𝕩 + ≍l-⊸↑"┘" + ⟩ + } + + FmtEmpty ← (0‿0≢≢)◶("┌┐"≍"└┘")‿(((2≠=)∨0=≠)◶{ + '┐'⌾(0‿¯1⊸⊑) 2 Enframe 1 PadH " "¨𝕩 + }‿{ + ≍(1<≠)◶⟨"⟨⟩",'↕'⌾⊑·∾·"‿"⊸∾¨F0¨⟩≢𝕩 + }) + + PaddingJoin ← { + s ← ≢¨ 𝕩 + w ← (0<=)◶⟨⥊,⌈˝⍟(=-1˙)⟩1⊑¨s + h ← ⌈˝⎉1⍟(0<=) ⊑¨s + o ← <∘∾⎉2 ≍⍟(0⌈2-=) (h ∾⌜ 𝕨×w¬(-𝕨×≠w)↑1) ↑¨ 𝕩 + 2 PadH (1⊸⌽⊸≡⥊h)◶⟨PadVMixed,PadV∘>⟩ o + } + + FmtMixed ← { + (=𝕩) Enframe (𝕨⊣1) PaddingJoin Fmt¨𝕩 + } + + FmtSimple ← (≠(0⊸<+≤)+´)∘(⥊<@˙)◶{ # Depth 1 + # All characters + k ← ∞⍟(0⊸=) -≠ c ← ¯1↓≢𝕩 + q ← '"' + # Escape quotes in strings (rank 1) and substitute control chars + # with control pictures for other ranks. + CSub ← { 𝕩 + (𝕩(=×'␡'-⊢)@+127) + ('␀'-@)×𝕩<@+32 } + 𝕩 ↩ (1≠=)◶⟨(1+q=⊢)⊸/,Csub⟩ 𝕩 + r ← =𝕩 + (r Enframe 1 PadH PadV)⍟(1≠r) ≍ (q⌾⊑c⥊(¯1⊑1∾c)↑'·') ∾⎉k 𝕩 ∾⎉k c-⊸↑q + }‿{ + # Not homogeneous, or empty + (∨´0=≢)◶FmtMixed‿FmtEmpty 𝕩 + }‿{ + # All numbers + ¯1 FmtMixed 𝕩 + } + + # Format to character matrix + Fmt ← (2⌊≡)◶FmtAtom‿FmtSimple‿FmtMixed + + # Convert to string + ¯1↓·⥊ ∾⟜(@+10)˘∘Fmt +} diff --git a/src/fmt.bqn b/src/fmt.bqn deleted file mode 100644 index 59721f6a..00000000 --- a/src/fmt.bqn +++ /dev/null @@ -1,84 +0,0 @@ -# Format an array to a string including newlines -{ - F0←𝔽 # Format a number or operation to a string - FmtAtom ← ≍ <⟜@◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,F0⟩ - - # Vertical padding for arrays of rank greater than 2 - PadCount ← { - # Empty lines after each row: 1 if it's at the end of a 2-cell, plus - # 1 if it's at the end of a 2-cell and a 3-cell, and so on - # But none at the very end - 0⌾(¯1⊸⊑) ⥊ 𝕨 +⎉¯1‿∞´ ×⌜˜`⌾⌽ (-𝕩)↑¨1 - } - PadV ← { - # Leading shape and padding count - p ← PadCount 1 ⌈ ls ← ¯1↓≢𝕩 - # If 𝕩 has cells, pad by selection; if it's empty, there are only - # pads but selection would try to get cells (1⌈ above), so overtake. - Pad ← {(»⊸<⊸×/0∾1+𝕨) ⊏ ' '¨∘⊏⊸∾𝕩} - p (0<≠∘⊢)◶⟨+´⊸↑,Pad⟩ ((×´ls)∾¯1↑≢𝕩) ⥊ 𝕩 - }⍟(2 < =) - PadVMixed ← { - # PadV, but with 2-cells enclosed: they might have different lengths - ∾ (1 PadCount ≢𝕩) (⊢∾∾⟜(¯1⊑≢)⥊' '˙)¨ ⥊𝕩 - } - - # Horizontal padding: just some spaces on either side - PadH ← { s←⟨≠𝕩,𝕨⟩⥊' ' ⋄ ∾≍⟨s,𝕩,s⟩ } - - # Add a frame to padded data - Enframe ← ∨○(1⊸≠)⟜≠◶{∨´2=+`-˝"⟨⟩"=⌜⊏𝕩}‿1◶{ - # One-line version - ≍"⟨"∾(¯1↓1↓⊏𝕩)∾"⟩" - }‿{ - # General case - l ← ¯1 ⊑ ≢𝕩 - ∾ ⟨ - ≍l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,F0⟩𝕨⟩ - ((0⌈4⌊𝕨-1)⊑"·╵╎┆┊")⌾⊑ 𝕩 - ≍l-⊸↑"┘" - ⟩ - } - - FmtEmpty ← (0‿0≢≢)◶("┌┐"≍"└┘")‿(((2≠=)∨0=≠)◶{ - '┐'⌾(0‿¯1⊸⊑) 2 Enframe 1 PadH " "¨𝕩 - }‿{ - ≍(1<≠)◶⟨"⟨⟩",'↕'⌾⊑·∾·"‿"⊸∾¨F0¨⟩≢𝕩 - }) - - PaddingJoin ← { - s ← ≢¨ 𝕩 - w ← (0<=)◶⟨⥊,⌈˝⍟(=-1˙)⟩1⊑¨s - h ← ⌈˝⎉1⍟(0<=) ⊑¨s - o ← <∘∾⎉2 ≍⍟(0⌈2-=) (h ∾⌜ 𝕨×w¬(-𝕨×≠w)↑1) ↑¨ 𝕩 - 2 PadH (1⊸⌽⊸≡⥊h)◶⟨PadVMixed,PadV∘>⟩ o - } - - FmtMixed ← { - (=𝕩) Enframe (𝕨⊣1) PaddingJoin Fmt¨𝕩 - } - - FmtSimple ← (≠(0⊸<+≤)+´)∘(⥊<@˙)◶{ # Depth 1 - # All characters - k ← ∞⍟(0⊸=) -≠ c ← ¯1↓≢𝕩 - q ← '"' - # Escape quotes in strings (rank 1) and substitute control chars - # with control pictures for other ranks. - CSub ← { 𝕩 + (𝕩(=×'␡'-⊢)@+127) + ('␀'-@)×𝕩<@+32 } - 𝕩 ↩ (1≠=)◶⟨(1+q=⊢)⊸/,Csub⟩ 𝕩 - r ← =𝕩 - (r Enframe 1 PadH PadV)⍟(1≠r) ≍ (q⌾⊑c⥊(¯1⊑1∾c)↑'·') ∾⎉k 𝕩 ∾⎉k c-⊸↑q - }‿{ - # Not homogeneous, or empty - (∨´0=≢)◶FmtMixed‿FmtEmpty 𝕩 - }‿{ - # All numbers - ¯1 FmtMixed 𝕩 - } - - # Format to character matrix - Fmt ← (2⌊≡)◶FmtAtom‿FmtSimple‿FmtMixed - - # Convert to string - ¯1↓·⥊ ∾⟜(@+10)˘∘Fmt -} -- cgit v1.2.3