+, -, /, *, %, ^ and unary - will fail if you use them with a value that can't be converted to a number.
Comparisons will work as expected if both values can be converted to a number, or both are actual strings, and will return false if one operand converts to a number and the other does not.
Code: Select all
setmetatable( matches, {
["__add"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
return (o1 + o2)
end
end,
["__sub"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
return (o1 - o2)
end
end,
["__div"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
return (o1 / o2)
end
end,
["__mul"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
return (o1 * o2)
end
end,
["__mod"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
return (o1 % o2)
end
end,
["__pow"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
return (o1 ^ o2)
end
end,
["__unm"] = function(op)
local o = tonumber(op)
if o then
return -o
end
end,
["__eq"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
if (o1 == o2) then
return true
else
return false
end
elseif ((o1 and (not o2)) or (o2 and (not o1))) then
return false
elseif ((not o1) and (not o2)) then
if (op1 == op2) then
return true
end
end
end,
["__lt"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
if (o1 < o2) then
return true
else
return false
end
elseif ((o1 and (not o2)) or (o2 and (not o1))) then
return false
elseif ((not o1) and (not o2)) then
if (op1 < op2) then
return true
end
end
end,
["__le"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
if (o1 <= o2) then
return true
else
return false
end
elseif ((o1 and (not o2)) or (o2 and (not o1))) then
return false
elseif ((not o1) and (not o2)) then
if (op1 <= op2) then
return true
end
end
end,
["__gt"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
if (o1 > o2) then
return true
else
return false
end
elseif ((o1 and (not o2)) or (o2 and (not o1))) then
return false
elseif ((not o1) and (not o2)) then
if (op1 > op2) then
return true
end
end
end,
["__ge"] = function(op1, op2)
local o1, o2 = tonumber(op1), tonumber(op2)
if (o1 and o2) then
if (o1 >= o2) then
return true
else
return false
end
elseif ((o1 and (not o2)) or (o2 and (not o1))) then
return false
elseif ((not o1) and (not o2)) then
if (op1 >= op2) then
return true
end
end
end,
})