examples/calculator: avoid crash from call to `to_n` on empty strings
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 21 Aug 2015 18:16:22 +0000 (14:16 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Sat, 22 Aug 2015 21:46:34 +0000 (17:46 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

examples/calculator/src/calculator_logic.nit

index ea45a99..e5e492b 100644 (file)
@@ -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