/
test_zpp.hlua
executable file
·193 lines (149 loc) · 5.12 KB
/
test_zpp.hlua
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
local M = require'zpp'
-- layout
local layout = M.layout
-- Basic
local isDoc = M.isDoc
local empty = M.empty
local text = M.text
local nest = M.nest
local group = M.group
local column = M.column
local nesting = M.nesting
local line = M.line -- \n | space
local linebreak = M.linebreak -- \n | empty
local softline = M.group(line)
local softbreak = M.group(linebreak)
local space = M.space
local cat = M.cat -- <>
local catspace = M.catspace -- <+>
local abovespace = M.abovespace -- <$>
local above = M.above -- <$$>
-- Primitives:
local lparen = M.lparen
local rparen = M.rparen
local langle = M.langle
local rangle = M.rangle
local lbrace = M.lbrace
local rbrace = M.rbrace
local lbracket = M.lbracket
local rbracket = M.rbracket
local backtick = M.backtick
local squote = M.squote
local dquote = M.dquote
local semi = M.semi
local colon = M.colon
local comma = M.comma
local dot = M.dot
local backslash = M.backslash
local assign = M.assign
-- Combinators
local ifoldDoc = M.ifoldDoc
local iseparateWith = M.iseparateWith
-- PPrint's combinators
local indent = M.indent
local align = M.align
local hang = M.hang
local fill = M.fill
local fillBreak = M.fillBreak
-- Misc
local compose = M.compose
local identity = M.identity
local insert = table.insert
--------------------------------------------------------------------------
-- TEST: --
--------------------------------------------------------------------------
--------------------------------------------------------------------------
local rep_n = fun(n,word)->
local result = {}
for i=1,n do result `insert` word end
return result
end
local fun enumerate(n:'num') ->
local res = {}
for i=1,n do res `insert` tostring(i) end
return res
end
local function test()
--------------------------------------------------------------------------
local function imap(tab,proc)
local result = {}
for i,v in ipairs(tab) do result[i] = proc(tab[i]) end
return result
end
local nest4 = fn(d) => nest(4,d) end
local words = fun(n) ->
iseparateWith(line,text)(rep_n(n,'foobar'))
end
local groupit = fn(x) => group(text"<"+x+text">")end
local fun enclose(l,r) ->
fun (body) -> group(l+nest4(line+body) / r) end
end
local fun fillSep(xs@{_:'str',...}) ->
iseparateWith(group(line),text)(xs)
end
local semisep = fun(strings@{_:'str',...}) ->
iseparateWith(text";"+line,text)(strings)
end
local brackets = fn(x) => enclose(text"{",text"}")(x) end
local ifthen =
fn(cond,body) => text"if("+cond+text")"+brackets(body) end
--------------------------------------------------------------------------
print(isDoc$groupit(words(4)))
print$brackets(semisep(rep_n(4,"Abracadabra")))
print$brackets(semisep(rep_n(2,"Abracadabra")))
print$ifthen(text"x != NULL",text'println("I`am short!");')
print$nest(8,(line+ifthen(text"x != NULL && align != NULL",
text'println("I`am aligned!");')))
print$ifthen(text"x != NULL && y != NULL && z != NULL",
text'println("Hello, Cruel World!");')
print$ nest(20,(linebreak+fillSep(enumerate(100))))
print$ fillSep(enumerate(100))
print$layout(hang(#~"hang"+1,fillSep(rep_n(100,"hang"))),72,1)
print$layout(indent(40,fillSep(rep_n(20,"indent"))),72,1)
-- fillers
local vcat = iseparateWith(linebreak,identity)
local data =
{{'empty',"Doc"};{"nest","Int -> Doc -> Doc"};{"linebreak","Doc"}}
local d1 = vcat$imap(data,fn(x)=>
fill(6, text(x[1]))-text"::"-text(x[2])end)
local d2 = vcat$imap(data,fn(x)=>
fillBreak(6,text(x[1]))-text"::"-text(x[2])end)
print$text"let"-align(d1)
print''
print$text"let"-align(d2)
-- NOTE: WAS BUG: group(align(x))
print$group(align(empty+text"++++++++++++"))
-- fillSep by ifoldDoc
print$ifoldDoc(fn(seed,x)=>seed+group(line)+x end)(rep_n( 0,text"fill"))
print$ifoldDoc(fn(seed,x)=>seed+group(line)+x end)(rep_n( 1,text"fill"))
print$ifoldDoc(fn(seed,x)=>seed+group(line)+x end)(rep_n( 2,text"fill"))
-- should be 3x9
print$ifoldDoc(fn(seed,x)=>seed+group(line)+x end)(rep_n(27,text"fill"))
local N = 1E4
print'++++++++++++'
-- * --
collectgarbage('collect')
local Ndocs=enumerate(N)
for i=1,#Ndocs do
Ndocs[i]=text(Ndocs[i])
end
local t3 = os.clock()
local softbr = group(line)
local XXX = ifoldDoc(fn(seed,x)=>seed+(softbr+x)end)(Ndocs)
layout(XXX,78,0.8)
local t4 = os.clock()
print('filling-fold '..N.." numbers:",t4-t3,"sec")
-- * --
-- * --
collectgarbage('collect')
local t1 = os.clock()
local XX = fillSep(enumerate(N))
layout(XX,78,0.8)
local t2 = os.clock()
print('filling '..N.." numbers:",t2-t1,"sec")
-- * --
local data17 = enumerate(17) -- 10 + 7
print(layout(iseparateWith(softline,text)(data17),20,1))
end -- of test
-- do test:
test()