Advertisement
LPGhatguy

Lua Fragment Preprocessor Mockup

May 9th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.85 KB | None | 0 0
  1. local fragments = {}
  2.  
  3. function load_fragment(location)
  4.     location = location .. ".lua"
  5.  
  6.     local file = io.open(location, "r")
  7.  
  8.     if (file) then
  9.         local frag_source = file:read("*all")
  10.  
  11.         fragments[location] = frag_source
  12.         file:close()
  13.  
  14.         return frag_source
  15.     end
  16. end
  17.  
  18. function get_fragment(location)
  19.     if (fragments[location]) then
  20.         return fragments[location]
  21.     else
  22.         return load_fragment(location)
  23.     end
  24. end
  25.  
  26. function load_source(location)
  27.     location = location .. ".lua"
  28.  
  29.     local file, err = io.open(location, "r")
  30.  
  31.     if (file) then
  32.         local source = file:read("*all")
  33.  
  34.         for statement, fragment_location in source:gmatch("(--@include ([^\n]+))") do
  35.             local fragment = get_fragment(fragment_location) or "--FRAGMENT '" .. fragment_location .. "' NOT FOUND"
  36.  
  37.             source = source:gsub(statement, fragment)
  38.         end
  39.  
  40.         return source
  41.     end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement