lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> import(io, 'read', 'write', 'setvbuf')

If you can assume that such statements happen only within say the first
10 lines of the file you could write a reader function that scans these
lines and does a simple gsub. You can even drop the quotes. Something
like this:

local function import(s)
	local o=""
	local t=nil
        for w in s:gmatch("%w+") do
		if t==nil then
			t=w
		else
			o=o.."local "..w.."="..t.."."..w.."\t"
		end
	end
	return o
end

T=[[
require"math"
import(io, 'read', 'write', 'setvbuf')
import(math, sin, cos)
print(sin(1))
]]

T=T:gsub("import%s*%((.-)%)", import)
io.write(T) -- actually return T

A syntax like "from name import name1,name,name3" would be even simpler
to parse and arguably more clear.