Guest User

Untitled

a guest
Dec 11th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.24 KB | None | 0 0
  1. local tArgs = {...} -- Get the commandline arguments
  2. local sExpr = table.concat(tArgs, " ")  -- Concatenate the args back to <arg1> <arg2> <arg..>
  3. local fnMath, sErr = loadstring("return "..sExpr)   -- Load the expression provided via commandline arguments into a function block
  4.  
  5. if not fnMath then  -- If fnMath is nil we failed to load the function, print the error returned
  6.     print("ERROR: "..sErr)
  7.     return
  8. end
  9. --[[
  10.     Given sExpr = "5+9/8*(98 - 45)"
  11.     The above loadstring is equivalent to
  12.     fnMath = function()
  13.         return 5+9/8*(98 - 45)
  14.     end
  15. --]]
  16.  
  17. setfenv(fnMath, math) -- Set the functions environment 'root' in the math table instead of _G
  18. --[[
  19.     This allows for an expression like:  8 * 6 / sin(rad(36))
  20.     But it also allows for expressions like:  _G.os.shutdown()
  21. --]]
  22.  
  23. local bSucc, vRes = pcall(fnMath)   -- This calls the function in a 'safe' manner any errors thrown in fnMath won't error this program
  24. --[[
  25.     If no errors are thrown then bSucc will be true and vRes will contain the return value,
  26.     Otherwise vRes will contain the error message
  27. --]]
  28.  
  29. if not bSucc then   -- If bSucc is false we failed calling the function, print the error
  30.     print("ERROR: "..vRes)
  31. else
  32.     print("Result: "..vRes)
  33. end
Advertisement
Add Comment
Please, Sign In to add comment