init
This commit is contained in:
commit
8d306ab690
14 changed files with 703 additions and 0 deletions
17
plug/arrow.lua
Normal file
17
plug/arrow.lua
Normal file
|
@ -0,0 +1,17 @@
|
|||
-- Arrow functions in Lua
|
||||
-- f = (n) => 2 + n
|
||||
|
||||
return function(op)
|
||||
return {
|
||||
name = 'Arrow functions',
|
||||
transform = function(e, opts)
|
||||
return e.src
|
||||
-- multiple-line
|
||||
:gsub('%(([^%(%)]*)%)%s*==>%s*(.-)%s<==',
|
||||
'function(%1) %2 end')
|
||||
-- single-line
|
||||
:gsub('%(([^%(%)]*)%)%s*=>[ \t\v\f]*([^\n\r]*)',
|
||||
'function(%1) return %2 end')
|
||||
end,
|
||||
}
|
||||
end
|
16
plug/assigns.lua
Normal file
16
plug/assigns.lua
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
local reg = '([%%w_%%.%%[%%]]+)%%s*(%s)=%%s*'
|
||||
local regv = '%1 = %1 %2 '
|
||||
|
||||
return function(op)
|
||||
return {
|
||||
name = 'Assigments',
|
||||
transform = function(e, opts)
|
||||
return e.src
|
||||
:gsub(reg:format '[%+%-%*%/%%^|&]', regv)
|
||||
:gsub(reg:format '%.%.', regv)
|
||||
:gsub(reg:format 'and', regv)
|
||||
:gsub(reg:format 'or', regv)
|
||||
end,
|
||||
}
|
||||
end
|
108
plug/lua.lua
Normal file
108
plug/lua.lua
Normal file
|
@ -0,0 +1,108 @@
|
|||
|
||||
lp.insPath {
|
||||
'?.lua',
|
||||
'?/init.lua',
|
||||
}
|
||||
|
||||
local env = {
|
||||
name = lp.name,
|
||||
ver = lp.version,
|
||||
copy = lp.copyright,
|
||||
|
||||
cache = '_c_',
|
||||
req = '_r_',
|
||||
mreq = '_m_', -- require by map
|
||||
mtemp = ' function($req) -- $file\n$src\n end, -- $file\n',
|
||||
pre = '--[=[=]=]',
|
||||
regex = '([^\n]-)' .. 'require%s*%(?["\'](.-)["\']%)?%s*',
|
||||
}
|
||||
|
||||
env.require = lp.strf('$req(%d)', env)
|
||||
|
||||
local base = lp.strf([[
|
||||
$pre -- $name $ver
|
||||
$pre -- $copy
|
||||
$pre (function(m)
|
||||
$pre -- Module cache
|
||||
$pre local $cache = {}
|
||||
%s$pre
|
||||
$pre -- Load main module
|
||||
$pre $require
|
||||
$pre
|
||||
$pre end) {
|
||||
%s
|
||||
$pre }
|
||||
]], env)
|
||||
|
||||
local main = lp.strf([[
|
||||
$pre
|
||||
$pre -- Require function
|
||||
$pre local function $req(i)
|
||||
$pre -- Check in cache
|
||||
$pre local c = $cache[i]
|
||||
$pre if not c then
|
||||
$pre -- Execute module function
|
||||
$pre c = m[i]($req)
|
||||
$pre
|
||||
$pre -- And put it to cache
|
||||
$pre $cache[i] = c
|
||||
$pre end
|
||||
$pre
|
||||
$pre -- Return cached returned value
|
||||
$pre return c
|
||||
$pre end
|
||||
]], env)
|
||||
|
||||
local mreq = lp.strf([[
|
||||
$pre
|
||||
$pre -- Require by map
|
||||
$pre local function $mreq(m, r)
|
||||
$pre -- Check if module exists
|
||||
$pre if not m[r]
|
||||
$pre then error('Cannot find module "'..r..'"')
|
||||
$pre end
|
||||
$pre
|
||||
$pre -- Using default require
|
||||
$pre return $req(m[r])
|
||||
$pre end
|
||||
]], env)
|
||||
|
||||
return function(op)
|
||||
return {
|
||||
name = 'Base Lua plugin',
|
||||
load = function(e, ext, oext)
|
||||
if ext == 'lua'
|
||||
then return true -- load every lua file
|
||||
end
|
||||
end,
|
||||
deps = function(e, opts)
|
||||
return e.src:gsub(env.regex, function(bef, name)
|
||||
name = name:gsub('%.', '/') -- lua.lua => lua/lua
|
||||
if bef:match '%-%-'
|
||||
then return '' end -- under comment
|
||||
local d = lp.get(name, e, opts)
|
||||
if d then
|
||||
return bef.. env.require:format(d.i)
|
||||
end
|
||||
end)
|
||||
end,
|
||||
transform = function(e, opts)
|
||||
if not e.entry then return end -- change only entry files
|
||||
if e.reqs > 0 then
|
||||
local m = ''
|
||||
for i = 1, #opts.ext do
|
||||
local v = opts.ext[opts.ext[i]] -- [i] => [file] => object
|
||||
m = m.. lp.strf(lp.strf(env.mtemp, env), v)
|
||||
end
|
||||
return base:format(main, opts.ext[e.file].i, m)
|
||||
end
|
||||
end,
|
||||
postbuild = function(e, opts)
|
||||
local f, err = load(e.src, 'lint')
|
||||
if err then
|
||||
print(e.src)
|
||||
error(err)
|
||||
end
|
||||
end,
|
||||
}
|
||||
end
|
28
plug/num.lua
Normal file
28
plug/num.lua
Normal file
|
@ -0,0 +1,28 @@
|
|||
|
||||
return function(op)
|
||||
return {
|
||||
name = 'Better numbers',
|
||||
transform = function(e, opts)
|
||||
return e.src
|
||||
:gsub('%.?%d[%._0-9A-Fa-fxboXBO]+', function(num)
|
||||
local base = 10
|
||||
local nnum = num
|
||||
:gsub('_', '')
|
||||
:gsub('^0([xboXBO])', function(m)
|
||||
if m == 'x' or m == 'X'
|
||||
then base = 16
|
||||
elseif m == 'b' or m == 'B'
|
||||
then base = 2
|
||||
elseif m == 'o' or m == 'O'
|
||||
then base = 8
|
||||
end
|
||||
return ''
|
||||
end)
|
||||
assert(not nnum:match '[xboXBO]', 'invalid number '..num)
|
||||
nnum = tonumber(nnum, base)
|
||||
assert(nnum, 'invalid number '..num)
|
||||
return tostring(nnum)
|
||||
end)
|
||||
end,
|
||||
}
|
||||
end
|
31
plug/switch.lua
Normal file
31
plug/switch.lua
Normal file
|
@ -0,0 +1,31 @@
|
|||
|
||||
-- BROKEN
|
||||
|
||||
local reg = 'switch%s*%(([^{}]-)%)%s*{(.-)}'
|
||||
local regv = '%s*([^=>]+)%s*=>%s*(.-);'
|
||||
|
||||
return function(op)
|
||||
return {
|
||||
name = 'Switch-case',
|
||||
transform = function(e, opts)
|
||||
return e.src:gsub(reg, function(case, code)
|
||||
local st
|
||||
return code:gsub(regv, function(when, c)
|
||||
local code = st and 'elseif' or 'if'
|
||||
st = true
|
||||
if when == '_'
|
||||
then code = 'else '
|
||||
else local st
|
||||
for v in when:gmatch '%s*([^,]+)' do
|
||||
code = code .. (st and ' or' or '')
|
||||
.. (' %s == %s'):format(case, v)
|
||||
st = true
|
||||
end
|
||||
code = code .. ' then '
|
||||
end
|
||||
return code .. c
|
||||
end) .. ' end'
|
||||
end)
|
||||
end,
|
||||
}
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue