Advertisement
Symmetryc

Macro Thinger

Mar 18th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.13 KB | None | 0 0
  1. local macros = {
  2.     ["@"] = {
  3.         pat = "@(%b())";
  4.         fn = function(_f)
  5.             local f = _f:sub(2, -2)
  6.                         :gsub("@0", "a")
  7.                         :gsub("@(%d)", "a[%1]")
  8.                         :gsub("@", "return ")
  9.             return "(function(...) local a = {...} "..f.." end)"
  10.         end;
  11.     };
  12.     ["$"] = {
  13.         add = [[
  14.                 local get_file_macro = setmetatable({}, {
  15.                     __index = function(self, _k)
  16.                         if not fs.exists(_k) then
  17.                             fs.open(_k, "w").close()
  18.                         end
  19.                         local h = fs.open(_k, "r")
  20.                         local text = h.readAll()
  21.                         h.close()
  22.                         return text
  23.                     end;
  24.                     __newindex = function(self, _k, _v)
  25.                         if type(_v) == "string" then
  26.                             local h = fs.open(_k, "w")
  27.                             h.write(_v)
  28.                             h.close()
  29.                         end
  30.                     end;
  31.                 })
  32.             ]];
  33.         pat = "$(%b())";
  34.         fn = function(_f)
  35.             local f = _f:sub(2, -2)
  36.             return "get_file_macro["..f.."]"
  37.         end;
  38.     };
  39. }
  40. return function(_str, ...)
  41.     local str, arg = _str, {...}
  42.     for macro in _str:gmatch("#macro%s+(%S+)") do
  43.         local use_macro = macros[macro]
  44.         str = (use_macro.add or "")..str
  45.         if use_macro.pat and use_macro.fn then
  46.             str = str:gsub(use_macro.pat, use_macro.fn)
  47.         end
  48.     end
  49.     return str
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement