lua-users home
lua-l archive

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


I've adapted to Lua 5.2 an old script of mime that reports the use of
globals in Lua programs. Here it is, in case someone else finds it useful.

-- globals52.lua
-- report the use of globals
-- typical usage: luac -p -l *.lua | lua globals52.lua | sort

local S={}
local G={}
local T={}
local F

while true do
	local s=io.read()
	if s==nil then break end
	local f=s:match("^[mf].-<(.-):[%d,]+>")
	if f then F=f end
	local l,op,g=s:match('%[%-?(%d*)%]%s*([GS]ET)TABUP.-;%s+_ENV%s+"(.*)"')
	if l then
		T[g]=true
		local w=F.."\t"..l
		--print(w,op,g)
		if op=="SET" then S[g]=w else G[g]=w end
	end
end

for k,v in pairs(T) do
	if S[k] then
		if _G[k]==nil then
			print(S[k],"def",k)
		else
			print(S[k],"redef",k)
		end
	elseif _G[k]==nil then
		print(G[k],"undef",k)
	else
		print(G[k],"read",k)
	end
end