lua-users home
lua-l archive

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


On Oct 18, 2011, at 2:11 PM, Patrick Mc(avery <patrick@spellingbeewinnars.org> wrote:

> I think every newcomer to the language would expect this to work:

Hmmm... why would they?

> (file oneVar.lua)
> local x = 1
> 
> (file simpleTest.lua)
> print("starting")
> dofile("oneVar.lua")
> print(x)
> print("the end")

Write your own:

$ cat TestInclude0.lua
local y = 2

$ cat TestInclude1.lua
local x = 1

-- #include TestInclude0.lua

$ cat TestInclude2.lua
print("starting")
-- #include TestInclude1.lua
print(x)
print(y)
print("the end")


$ cat TestInclude.lua
local function Include( aName )
  local aFunction = function()
    local aReader = assert( io.open( aName, 'rb' ) )

    for aLine in function() return aReader:read( '*L' ) end do
      if aLine:find( '#include ', 1, true ) then
        local anInclude = aLine:match( '#include (.-)%c-$' )

        for aLine in Include( anInclude ) do
          coroutine.yield( aLine )
        end
      else
        coroutine.yield( aLine )
      end
    end
  
    aReader:close()
  end

  return coroutine.wrap( aFunction )
end

local aChunk = assert( load( Include( 'TestInclude2.lua' ) ) )

aChunk()

% lua TestInclude.lua

starting
1
2
the end