From 4e869fcb6ac1d7febaa5633774fb4cfd293bd297 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Fri, 21 Aug 2015 14:16:22 -0400 Subject: [PATCH] examples/calculator: avoid crash from call to `to_n` on empty strings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Alexis Laferrière --- examples/calculator/src/calculator_logic.nit | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) 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 -- 1.7.9.5