编辑代码


function _loop_()
    local i = 0
    return function()
        if i > 1000000 then
            error('循环次数太多')
        end
        i = i + 1
        return true
    end
end

--- 处理额外检查数据 ${lueCode}/#tableKey#
local function loadParse(data, chat, spare, notReturn)
    chat = tostring(chat):gsub("'?$%{(.-)%}'?", function(k)
        local code = not notReturn and 'return ' .. k or k
        local func = load(code, '', 'bt', data)
        return func()
    end)
    for _ in _loop_() do
        local bool = false
        chat = chat:gsub("#(.-)#", function(k)
            bool = true
            return data[k] or spare[k] or ''
        end)
        if not bool then
            break
        end
    end
    return chat
end

--- 解析数据
local function parse(data, chat, notReturn)
    if chat then
        local spare = data.data or data._data
        spare = type(spare) == 'table' and spare or data
        chat = loadParse(data, chat, spare, notReturn)
        for _ in _loop_() do
            local bool = false
            chat = chat:gsub("'(.-)'", function(k)
                bool = true
                k = data[k] or spare[k] or ''
                return loadParse(data, k, spare, notReturn)
            end)
            if not bool then
                break
            end
        end
    end
    return chat
end

local data = {
    ubertip = [[
        这里是level数据:'level * 50'
        这里是name|2索引:'name_#level#'
        这里是table数据:${value[1]}
        这里是简单算式运算:${level * 50}
    ]],
    level = 2,
    name_1 = 100,
    name_2 = 200,
    value = {
        2000, 4000
    },
}

print(parse(data, data.ubertip))