From: Alexis Laferrière Date: Fri, 21 Aug 2015 18:16:22 +0000 (-0400) Subject: examples/calculator: avoid crash from call to `to_n` on empty strings X-Git-Tag: v0.7.8~71^2~3 X-Git-Url: http://nitlanguage.org examples/calculator: avoid crash from call to `to_n` on empty strings Signed-off-by: Alexis Laferrière --- diff --git a/examples/calculator/src/calculator_logic.nit b/examples/calculator/src/calculator_logic.nit index ea45a99..e5e492b 100644 --- a/examples/calculator/src/calculator_logic.nit +++ b/examples/calculator/src/calculator_logic.nit @@ -101,26 +101,27 @@ class CalculatorContext protected fun apply_last_op_if_any do var op = last_op - var result = result - var current = current - if current == null then current = new FlatBuffer + self.current = null + + if current == null then return if op == null then result = current.to_n - else if op == '+' then - result = result.add(current.to_n) - else if op == '-' then - result = result.sub(current.to_n) - else if op == '/' then - result = result.div(current.to_n) - else if op == '*' then - result = result.mul(current.to_n) + else if result != null then + if op == '+' then + result = result.add(current.to_n) + else if op == '-' then + result = result.sub(current.to_n) + else if op == '/' then + result = result.div(current.to_n) + else if op == '*' then + result = result.mul(current.to_n) + end end self.result = result - self.current = null end end