Might I suggest making the second parameter optional, and if no size/range is given it defaults to a single instruction and so you use
getInstructionSize
inside the function to determine size/range.
Also you should always format your code, having everything on a single indent tends to look very messy.
For example something like this is almost unreadable:
Code: Select all
local function compile(template, environmentOrLoadLocalEnv, loadLocalEnv)
---- Turn the template into a string that can be run though Lua.
---- Builder will be used to efficiently build the string we'll run.
---- The string will use it's own builder (_ret). Each part that comprises
---- _ret will be the various pieces of the template. Strings, variables
---- that should be printed and functions that should be run.
if not template or template == '' then return '' end
local env = { }
if type(environmentOrLoadLocalEnv) == 'table' then
env = environmentOrLoadLocalEnv
elseif type(environmentOrLoadLocalEnv) == 'boolean' then
loadLocalEnv = environmentOrLoadLocalEnv
end
env.__index = env
setmetatable(env, { __index = _G })
if loadLocalEnv then
local i = 1
while true do
local name, value = debug.getlocal(2, i)
if not name then break end
env[name] = value
i = i + 1
end
end
local builder = { '_ret = {}\n' }
local pos = 1
local b
local func
local err
while pos < #template do
---- Look for start of a Lua block.
b = template:find('<[<%%]', pos)
if not b then
break
end
---- Check if this is a block or escaped <.
if template:sub(b-1, b-1) == '\\' then
appender(builder, template:sub(pos, b-2))
appender(builder, '<')
pos = b+1
else
---- Add all text up until this block.
appender(builder, template:sub(pos, b-1))
---- Find the end of the block.
pos = template:find('[>%%]>', b)
if not pos then
appender(builder, 'End tag missing')
break
end
runBlock(builder, template:sub(b, pos+2))
---- Skip back the >> (pos points to the start of >>).
pos = pos+2
end
end
---- Add any text after the last block. Or all of it if there
---- are no blocks.
if pos then
appender(builder, template:sub(pos, #template))
end
builder[#builder+1] = 'return table.concat(_ret)'
---- Run the Lua code we built though Lua and get the result.
func, err = load(table.concat(builder, '\n'), 'template', 't', env)
if not func then
return nil, err
end
return func()
end
Where as this is a lot easier to read:
Code: Select all
local function compile(template, environmentOrLoadLocalEnv, loadLocalEnv)
---- Turn the template into a string that can be run though Lua.
---- Builder will be used to efficiently build the string we'll run.
---- The string will use it's own builder (_ret). Each part that comprises
---- _ret will be the various pieces of the template. Strings, variables
---- that should be printed and functions that should be run.
if not template or template == '' then return '' end
local env = { }
if type(environmentOrLoadLocalEnv) == 'table' then
env = environmentOrLoadLocalEnv
elseif type(environmentOrLoadLocalEnv) == 'boolean' then
loadLocalEnv = environmentOrLoadLocalEnv
end
env.__index = env
setmetatable(env, { __index = _G })
if loadLocalEnv then
local i = 1
while true do
local name, value = debug.getlocal(2, i)
if not name then break end
env[name] = value
i = i + 1
end
end
local builder = { '_ret = {}\n' }
local pos = 1
local b
local func
local err
while pos < #template do
---- Look for start of a Lua block.
b = template:find('<[<%%]', pos)
if not b then
break
end
---- Check if this is a block or escaped <.
if template:sub(b-1, b-1) == '\\' then
appender(builder, template:sub(pos, b-2))
appender(builder, '<')
pos = b+1
else
---- Add all text up until this block.
appender(builder, template:sub(pos, b-1))
---- Find the end of the block.
pos = template:find('[>%%]>', b)
if not pos then
appender(builder, 'End tag missing')
break
end
runBlock(builder, template:sub(b, pos+2))
---- Skip back the >> (pos points to the start of >>).
pos = pos+2
end
end
---- Add any text after the last block. Or all of it if there
---- are no blocks.
if pos then
appender(builder, template:sub(pos, #template))
end
builder[#builder+1] = 'return table.concat(_ret)'
---- Run the Lua code we built though Lua and get the result.
func, err = load(table.concat(builder, '\n'), 'template', 't', env)
if not func then
return nil, err
end
return func()
end