
local tArgs = { ... }
if #tArgs > 0 then
	print( "This is an interactive Lua prompt." )
	print( "To run a lua program, just type its name." )
	return
end

local bRunning = true
local tCommandHistory = {}
local tEnv = {
	["exit"] = function()
		bRunning = false
	end,
}
setmetatable( tEnv, { __index = getfenv() } )

print( "Interactive Lua prompt." )
print( "Call exit() to exit." )
while bRunning do
	write( "lua> " )
	
	local s = read( nil, tCommandHistory )
	table.insert( tCommandHistory, s )
	
	local nForcePrint = 0
	local func, e = loadstring( s, "lua" )
	local func2, e2 = loadstring( "return "..s, "lua" )
	if not func then
		if func2 then
			func = func2
			e = nil
			nForcePrint = 1
		end
	else
		if func2 then
			func = func2
		end
	end
	
	if func then
        setfenv( func, tEnv )
        local tResults = { pcall( function() return func() end ) }
        if tResults[1] then
        	local n = 1
        	while (tResults[n + 1] ~= nil) or (n <= nForcePrint) do
        		print( tostring( tResults[n + 1] ) )
        		n = n + 1
        	end
        else
        	print( tResults[2] )
        end
    else
    	print( e )
    end
    
end
