107
108
109
110
111
112
113
114
115
116
117
118
119
120
...
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
...
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
...
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
end
struct m.acc {
buf: rawstring
sz: intptr
run: intptr
space: intptr
}
terra m.cdowncase(c: int8)
if c >= @'A' and c <= @'Z' then
return c + (@'a' - @'A')
else return c end
end
................................................................................
local terra biggest(a: intptr, b: intptr)
if a > b then return a else return b end
end
terra m.acc:init(run: intptr)
--lib.dbg('initializing string accumulator')
if run == 0 then
lib.warn('attempted to allocate zero-length string accumulator')
self.buf = nil
else
self.buf = [rawstring](lib.mem.heapa_raw(run))
if self.buf == nil then
lib.warn('string buffer allocation failed, very little memory availble')
................................................................................
end
end
self.run = lib.trn(self.buf == nil, 0, run)
self.space = self.run
self.sz = 0
return self
end;
terra m.acc:free()
--lib.dbg('freeing string accumulator')
if self.buf ~= nil and self.space > 0 then
lib.mem.heapf(self.buf)
end
end;
terra m.acc:crush()
--lib.dbg('crushing string accumulator')
self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.sz))
self.space = self.sz
return self
end;
terra m.acc:finalize()
--lib.dbg('finalizing string accumulator')
................................................................................
self.buf = nil
self.sz = 0
return pt
end;
terra m.acc:cue(sz: intptr)
if sz <= self.run then return end
self.run = sz
if self.space - self.sz < self.run then
self.space = self.sz + self.run
self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.space))
end
end
terra m.acc:reset() -- semantic convenience function
self.sz = 0
end
................................................................................
if str == nil then return self end
--if str[len - 1] == 0xA then llen = llen - 1 end -- don't display newlines in debug output
-- lib.dbg('pushing "',{str,llen},'" onto accumulator')
if self.buf == nil then self:init(self.run) end
if self.buf == nil then lib.warn('attempted to push string onto unallocated accumulator') return self end
if len == 0 then len = m.sz(str) end
if len >= self.space - self.sz then
self.space = self.space + biggest(self.run,len + 1)
self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.space))
end
lib.mem.cpy(self.buf + self.sz, str, len)
self.sz = self.sz + len
self.buf[self.sz] = 0
return self
end;
................................................................................
return `self:push([str:asvalue()], [#(str:asvalue())]) end)
m.acc.methods.ppush = terra(self: &m.acc, str: lib.mem.ptr(int8))
self:push(str.ptr, str.ct) return self end;
m.acc.methods.rpush = terra(self: &m.acc, str: lib.mem.ref(int8))
self:push(str.ptr, str.ct) return self end;
m.acc.methods.merge = terra(self: &m.acc, str: lib.mem.ptr(int8))
self:push(str.ptr, str.ct) str:free() return self end;
m.acc.methods.compose = macro(function(self, ...)
local minlen = 0
local pstrs = {}
for i,v in ipairs{...} do
if type(v) == 'table' then
local gl = 16 -- guess wildly
if v.tree and v.tree.type.convertible == 'tuple' then
pstrs[#pstrs+1] = {str = `v._0, len = `v._1}
................................................................................
else pstrs[#pstrs+1] = {str = v, len = 0} end
minlen = minlen + gl
elseif type(v) == 'string' then
pstrs[#pstrs+1] = {str = v, len = #v}
minlen = minlen + #v + 1
else error('invalid type in compose expression') end
end
local call = `self:init(minlen)
for i,v in ipairs(pstrs) do
call = `[call]:push([v.str],[v.len])
end
return call
end)
m.acc.metamethods.__lshift = terralib.overloadedfunction('(<<)', {
terra(self: &m.acc, str: rawstring) return self: push(str,0) end;
terra(self: &m.acc, str: lib.mem.ptr(int8)) return self:ppush(str ) end;
})
m.box = terralib.memoize(function(ty)
local b = struct {
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
|
|
|
|
|
>
>
>
>
>
>
>
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
...
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
...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
...
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
end
struct m.acc {
buf: rawstring
sz: intptr
run: intptr
space: intptr
pool: &lib.mem.pool
}
terra m.cdowncase(c: int8)
if c >= @'A' and c <= @'Z' then
return c + (@'a' - @'A')
else return c end
end
................................................................................
local terra biggest(a: intptr, b: intptr)
if a > b then return a else return b end
end
terra m.acc:init(run: intptr)
--lib.dbg('initializing string accumulator')
self.pool = nil
if run == 0 then
lib.warn('attempted to allocate zero-length string accumulator')
self.buf = nil
else
self.buf = [rawstring](lib.mem.heapa_raw(run))
if self.buf == nil then
lib.warn('string buffer allocation failed, very little memory availble')
................................................................................
end
end
self.run = lib.trn(self.buf == nil, 0, run)
self.space = self.run
self.sz = 0
return self
end;
terra m.acc:pool(pool: &lib.mem.pool, run: intptr)
self.buf = [&int8](pool:alloc_bytes(run))
self.pool = pool
self.run = run
self.space = self.run
self.sz = 0
return self
end
terra m.acc:free()
--lib.dbg('freeing string accumulator')
if self.pool ~= nil then
lib.dbg('attempted to free pooled string accumulator; use frame-reset instead')
return
end
if self.buf ~= nil and self.space > 0 then
lib.mem.heapf(self.buf)
end
end;
terra m.acc:crush()
--lib.dbg('crushing string accumulator')
if self.pool ~= nil then return self end -- no point unless at end of buffer
self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.sz))
self.space = self.sz
return self
end;
terra m.acc:finalize()
--lib.dbg('finalizing string accumulator')
................................................................................
self.buf = nil
self.sz = 0
return pt
end;
terra m.acc:cue(sz: intptr)
if sz <= self.run then return end
var curspace = self.space
self.run = sz
if self.space - self.sz < self.run then
self.space = self.sz + self.run
if self.pool ~= nil then
self.buf = [&int8](self.pool:realloc_bytes(self.buf, curspace, self.space))
else
self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.space))
end
end
end
terra m.acc:reset() -- semantic convenience function
self.sz = 0
end
................................................................................
if str == nil then return self end
--if str[len - 1] == 0xA then llen = llen - 1 end -- don't display newlines in debug output
-- lib.dbg('pushing "',{str,llen},'" onto accumulator')
if self.buf == nil then self:init(self.run) end
if self.buf == nil then lib.warn('attempted to push string onto unallocated accumulator') return self end
if len == 0 then len = m.sz(str) end
if len >= self.space - self.sz then
self:cue(self.space + biggest(self.run,len + 1))
--self.space = self.space + biggest(self.run,len + 1)
--self.buf = [rawstring](lib.mem.heapr_raw(self.buf, self.space))
end
lib.mem.cpy(self.buf + self.sz, str, len)
self.sz = self.sz + len
self.buf[self.sz] = 0
return self
end;
................................................................................
return `self:push([str:asvalue()], [#(str:asvalue())]) end)
m.acc.methods.ppush = terra(self: &m.acc, str: lib.mem.ptr(int8))
self:push(str.ptr, str.ct) return self end;
m.acc.methods.rpush = terra(self: &m.acc, str: lib.mem.ref(int8))
self:push(str.ptr, str.ct) return self end;
m.acc.methods.merge = terra(self: &m.acc, str: lib.mem.ptr(int8))
self:push(str.ptr, str.ct) str:free() return self end;
local composefn = function(call, ...)
local minlen = 0
local pstrs = {}
for i,v in ipairs{...} do
if type(v) == 'table' then
local gl = 16 -- guess wildly
if v.tree and v.tree.type.convertible == 'tuple' then
pstrs[#pstrs+1] = {str = `v._0, len = `v._1}
................................................................................
else pstrs[#pstrs+1] = {str = v, len = 0} end
minlen = minlen + gl
elseif type(v) == 'string' then
pstrs[#pstrs+1] = {str = v, len = #v}
minlen = minlen + #v + 1
else error('invalid type in compose expression') end
end
call = call(minlen) --`self:init(minlen)
for i,v in ipairs(pstrs) do
call = `[call]:push([v.str],[v.len])
end
return call
end
m.acc.methods.compose = macro(function(self, ...)
return composefn(function(minlen) return `self:init(minlen) end, ...)
end)
m.acc.methods.pcompose = macro(function(self, pool, ...)
return composefn(function(minlen) return `self:pool(pool,minlen) end, ...)
end)
m.acc.metamethods.__lshift = terralib.overloadedfunction('(<<)', {
terra(self: &m.acc, str: rawstring) return self: push(str,0) end;
terra(self: &m.acc, str: lib.mem.ptr(int8)) return self:ppush(str ) end;
})
m.box = terralib.memoize(function(ty)
local b = struct {
|