1
2
3
4
5
6
7
8
9
10
11
12
13
..
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
..
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
..
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
|
-- vim: ft=terra
-- string template generator:
-- returns a function that fills out a template
-- with the strings given
local util = lib.util
local m = {}
function m.mk(tplspec)
local str
if type(tplspec) == 'string'
then str = tplspec tplspec = {}
else str = tplspec.body
end
................................................................................
str = str:gsub('%s+[\n$]','')
str = str:gsub('\n','')
str = str:gsub('</a><a ','</a> <a ') -- keep nav links from getting smooshed
str = str:gsub(tplchar .. '%?([-%w]+)', function(file)
if not docs[file] then docs[file] = data.doc[file] end
return string.format('<a href="#help-%s" class="help">?</a>', file)
end)
for start, key, stop in string.gmatch(str,'()'..tplchar..'(%w+)()') do
if string.sub(str,start-1,start-1) ~= '\\' then
segs[#segs+1] = string.sub(str,last,start-1)
fields[#segs] = key
last = stop
end
end
segs[#segs+1] = string.sub(str,last)
for i, s in ipairs(segs) do
segs[i] = string.gsub(s, '\\'..tplchar, tplchar_o)
................................................................................
local runningtally = symbol(intptr)
local tallyup = {quote
var [runningtally] = 1 + constlen
end}
local rec = terralib.types.newstruct(string.format('template<%s>',tplspec.id or ''))
local symself = symbol(&rec)
do local kfac = {}
for afterseg,key in pairs(fields) do
if not kfac[key] then
rec.entries[#rec.entries + 1] = {
field = key;
type = lib.mem.ptr(int8);
}
end
kfac[key] = (kfac[key] or 0) + 1
end
for key, fac in pairs(kfac) do
tallyup[#tallyup + 1] = quote
[runningtally] = [runningtally] + ([symself].[key].ct)*fac
end
end
end
local copiers = {}
local senders = {}
local appenders = {}
................................................................................
local cpypos = symbol(&opaque)
local accumulator = symbol(&lib.str.acc)
local destcon = symbol(&lib.net.mg_connection)
for idx, seg in ipairs(segs) do
copiers[#copiers+1] = quote [cpypos] = lib.mem.cpy([cpypos], [&opaque]([seg]), [#seg]) end
senders[#senders+1] = quote lib.net.mg_send([destcon], [seg], [#seg]) end
appenders[#appenders+1] = quote [accumulator]:push([seg], [#seg]) end
if fields[idx] then
--local fsz = `lib.str.sz(symself.[fields[idx]])
local fval = `symself.[fields[idx]].ptr
local fsz = `symself.[fields[idx]].ct
copiers[#copiers+1] = quote
[cpypos] = lib.mem.cpy([cpypos], [&opaque]([fval]), [fsz])
end
senders[#senders+1] = quote
lib.net.mg_send([destcon], [fval], [fsz])
end
appenders[#appenders+1] = quote
[accumulator]:push([fval], [fsz])
end
end
end
local tid = tplspec.id or '<anonymous>'
rec.methods.tostr = terra([symself])
lib.dbg(['compiling template ' .. tid])
[tallyup]
var [symtxt] = lib.mem.heapa(int8, [runningtally])
var [cpypos] = [&opaque](symtxt.ptr)
[copiers]
@[&int8](cpypos) = 0
return symtxt
end
rec.methods.append = terra([symself], [accumulator])
lib.dbg(['appending template ' .. tid])
[tallyup]
accumulator:cue([runningtally])
[appenders]
|
>
|
|
>
|
|
|
|
>
>
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
|
|
>
>
|
|
>
|
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
..
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
..
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
|
-- vim: ft=terra
-- string template generator:
-- returns a function that fills out a template
-- with the strings given
local util = lib.util
local pstr = lib.mem.ptr(int8)
local m = {}
function m.mk(tplspec)
local str
if type(tplspec) == 'string'
then str = tplspec tplspec = {}
else str = tplspec.body
end
................................................................................
str = str:gsub('%s+[\n$]','')
str = str:gsub('\n','')
str = str:gsub('</a><a ','</a> <a ') -- keep nav links from getting smooshed
str = str:gsub(tplchar .. '%?([-%w]+)', function(file)
if not docs[file] then docs[file] = data.doc[file] end
return string.format('<a href="#help-%s" class="help">?</a>', file)
end)
for start, mode, key, stop in string.gmatch(str,'()'..tplchar..'([:!]?)(%w+)()') do
if string.sub(str,start-1,start-1) ~= '\\' then
segs[#segs+1] = string.sub(str,last,start-1)
fields[#segs] = { key = key, mode = (mode ~= '' and mode or nil) }
last = stop
end
end
segs[#segs+1] = string.sub(str,last)
for i, s in ipairs(segs) do
segs[i] = string.gsub(s, '\\'..tplchar, tplchar_o)
................................................................................
local runningtally = symbol(intptr)
local tallyup = {quote
var [runningtally] = 1 + constlen
end}
local rec = terralib.types.newstruct(string.format('template<%s>',tplspec.id or ''))
local symself = symbol(&rec)
do local kfac = {}
local sanmode = {}
for afterseg,fld in ipairs(fields) do
if not kfac[fld.key] then
rec.entries[#rec.entries + 1] = {
field = fld.key;
type = lib.mem.ptr(int8);
}
end
kfac[fld.key] = (kfac[fld.key] or 0) + 1
sanmode[fld.key] = fld.mode == ':' and 6 or fld.mode == '!' and 5 or 1
end
for key, fac in pairs(kfac) do
local sanfac = sanmode[key]
tallyup[#tallyup + 1] = quote
[runningtally] = [runningtally] + ([symself].[key].ct)*fac*sanfac
end
end
end
local copiers = {}
local senders = {}
local appenders = {}
................................................................................
local cpypos = symbol(&opaque)
local accumulator = symbol(&lib.str.acc)
local destcon = symbol(&lib.net.mg_connection)
for idx, seg in ipairs(segs) do
copiers[#copiers+1] = quote [cpypos] = lib.mem.cpy([cpypos], [&opaque]([seg]), [#seg]) end
senders[#senders+1] = quote lib.net.mg_send([destcon], [seg], [#seg]) end
appenders[#appenders+1] = quote [accumulator]:push([seg], [#seg]) end
if fields[idx] and fields[idx].mode then
local f = fields[idx]
local fp = `symself.[f.key]
copiers[#copiers+1] = quote
if fp.ct > 0 then
var san = lib.html.sanitize(fp, [f.mode == ':'])
[cpypos] = lib.mem.cpy([cpypos], [&opaque](san.ptr), san.ct)
--san:free()
end
end
senders[#senders+1] = quote
if fp.ct > 0 then
var san = lib.html.sanitize(fp, [f.mode == ':'])
lib.net.mg_send([destcon], san.ptr, san.ct)
--san:free()
end
end
appenders[#appenders+1] = quote
if fp.ct > 0 then
var san = lib.html.sanitize(fp, [f.mode == ':'])
[accumulator]:ppush(san)
--san:free()
end
end
elseif fields[idx] then
local f = fields[idx]
local fp = `symself.[f.key]
copiers[#copiers+1] = quote
if fp.ct > 0 then
[cpypos] = lib.mem.cpy([cpypos], [&opaque](fp.ptr), fp.ct)
end
end
senders[#senders+1] = quote
if fp.ct > 0 then
lib.net.mg_send([destcon], fp.ptr, fp.ct)
end
end
appenders[#appenders+1] = quote
if fp.ct > 0 then [accumulator]:ppush(fp) end
end
end
end
local tid = tplspec.id or '<anonymous>'
rec.methods.tostr = terra([symself])
lib.dbg(['compiling template ' .. tid])
[tallyup]
var [symtxt] = lib.mem.heapa(int8, [runningtally])
var [cpypos] = [&opaque](symtxt.ptr)
[copiers]
@[&int8](cpypos) = 0
symtxt.ct = [&int8](cpypos) - symtxt.ptr
return symtxt
end
rec.methods.append = terra([symself], [accumulator])
lib.dbg(['appending template ' .. tid])
[tallyup]
accumulator:cue([runningtally])
[appenders]
|