Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 6th, 2011  |  syntax: Lua  |  size: 1.50 KB  |  views: 197  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print  |  QR code  |  clone
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. rule_actions = {}
  2. rules_done = {}
  3.  
  4. function do_rule(name)
  5.     assert (rule_actions[name], "no such rule "..tostring(name))
  6.     return rule_actions[name]()
  7. end
  8.  
  9. function do_action(act)
  10.     if type(act) == "string" then
  11.         return os.execute(act) == 0
  12.     elseif type(act) == "function" then
  13.         return act()
  14.     end
  15. end
  16.  
  17. function rule(name)
  18.     assert(not rule_actions[name], "rule "..tostring(name).." has already been defined!")
  19.     return function(dependencies)
  20.         return function(actions)
  21.             rule_actions[name] = function()
  22.                 if rules_done[name] then
  23.                     return true
  24.                 end
  25.                 for k, v in pairs(dependencies) do
  26.                     if not do_rule(v) then
  27.                         return false
  28.                     end
  29.                 end
  30.                 if type(actions) == "table" then
  31.                     for k, v in pairs(actions) do
  32.                         if not do_action(v) then
  33.                             return false
  34.                         end
  35.                     end
  36.                 else
  37.                     if not do_action(actions) then
  38.                         return false
  39.                     end
  40.                 end
  41.                 rules_done[name] = true
  42.                 return true
  43.             end
  44.         end
  45.     end
  46. end
  47.  
  48.  
  49. rule "printhello" {} [[
  50.     echo hello
  51. ]]
  52. rule "printhi" {} {
  53.     function() print ("HI") return true end
  54. }
  55. rule "all" {"printhello", "printhi"} {}
  56.  
  57. do_rule "all"
clone this paste RAW Paste Data
Top