Advertisement
WhiteFire_Sondergaar

dofile

Jun 6th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1. -- Compile ACT code
  2. -- By Fenthis
  3.  
  4. -- This program takes a specified file, removes Lua
  5. -- style comments, and spaces. It also allows you to
  6. -- define macros like:
  7. --
  8. -- macroname = macrotext
  9. --
  10. -- And use them as [macroname].
  11. --
  12. -- Extra arguments to the program are turned into macros
  13. -- such as [1], [2], etc.
  14.  
  15. -- macro store
  16. macros = {}
  17. -- parsed code
  18. code = ""
  19.  
  20. -- Arguments!
  21. args = { ... }
  22. if #args < 1 then
  23.     print("Usage: dofile <filename> [<arg1> ...]")
  24.     return
  25. end
  26.  
  27. -- Dur
  28. if not turtle then
  29.     print("I am not running on a turtle.")
  30.     return
  31. end
  32.  
  33. -- Check to see if the API is loaded
  34. if not act then
  35.     print("act api not installed.")
  36.     return
  37. end
  38.  
  39. -- Open the file, make sure we can read it.
  40. filename = args[1]
  41. f = fs.open(filename, 'r')
  42. if not f then
  43.     print("Can not read file: "..filename)
  44.     return
  45. end
  46.  
  47. -- Define arguments as macros
  48. if #args > 1 then
  49.     for i = 2, #args do
  50.         macros[i-1] = args[i]
  51.     end
  52. end
  53.  
  54. -- Process the file
  55. line = f:readLine()
  56. while line do
  57.     -- Remove comments
  58.     line = line:gsub("%-%-.*", "")
  59.     -- Remove whitespace
  60.     line = line:gsub("%s", "")
  61.  
  62.     -- Define macros
  63.     macro, value = line:match("^([^=]*)=(.*)$")
  64.     if macro then
  65.         if not macro:match("^[%w_%-]*$") then
  66.             print("Macro names may only contain letters, numbers, _ and -.")
  67.             return
  68.         end
  69.         -- print("Defining macro "..macro.." with value "..value)
  70.         macros[macro] = value
  71.         line = ""
  72.     -- Or apply macros
  73.     else
  74.         for key, value in pairs(macros) do
  75.             line = line:gsub("%["..key.."%]", value)
  76.         end
  77.     end
  78.     -- save to code
  79.     code = code .. line
  80.  
  81.     -- Next!
  82.     line = f:readLine()
  83. end
  84.  
  85. -- Let them know what we are doing
  86. print("Code: "..code)
  87.  
  88. -- Dooo et!
  89. act.act(code)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement