luapack/plug/switch.lua

32 lines
766 B
Lua

-- 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