-
Posted by galvin on Sun, 04 Jul 2010 06:40:00
local operators = { ["+"] = function(a, b) return a + b end, ["-"] = function(a ,b) return a - b end, ["*"] = function(a ,b) return a * b end, ["/"] = function(a, b) return a / b end } local NUMBER_PATTERN = "^%s*([+-]?%d*%.?%d+)" local OPERATOR_PATTERN = "^%s*(.)" local END_PATTERN = "^%s*$" local NUMBER_ERROR = "No valid number at position %d" local OPERATOR_ERROR = "Unrecognized operator at position %d: '%s'" local errorState = false local function reportError(message) print(message) errorState = true end local function calculate(number1, ...) if errorState then return end for i = 1, select("#", ...), 2 do print(select(i, ...)) local operator, number2 = select(i, ...) number1 = operators[operator](number1, number2) end return number1 end local function getpairs(message, start) local _, operatorFinish, operator = message:find(OPERATOR_PATTERN, start) local operatorFunction = operators[operator] if not operatorFunction then reportError(OPERATOR_ERROR:format(start, operator)) return end operatorFinish = operatorFinish + 1 local _, numberFinish, number = message:find(NUMBER_PATTERN, operatorFinish) if not number then reportError(NUMBER_ERROR:format(operatorFinish)) return end numberFinish = numberFinish + 1 if message:match(END_PATTERN, numberFinish) then return operatorFunction, number else return operatorFunction, number, getpairs(message, numberFinish) end end local function tokenize(message) local _, finish, number = message:find(NUMBER_PATTERN) if not number then reportError(NUMBER_ERROR:format(1)) return end finish = finish + 1 if message:match(END_PATTERN, finish) == "" then return number else return number, getpairs(message, finish) end end SLASH_SIMPLECALC1 = "/calculate" SLASH_SIMPLECALC2 = "/calc" SlashCmdList["SIMPLECALC"] = function(message) errorState = false print(calculate(tokenize(message))) end
When I do a simple command: /calculate 1 + 2 I get the following error
Message: Interface\AddOns\UiMisc\UiMisc.lua:38: attempt to call field '?' (a nil value) Time: 07/04/10 02:38:56 Count: 2 Stack: Interface\AddOns\UiMisc\UiMisc.lua:38: in function <Interface\AddOns\UiMisc\UiMisc.lua:30> Interface\AddOns\UiMisc\UiMisc.lua:88: in function `?' Interface\FrameXML\ChatFrame.lua:4049: in function `ChatEdit_ParseText' Interface\FrameXML\ChatFrame.lua:3660: in function `ChatEdit_SendText' Interface\FrameXML\ChatFrame.lua:3698: in function `ChatEdit_OnEnterPressed' [string "*:OnEnterPressed"]:1: in function <[string "*:OnEnterPressed"]:1> Locals: number1 = "1" (for index) = 1 (for limit) = 2 (for step) = 2 i = 1 operator = <function> defined @Interface\AddOns\UiMisc\UiMisc.lua:2 number2 = "2" (*temporary) = nil (*temporary) = "1" (*temporary) = "2" (*temporary) = "attempt to call field '?' (a nil value)" errorState = false operators = <table> { - = <function> defined @Interface\AddOns\UiMisc\UiMisc.lua:5 / = <function> defined @Interface\AddOns\UiMisc\UiMisc.lua:11 + = <function> defined @Interface\AddOns\UiMisc\UiMisc.lua:2 * = <function> defined @Interface\AddOns\UiMisc\UiMisc.lua:8 }
Also curious what happens when you do a return of a function that returns more than one argument.
so for example. "return somevariable, x()" and if x() returns more than one argument how does that effect the return somevariable, x() ?
-
Posted by jnwhiteh on Sun, 04 Jul 2010 08:56:30
There's an issue with the bolding of the text in the second code block on page 343. You also need to edit the two lines within the for statement from the previous version. As listed on that page, the calculate function should be this:
local function calculate(number1, ...) if errorState then return end print(number1, ...) for i = 1, select("#", ...), 2 do local operator, number2 = select(i, ...) number1 = operator(number1, number2) end return number1 end
Also curious what happens when you do a return of a function that returns more than one argument.
so for example. "return somevariable, x()" and if x() returns more than one argument how does that effect the return somevariable, x() ?
The semantics of multiple return values are discussed early on in the book, check the table of contents. In this case, as long as the thing returning multiple values is at the end of a return list, all of those values will be returned.. same as usual.
-
Posted by galvin on Sun, 04 Jul 2010 21:48:15
Thanks, works now.