Advertisement
LBPHacker

Untitled

Jul 18th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. local parseCommand = function(str)
  2.     --[[
  3.     * Lua uses the \ for escaping characters - not the pattern part though, that
  4.       uses the % - so every character that is after a % is an escape sequence.
  5.     * Here is a common one: %s - this is a character set - contains all whitespace
  6.       characters, including \t, space itself, etc.
  7.     * + is suffix. It means that we have more than 0 of the character
  8.       (or character set, like %s) which it follows
  9.     * ( and ) enclose a capture - .gsub returns the content of every capture, or if
  10.       there is no capture at all, the whole pattern
  11.     * . means ANY character
  12.     * as you can see, we captured a sequence of characters with "(.+)" - the pattern
  13.       looks for SOME whitespaces at the beginning, then for some characters of ANY kind,
  14.       then whitespaces again - we caprute the ANY character part, and that's what .gsub
  15.       returns - yay, we have the full command without spaces on its ends
  16.     ]]
  17.     str = str:gsub("%s+(.+)%s+")
  18.     local words = {}
  19.     --[[
  20.     * .gmatch is an iterator - it creates a table which contains all occurences of the
  21.       pattern in the string - then it returns a function which - if used properly in a
  22.       for loop, iterates through that table
  23.     * ^ means NOT, so [^%s] is a pattern which contains no whitespaces, but does contain
  24.       something (hence the +) - result: we have a words table filled with the words
  25.     ]]
  26.     for word in str:gmatch("[^%s]+") do table.insert(words, word) end
  27.     -- now words[1] contains the command itself
  28.     if words[1] then
  29.         -- you can go on from now I think
  30.         if words[1] == "open" and words[3] == "door" then
  31.            
  32.         elseif words[1] == "close" and words[3] == "door" then
  33.            
  34.         elseif words[1] == "turn" and words[3] == "lights" then
  35.            
  36.         elseif words[1] == "add" then
  37.            
  38.         elseif words[1] == "remove" then
  39.            
  40.         else
  41.             -- prints an error message about words[1] not being a real command
  42.         end
  43.     else
  44.         -- str was empty
  45.         -- you can remove this branch of course
  46.     end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement