Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tArgs = {...} -- Get the commandline arguments
- local sExpr = table.concat(tArgs, " ") -- Concatenate the args back to <arg1> <arg2> <arg..>
- local fnMath, sErr = loadstring("return "..sExpr) -- Load the expression provided via commandline arguments into a function block
- if not fnMath then -- If fnMath is nil we failed to load the function, print the error returned
- print("ERROR: "..sErr)
- return
- end
- --[[
- Given sExpr = "5+9/8*(98 - 45)"
- The above loadstring is equivalent to
- fnMath = function()
- return 5+9/8*(98 - 45)
- end
- --]]
- setfenv(fnMath, math) -- Set the functions environment 'root' in the math table instead of _G
- --[[
- This allows for an expression like: 8 * 6 / sin(rad(36))
- But it also allows for expressions like: _G.os.shutdown()
- --]]
- local bSucc, vRes = pcall(fnMath) -- This calls the function in a 'safe' manner any errors thrown in fnMath won't error this program
- --[[
- If no errors are thrown then bSucc will be true and vRes will contain the return value,
- Otherwise vRes will contain the error message
- --]]
- if not bSucc then -- If bSucc is false we failed calling the function, print the error
- print("ERROR: "..vRes)
- else
- print("Result: "..vRes)
- end
Advertisement
Add Comment
Please, Sign In to add comment