1
votes

I have tried to realize adding numbers in lua:

local Calculator = {};
function Calculator.get( frame )
 local new_args = str._getParameters( frame.args, { 'f', 'o', 's' } );
 local f = tonumber( new_args['f'] ) or 1;
 local o = ( new_args['o'] ) or ""; 
 local s = tonumber( new_args['s'] ) or 1;
 Calculator.ret(first, second, operation);
end

function Calculator.ret (f, o, s)
 if(o == "+") then return f+s;
end

return Calculator

Even if place a end in end, error doesn't disappear.

1
Also drop the semicolons. They're unnecessary and not really idiomatic.Bartek Banachewicz
your function call Calculator.ret(first, second, operation) is also wrong. first, second and operation are nil. your variables are f, o and s. And the order of parameters is also wrong. You should call Calculator.ret(f, o, s). Also using () in the if-statement is not necessary. Further you don't use the return value of your Calculator.ret call, so your Program doesn't actually do muchPiglet
@BartekBanachewicz This is convention, FYI. I also use semicolons and empty statementsKlaider
@Hand1Cloud Yes, it's a convention, and it's specifically Lua convention to not use the semicolons. They're a remnant of the dark past only created for compilers, and there's no reason to perpetuate this unnecessary syntax noise. And empty statements are a corner case which doesn't apply here.Bartek Banachewicz
@BartekBanachewicz Empty statements don't appear in his code, but are supported at least on Lua 5.2. Don't get me wrong.Klaider

1 Answers

2
votes
function Calculator.ret (f, o, s)
 if(o == "+") then return f+s end
end                            ^----------------- here

if in Lua always has to have an end (kinda unlike {} for ifs in C-like languages).