aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/bqn.js6
-rw-r--r--src/fmt.bqn74
2 files changed, 47 insertions, 33 deletions
diff --git a/docs/bqn.js b/docs/bqn.js
index fca87403..a0365699 100644
--- a/docs/bqn.js
+++ b/docs/bqn.js
@@ -196,9 +196,9 @@ let fmt0 = x =>
(x.glyph||(x.m?"*"+x.m+"-modifier*":"*function*"))
: ""+x;
let fmt1 = run(
- new Uint8Array([15,1,25,21,0,4,22,0,5,11,14,0,9,0,7,0,44,19,0,39,15,2,8,22,0,6,11,14,15,3,22,0,7,11,14,21,0,6,0,34,21,0,7,8,22,0,8,11,14,15,4,22,0,9,11,14,21,0,9,15,5,3,2,0,37,15,6,8,22,0,10,11,14,0,60,0,17,0,59,17,15,7,15,8,3,2,0,37,0,8,0,9,0,43,19,0,6,0,9,0,8,0,44,19,19,8,3,2,0,37,0,12,0,12,0,43,0,43,3,2,19,8,22,0,11,11,14,15,9,0,36,15,10,8,22,0,12,11,14,15,11,22,0,13,11,14,0,65,0,13,0,16,0,16,19,0,33,0,66,8,3,2,0,37,0,8,0,33,0,47,8,8,21,0,5,3,2,0,37,0,41,0,34,0,10,8,8,0,17,9,15,12,21,0,13,3,3,0,37,0,11,0,3,0,44,19,8,22,0,14,11,14,21,0,1,21,0,14,16,0,27,0,50,0,0,0,47,17,0,34,0,16,8,7,16,0,15,16,0,19,0,40,17,25,21,0,1,0,12,16,0,19,0,40,17,22,0,3,11,14,0,42,0,28,0,18,7,21,0,3,0,4,0,42,17,0,1,16,17,0,20,0,35,0,32,0,26,0,29,0,2,7,7,7,8,16,0,30,0,40,0,41,3,2,0,38,0,0,8,7,16,0,15,16,22,0,4,11,14,21,0,4,0,23,0,33,0,40,8,0,35,0,43,8,16,22,0,4,12,14,15,13,22,0,5,11,14,21,0,1,0,15,21,0,1,0,12,16,0,23,0,40,17,0,16,21,0,3,0,30,0,2,7,16,17,17,21,0,5,0,18,0,33,0,30,0,0,7,8,3,2,0,37,21,0,3,0,24,0,43,17,0,23,16,8,21,0,4,17,25,21,0,1,0,13,0,42,0,38,0,16,8,0,42,0,38,0,16,8,19,0,51,0,21,21,0,2,17,17,25,0,53,0,16,21,0,1,0,22,16,0,19,0,42,17,0,19,0,40,17,17,0,16,0,52,17,0,17,16,25,21,0,1,0,12,16,0,23,0,40,17,22,0,3,11,14,0,55,21,0,2,0,14,0,3,0,42,19,0,26,0,22,7,0,56,19,0,15,9,21,1,5,3,2,0,37,0,7,0,33,0,45,8,8,16,3,2,0,16,16,0,18,0,42,21,0,3,3,2,17,21,0,1,0,23,0,35,0,57,0,23,0,42,0,1,21,0,2,17,0,4,0,43,17,0,3,0,46,17,17,8,16,0,58,0,18,21,0,3,0,1,16,0,16,0,42,17,17,3,3,0,16,16,25,21,0,1,0,22,16,0,29,0,9,7,0,54,17,0,31,0,1,7,16,0,32,0,0,7,16,0,24,0,44,17,0,23,16,0,6,21,0,1,0,8,16,0,8,0,42,17,17,0,6,21,0,2,0,8,0,42,17,17,25,21,0,1,0,28,0,51,7,16,21,1,7,0,42,17,21,1,10,0,44,17,0,23,0,33,0,43,0,40,3,2,8,0,35,0,61,8,16,25,21,0,1,0,12,16,0,62,0,28,21,1,5,7,0,28,0,64,0,34,0,16,8,7,9,0,16,9,0,23,0,33,0,40,8,0,35,0,63,8,9,3,2,0,37,0,8,0,7,0,42,19,8,16,0,26,0,16,7,0,54,17,0,17,16,25,21,0,1,0,28,0,12,7,16,22,0,3,11,14,21,0,3,0,28,0,23,7,0,42,17,0,15,0,25,0,42,7,0,1,0,9,19,0,39,0,31,0,4,7,8,3,2,0,37,0,9,0,7,0,43,19,8,16,22,0,4,11,14,21,0,3,0,28,0,23,7,16,0,9,0,7,0,43,19,0,39,0,42,0,38,0,31,0,4,7,8,8,16,22,0,5,11,14,21,0,1,0,28,0,18,7,0,42,0,18,21,0,4,0,8,16,0,2,21,0,2,17,0,1,16,17,0,5,21,0,4,17,0,2,21,0,2,17,0,29,0,16,7,21,0,5,17,17,0,9,0,1,0,44,19,0,4,0,43,19,0,39,0,17,8,16,0,44,0,38,0,16,8,16,25,21,0,1,21,1,12,0,42,17,25,21,0,1,0,28,21,1,14,7,16,21,1,12,21,0,2,17,21,1,8,0,44,17,21,1,10,21,0,1,0,9,16,17,25,0,41,0,10,21,0,1,17,22,0,3,11,14,21,0,1,0,9,16,22,0,4,11,14,21,0,1,15,14,15,15,15,16,3,3,0,37,21,0,3,0,15,16,0,30,0,0,7,0,10,0,0,0,7,0,33,0,43,8,19,0,8,19,16,8,16,25,21,0,2,0,0,0,42,17,0,21,16,22,0,3,11,14,0,51,0,15,21,0,1,0,12,16,0,23,0,40,17,17,0,16,21,0,1,17,0,22,21,0,3,0,21,0,33,21,0,3,0,24,16,0,5,16,8,0,35,0,28,0,40,7,8,16,17,25,21,0,1,0,12,16,0,19,0,40,17,22,0,3,11,0,8,16,0,1,16,0,9,0,33,0,43,8,0,39,0,41,8,16,22,0,4,11,14,0,67,22,0,5,11,14,15,17,22,0,6,11,14,21,0,1,0,21,0,33,0,14,0,9,21,0,5,19,0,0,0,42,19,8,21,0,6,3,2,0,37,0,9,0,8,0,42,19,8,16,22,0,1,12,14,21,0,5,0,18,21,0,3,17,0,15,0,35,0,20,8,16,21,0,4,0,38,0,16,8,21,0,1,17,21,0,4,0,38,0,16,8,21,0,5,0,18,21,0,3,17,17,0,17,16,21,1,4,0,8,0,42,17,0,39,21,2,6,21,2,7,0,42,19,21,2,10,21,1,4,19,8,16,25,21,0,1,21,2,13,21,2,11,3,2,0,37,0,12,0,9,0,43,19,0,30,0,6,7,9,8,16,25,21,0,1,21,2,13,0,40,17,25,0,49,0,0,0,47,17,0,7,21,0,1,17,0,2,0,47,0,1,0,69,17,17,0,0,0,48,0,0,0,47,17,0,14,0,1,0,68,19,0,2,0,9,19,21,0,1,17,17,0,0,21,0,1,17,25])
- ,[runtime[0],runtime[1],runtime[2],runtime[6],runtime[7],runtime[9],runtime[11],runtime[12],runtime[14],runtime[15],runtime[16],runtime[18],runtime[19],runtime[20],runtime[21],runtime[22],runtime[23],runtime[24],runtime[25],runtime[26],runtime[28],runtime[30],runtime[33],runtime[34],runtime[37],runtime[41],runtime[42],runtime[43],runtime[44],runtime[45],runtime[47],runtime[48],runtime[49],runtime[52],runtime[53],runtime[54],runtime[55],runtime[56],runtime[57],runtime[59],-1,Infinity,1,0,2,5,4,'
- ,[[0,1,0,0],[1,0,3,15],[0,0,244,6],[0,0,410,3],[0,0,440,3],[0,0,470,4],[0,0,594,3],[0,0,654,3],[0,0,693,3],[0,0,759,6],[0,0,934,3],[0,0,944,3],[0,0,978,5],[0,0,1045,4],[0,0,1113,7],[0,0,1287,3],[0,0,1316,3],[0,0,1326,3]]
+ new Uint8Array([15,1,25,21,0,1,22,0,2,11,14,0,53,0,13,0,16,0,16,19,0,35,0,54,8,3,2,0,39,0,8,0,35,0,43,8,8,21,0,2,3,2,0,39,0,42,0,36,0,10,8,8,0,17,9,22,0,3,11,14,0,9,0,7,0,47,19,0,41,15,2,8,22,0,4,11,14,15,3,22,0,5,11,14,21,0,4,0,36,21,0,5,8,22,0,6,11,14,15,4,15,5,3,2,0,39,15,6,0,45,3,2,0,39,0,8,0,36,0,8,0,35,0,45,8,0,34,0,6,8,8,8,8,22,0,7,11,14,0,65,0,17,0,64,17,15,7,15,8,3,2,0,39,0,8,0,9,0,46,19,0,6,0,9,0,8,0,47,19,19,8,3,2,0,39,0,12,0,12,0,46,0,46,3,2,19,8,22,0,8,11,14,15,9,0,38,15,10,8,22,0,9,11,14,15,11,22,0,10,11,14,15,12,15,13,15,14,3,3,0,39,0,25,0,42,7,0,10,0,15,19,0,33,0,30,0,0,7,0,10,0,0,0,7,0,35,0,46,8,19,0,8,19,8,8,22,0,11,11,14,21,0,3,21,0,11,21,0,10,3,3,0,39,0,11,0,3,0,47,19,8,22,0,12,11,14,21,0,12,0,33,0,27,0,52,0,0,0,43,17,0,36,0,16,8,7,8,0,15,9,0,19,0,44,19,25,21,0,1,0,12,16,0,19,0,44,17,22,0,3,11,14,0,45,0,28,0,18,7,21,0,3,0,4,0,45,17,0,1,16,17,0,20,0,37,0,32,0,26,0,29,0,2,7,7,7,8,16,0,30,0,44,0,42,3,2,0,40,0,0,8,7,16,0,15,16,22,0,4,11,14,21,0,4,0,23,0,35,0,44,8,0,37,0,46,8,16,22,0,4,12,14,15,15,22,0,5,11,14,21,0,1,0,15,21,0,1,0,12,16,0,23,0,44,17,0,16,21,0,3,0,30,0,2,7,16,17,17,21,0,5,0,18,0,35,0,30,0,0,7,8,3,2,0,39,21,0,3,0,24,0,46,17,0,23,16,8,21,0,4,17,25,0,56,0,15,21,0,1,0,8,16,21,0,2,3,2,17,22,0,3,11,14,21,0,3,21,0,1,21,0,3,3,3,0,17,16,0,16,16,25,0,59,0,16,21,0,1,0,22,16,0,19,0,45,17,0,19,0,44,17,17,0,16,0,58,17,0,17,16,25,21,0,1,0,12,16,0,23,0,44,17,22,0,3,11,14,0,60,21,0,2,0,14,0,3,0,45,19,0,26,0,22,7,0,61,19,0,15,9,21,1,2,3,2,0,39,0,7,0,35,0,48,8,8,16,3,2,0,16,16,0,18,21,0,3,17,0,17,16,21,0,1,0,23,0,37,0,62,0,23,0,45,0,1,21,0,2,17,0,4,0,46,17,0,3,0,49,17,17,8,16,0,63,0,18,0,35,0,1,8,21,0,3,17,0,17,16,3,3,0,16,16,25,21,0,1,0,22,16,0,29,0,9,7,0,57,17,0,31,0,1,7,16,0,32,0,0,7,16,0,9,0,47,17,0,30,0,6,7,16,25,21,0,1,0,28,0,55,7,16,21,1,5,0,45,17,21,1,7,0,47,17,0,23,0,35,0,46,0,44,3,2,8,0,37,0,66,8,16,25,21,0,1,0,12,16,0,67,0,28,21,1,2,7,0,28,0,69,0,36,0,16,8,7,9,0,16,9,0,23,0,35,0,44,8,0,37,0,68,8,9,3,2,0,39,0,8,0,7,0,45,19,8,16,0,26,0,16,7,0,57,17,0,17,16,25,21,0,1,0,28,0,12,7,16,22,0,3,11,14,21,0,3,0,28,0,23,7,0,45,17,0,15,0,25,0,45,7,0,1,0,9,19,0,41,0,31,0,4,7,8,3,2,0,39,0,9,0,7,0,46,19,8,16,22,0,4,11,14,21,0,3,0,28,0,23,7,16,0,9,0,7,0,46,19,0,41,0,45,0,40,0,31,0,4,7,8,8,16,22,0,5,11,14,21,0,1,0,28,0,18,7,0,45,0,18,21,0,4,0,8,16,0,2,21,0,2,17,0,1,16,17,0,5,21,0,4,17,0,2,21,0,2,17,0,29,0,16,7,21,0,5,17,17,0,9,0,1,0,47,19,0,4,0,46,19,0,41,0,17,8,16,0,47,0,40,0,16,8,16,25,21,0,1,21,1,9,0,45,17,25,21,0,1,0,28,21,1,12,7,16,21,1,9,21,0,2,17,21,1,6,0,47,17,21,1,7,21,0,1,0,9,16,17,25,21,0,1,0,12,16,0,19,0,44,17,22,0,3,11,0,8,16,0,1,16,0,9,0,35,0,46,8,0,41,0,42,8,16,22,0,4,11,14,0,70,22,0,5,11,14,15,16,22,0,6,11,14,21,0,1,0,21,0,35,0,14,0,9,21,0,5,19,0,0,0,45,19,8,21,0,6,3,2,0,39,0,9,0,8,0,45,19,8,16,22,0,1,12,14,21,0,1,0,9,16,22,0,7,11,14,21,0,5,0,18,0,35,0,1,8,21,0,3,17,21,0,4,0,40,0,16,8,21,0,1,17,21,0,4,0,40,0,16,8,21,0,5,0,18,21,0,3,17,17,0,17,16,21,0,7,0,8,0,45,17,0,41,21,1,4,21,1,5,0,45,19,21,1,7,21,0,7,19,8,16,25,21,0,1,21,1,10,21,1,8,3,2,0,39,0,12,0,9,0,46,19,0,30,0,6,7,9,8,16,25,21,0,1,21,1,10,0,44,17,25,21,0,2,0,0,0,45,17,0,21,16,22,0,3,11,14,0,55,0,15,21,0,1,0,12,16,0,23,0,44,17,17,0,16,21,0,1,17,0,22,21,0,3,0,21,0,35,21,0,3,0,24,16,0,5,16,8,0,37,0,28,0,44,7,8,16,17,25,0,51,0,0,0,43,17,0,7,21,0,1,17,0,2,0,43,0,1,0,72,17,17,0,0,0,50,0,0,0,43,17,0,14,0,1,0,71,19,0,2,0,9,19,21,0,1,17,17,0,0,21,0,1,17,25])
+ ,[runtime[0],runtime[1],runtime[2],runtime[6],runtime[7],runtime[9],runtime[11],runtime[12],runtime[14],runtime[15],runtime[16],runtime[18],runtime[19],runtime[20],runtime[21],runtime[22],runtime[23],runtime[24],runtime[25],runtime[26],runtime[28],runtime[30],runtime[33],runtime[34],runtime[37],runtime[41],runtime[42],runtime[43],runtime[44],runtime[45],runtime[47],runtime[48],runtime[49],runtime[50],runtime[51],runtime[52],runtime[53],runtime[54],runtime[55],runtime[56],runtime[57],runtime[59],Infinity,'
+ ,[[0,1,0,0],[1,1,3,13],[0,0,316,6],[0,0,482,4],[0,0,521,3],[0,0,551,4],[0,0,674,3],[0,0,712,3],[0,0,751,3],[0,0,817,6],[0,0,992,3],[0,0,1002,3],[0,0,1036,8],[0,0,1218,3],[0,0,1247,3],[0,0,1257,4],[0,0,1325,3]]
)(a=>str(fmt0(a)));
let fmt = x => fmt1(x).map(c=>c===0?" ":c).join("");
diff --git a/src/fmt.bqn b/src/fmt.bqn
index 9344042d..3db01657 100644
--- a/src/fmt.bqn
+++ b/src/fmt.bqn
@@ -1,6 +1,8 @@
-# Format an array to a character matrix
+# Format an array to a string including newlines
{
F0←𝔽 # Function to format a scalar
+ FmtAtom ← (≍ ≤⟜∞◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,F0⟩)
+
# Vertical padding for arrays of rank greater than 2
PadV ← {
# Leading shape
@@ -13,52 +15,64 @@
Pad ← {i←/1+𝕨 ⋄ (¯1¨⌾((¬∊i)⊸/)i) ⊏ 𝕩∾(¯1⊑≢𝕩)⥊" "}
p (⊑0∊ls)◶⟨Pad,+´⊸↑⟩ ((×´ls)∾¯1⊑≢𝕩) ⥊ 𝕩
}⍟(2 < =)
+
# Horizontal padding: just some spaces on either side
- PadH ← {
- (𝕨/" ") (∾⎉1∾⎉1⊣) 𝕩
- }
+ PadH ← { s←⟨≠𝕩,𝕨⟩⥊' ' ⋄ ∾≍⟨s,𝕩,s⟩ }
+
Pad ← PadH⟜PadV
- Enf0 ← { ≍"⟨"∾(¯1↓1↓⊏𝕩)∾"⟩" }
- Enframe ← {(1≠𝕨)∨(1≠≠𝕩)∨⊑2∊+`-˝"⟨⟩"=⌜⊏𝕩}◶Enf0‿{
+
+ # Add a frame to padded data
+ Enframe ← ∨○(1⊸≠)⟜≠◶{∨´2=+`-˝"⟨⟩"=⌜⊏𝕩}‿1◶{
+ # One-line version
+ ≍"⟨"∾(¯1↓1↓⊏𝕩)∾"⟩"
+ }‿{
+ # General case
l ← ¯1 ⊑ ≢𝕩
- ∾ ⟨ # "┼╒╛╪"
- 1‿l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,F0⟩𝕨⟩
+ ∾ ⟨
+ ≍l↑∾⟨"┌",(5⊸<)◶⟨⥊"·─"⊏˜1⌊⊢,F0⟩𝕨⟩
((4⌊0⌈𝕨-1)⊑"·╵╎┆┊")⌾⊑ 𝕩
- (1∾-l)↑"┘"
+ ≍l-⊸↑"┘"
}
+
FmtEmpty ← (0‿0≢≢)◶("┌┐"≍"└┘")‿(((2≠=)∨0=≠)◶{
'┐'⌾(0‿¯1⊸⊑) 2 Enframe 1 PadH " "¨𝕩
}‿{
≍"⟨⟩"∾˜(1<≠)◶⟨"",'⥊'⌾(¯1⊸⊑)·∾·∾⟜"‿"¨F0¨⟩≢𝕩
})
+
PaddingJoin ← {1 PaddingJoin 𝕩}⊘{
s ← ≢¨ 𝕩
w ← (0<=)◶⟨⥊,⌈˝⍟(=-1˙)⟩1⊑¨s
h ← ⌈˝⎉1⍟(0<=) ⊑¨s
∾⎉2 ≍⍟(0⌈2-=) (h ∾⌜ 𝕨×w¬(-𝕨×≠w)↑1) ↑¨ 𝕩
}
+
FmtMixed ← {
- (=𝕩) Enframe 2 Pad 𝕨 PaddingJoin F¨𝕩
+ (=𝕩) Enframe 2 Pad 𝕨 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) ≍ (c↑q) ∾⎉k 𝕩 ∾⎉k c-⊸↑q
+ }‿{
+ # Not homogeneous, or empty
+ (∨´0=≢)◶FmtMixed‿FmtEmpty 𝕩
+ }‿{
+ # All numbers
+ ¯1 FmtMixed 𝕩
}
- F ← (2⌊≡)◶(≍≤⟜∞◶⟨@⊸≠◶⟨"@","'"⊸(∾∾⊣)⟩,F0⟩)‿{
- num ← 𝕩≤∞ ⋄ r ← =𝕩
- ((≠(0⊸<+≤)+´)⥊num)◶{
- # 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 Enframe 1 PadH PadV)⍟(1≠r) ≍ (c↑q) ∾⎉k 𝕩 ∾⎉k ⌽⌾⥊c↑q
- }‿{
- # Not homogeneous, or empty
- (∨´0=≢)◶FmtMixed‿FmtEmpty 𝕩
- }‿{
- # All numbers
- ¯1 FmtMixed 𝕩
- } 𝕩
- }‿FmtMixed
- ¯1↓⥊ ∾⟜(@+10)˘ F 𝕩
+
+ # Format to character matrix
+ Fmt ← (2⌊≡)◶FmtAtom‿FmtSimple‿FmtMixed
+
+ # Convert to string
+ ¯1↓·⥊ ∾⟜(@+10)˘∘Fmt
}