function solve(s) ret = s if(string.match(s, "[%+%-]") ~= nil) then num = tonumber(string.match(s, "(%d+)[%+%-]")) for d in string.gmatch(s, "([%+%-]%d+)") do num = num + tonumber(d) end ret = tostring(num) end return ret end src = io.read() pat = "(%([%d%+%-]+%))" a = string.match(src, pat) while(a ~= nil) do b = string.match(a, "%((.+)%)") r = solve(b) src = string.gsub(src, pat, r, 1) a = string.match(src, pat) end src = string.gsub(src, "%-%-", "+") src = string.gsub(src, "%+%+", "+") src = string.gsub(src, "%-%+", "-") src = string.gsub(src, "%+%-", "-") src = solve(src) print(src)