1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# Format an array to a string including newlines
{
β¨Type,Decomp,FF,FNβ©βπ # Format a number or operation to a string
FmtAtom β β <β@βΆβ¨@βΈβ βΆβ¨"@","'"βΈ(βΎβΎβ£)β©,FNβ©
# 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ββ’,FNβ©π¨β©
((0β4βπ¨-1)β"Β·β΅βββ")βΎβ π©
βl-βΈβ"β"
β©
}
FmtEmpty β (0βΏ0β’β’)βΆ("ββ"β"ββ")βΏ(((2β =)β¨0=β )βΆ{
'β'βΎ(0βΏΒ―1βΈβ) 2 Enframe 1 PadH " "Β¨π©
}βΏ{
β(1<β )βΆβ¨"β¨β©",'β'βΎβΒ·βΎΒ·"βΏ"βΈβΎΒ¨FNΒ¨β©β’π©
})
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
r β =π© β π© β© 0βπ©
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) β (qβΎβcβ₯(Β―1β1βΎc)β'Β·') βΎβk π© βΎβk c-βΈβq
}βΏ{
# Not homogeneous, or empty
(β¨Β΄0=β’)βΆFmtMixedβΏFmtEmpty π©
}βΏ{
# All numbers
Β―1 FmtMixed π©
}
# Format data type
FmtDat β (2ββ‘)βΆFmtAtomβΏFmtSimpleβΏFmtMixed
# Format part of a compound operation; return precedenceβΏstring
FmtOp β {
tn β '*'(βΎβΎβ£)Β¨"array"βΏ"function"βΏ"1-modifier"βΏ"2-modifier"
FmtComp β Type (3β€β£)βΆβ¨0ββ<(1=β )βΆβ¨βtn,ββ©βFmtDatββ’, FmtOpβ© β’
k β β d β Decomp π©
p β k β "00321111"-'0'
Paren β "("βΎβΎβ")"
FromComp β βΎ(β½β(p>1)Β·-0=βββ )(+ββParenββ’β((2βp)β€β£)1βΈβ)βFmtCompΒ¨β’
s β (2βk)βΆβ¨β₯βFFπ©Λ,(π¨-2)βΆtn,FromCompβ© 1βd
pβΏs
}
# Format any value to character matrix
Fmt β Type (3β€β£)βΆβ¨FmtDatβ’,β1βFmtOpβ© β’
# Convert to string
Β―1βΒ·β₯ βΎβ(@+10)ΛβFmt
}
|