examples/calculator: revamp style of the calculator code
[nit.git] / examples / calculator / src / calculator_logic.nit
index c5fbaf7..57d3b5a 100644 (file)
@@ -1,7 +1,5 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
-# Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
-#
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
 # You may obtain a copy of the License at
 # Business logic of a calculator
 module calculator_logic
 
+# Hold the state of the calculator and its services
 class CalculatorContext
+       # Result of the last operation
        var result: nullable Numeric = null
 
+       # Last operation pushed with `push_op`, to be executed on the next push
        var last_op: nullable Char = null
 
+       # Value currently being entered
        var current: nullable FlatBuffer = null
+
+       # Text to display on screen
        fun display_text: String
        do
                var result = result
@@ -51,7 +55,8 @@ class CalculatorContext
                return buf.to_s
        end
 
-       fun push_op( op : Char )
+       # Push operation `op`, will usually execute the last operation
+       fun push_op(op: Char)
        do
                apply_last_op_if_any
                if op == 'C' then
@@ -65,7 +70,8 @@ class CalculatorContext
                self.current = null
        end
 
-       fun push_digit( digit : Int )
+       # Push a digit
+       fun push_digit(digit: Int)
        do
                var current = current
                if current == null then current = new FlatBuffer
@@ -78,6 +84,7 @@ class CalculatorContext
                end
        end
 
+       # Switch entry mode from integer to decimal
        fun switch_to_decimals
        do
                var current = current
@@ -86,7 +93,8 @@ class CalculatorContext
                self.current = current
        end
 
-       fun apply_last_op_if_any
+       # Execute the last operation it not null
+       protected fun apply_last_op_if_any
        do
                var op = last_op
 
@@ -106,6 +114,7 @@ class CalculatorContext
                else if op == '*' then
                        result = result.mul(current.to_n)
                end
+
                self.result = result
                self.current = null
        end