Advertisement
theTANCO

switch.lua

Jul 28th, 2021 (edited)
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1. -- This function attempts to emulate switch statements used in programming languages such as C++.
  2. -- First argument is the case in the statement to run. This is a value to index an element in a table. Indexes can be any number or string.
  3. -- Second argument is a table containing functions representing each case. This only executes the case indexed by the first argument. Setting a case equal to a number or string represents a pointer to a different case function. This is to avoid copy and pasting functions. Pointers cannot point to pointers. Unlike C++ switch statements, these switch statements can be nested within switch statements.
  4. -- Third argument is the default case. This is run if none of the other cases are executed.
  5. -- Additional arguments are passed to the case functions.
  6.  
  7. switch = function(case, cases, defCase, ...)
  8.     if cases[case] then
  9.         if type(cases[case]) == "function" then return cases[case](...)
  10.         elseif type(cases[case]) == "number" or type(cases[case]) == "string" then return cases[cases[case] ](...)
  11.         else error("Case element must be a number, string, or function.", 2) end
  12.     else
  13.         if defCase then return defCase(...)
  14.         else error("Undefined case '"..tostring(case).."'.", 2) end
  15.     end
  16. end
  17.  
  18. -- Examples of the switch statement in use.
  19. local function testProgram()
  20.     for a = 0, 5 do
  21.         switch(a, {
  22.             [0] = function()
  23.                 print('h')
  24.             end,
  25.             [1] = function()
  26.                 print('e')
  27.             end,
  28.             [2] = function()
  29.                 print('l')
  30.             end,
  31.             [3] = 2, -- This case points to case [2], so case [3] will run the function for case [2]. Another case may not point to case [3], or else the program will throw an error.
  32.             [4] = function()
  33.                 print('o')
  34.             end
  35.         }, function(punctuation)
  36.             print(punctuation)
  37.         end, '.')
  38.     end
  39.  
  40.     switch("pear", {
  41.         ["apple"] = function()
  42.             print("Hello world")
  43.         end,
  44.         pear = function()
  45.             print("Switch statements are neat.")
  46.         end
  47.     }, function() end)
  48.  
  49.     switch("apple", {
  50.         ["fruit"] = function()
  51.             print("Hello world")
  52.         end,
  53.         veggie = function()
  54.             print("Switch statements are neat.")
  55.         end,
  56.         apple = "fruit"
  57.     }, function() end)
  58. end
  59.  
  60. -- Uncomment the line below to run the test program function.
  61. -- testProgram()
  62.  
  63. -- This program was written for ComputerCraft, but I have not used any functions that are not standard to Lua, so it should work with any Lua application.
  64. -- This program was tested in CCEmuX which is a ComputerCraft "emulator" so that I don't have to start up all of Minecraft every time I want to make something in CC that's not explicitly Minecraft related.
  65. -- You can use this with ComputerCraft's os.loadAPI() to load the Class() function into a separate program file.
  66. -- Alternatively, you can use the require() function, which does effectively the same thing but is newer to ComputerCraft.
  67. -- If you can I highly recommend using require() instead because it's way more efficient, and it's a standard Lua function so it should work beyond ComputerCraft, but I have not tested this to be sure.
  68.  
  69. --[[ Changelog
  70.     2022/06/22: This switch statement will be deprecated for performance reasons. I realized that this is actually 2 times slower than regular if statements.
  71.     2022/03/03:
  72.         If a default case is not defined and the given case is also not defined, an error will be thrown for the invalid case.
  73.     2021/07/28:
  74.         Initial release.
  75. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement