/* ********************************************************************* This source file is a part of the standard library of Scol For the latest info, see http://www.scolring.org Copyright (c) 2014 Stephane Bisaro aka Iri . This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ********************************************************************* */ /* * Standard functions for boolean * See http://redmine.scolring.org/projects/tutorials/wiki/Scol_usage * for more informations */ /*! \file bool.pkg * \author Scol team * \version 0.1 * \copyright GNU Lesser General Public License 2.0 or later * \brief Boolean and Logical Standard Library API. * * Like C, 0 is FALSE, all other values is TRUE * **/ /* Like C, 0 is false, all other values is TRUE */ /*! \brief Define the FALSE value : always 0 * * \ingroup std_bool * Prototype : fun [] I * * \return I : always 0 **/ fun FALSE () = 0;; /*! \brief Define the TRUE value : NOT FALSE (1) * * \ingroup std_bool * Prototype : fun [] I * * \return I : always 1 **/ fun TRUE () = !FALSE;; /*! \struct BOOL * * \brief Opaque internal structure. You should not call it directly, use * API instead ! Be carefull, BOOL is an Scol object, not an I. It is not * really a boolean. * **/ struct BOOL = [ bool_value : I, bool_cbchange : fun [BOOL I] I, bool_cbtrue : fun [BOOL] I, bool_cbfalse : fun [BOOL] I, bool_cbUndefined : fun [BOOL] I ] mkBOOL;; /*! \brief Create a new BOOL Scol object. * * The value will be 'undefined' (i.e neither FALSE nor TRUE) by defaut. * * \ingroup std_bool * Prototype : fun [ ] BOOL * * \return BOOL : a new BOOL Scol object **/ fun _CRbool ()= mkBOOL [nil nil nil nil nil];; /*! \brief Perform a reset to a BOOL Scol object. The value will be 'undefined'. * * \see _CRbool * * \ingroup std_bool * Prototype : fun [BOOL] BOOL * * \param BOOL : any BOOL Scol object * * \return BOOL : the same BOOL Scol object resetted **/ fun _RESETbool (b)= set b.bool_value = nil; set b.bool_cbchange = nil; set b.bool_cbtrue = nil; set b.bool_cbfalse = nil; set b.bool_cbUndefined = nil; b;; /*! \brief Define a new value for a BOOL Scol object. * * A new value can be : * - TRUE * - FALSE * - or any other value. In this last case, the value will be 'undefined' * (it can be 'nil' but this can be changed in the future, so don't take account * and keep the 'undefined' idea only.) * * Depending the given new value, one of these callbacks will be also called : * - 'state changed to true' * - 'state changed to false' * - 'state changed to undefined' * * The other callback 'state changed' will be always called after. * * \see _CBboolChanged * \see _CBboolTrue * \see _CBboolFalse * \see _CBboolUndefined * * \ingroup std_bool * Prototype : fun [BOOL I] BOOL * * \param BOOL : any BOOL Scol object * \param I : a new value to set * * \return BOOL : the same BOOL Scol object **/ fun _SETbool (b, state) = set b.bool_value = if state == FALSE then ( exec b.bool_cbfalse with [b]; state ) else if state == TRUE then ( exec b.bool_cbtrue with [b]; state ) else ( exec b.bool_cbUndefined with [b]; nil ); exec b.bool_cbchange with [b b.bool_value]; b;; /*! \brief Return the current value for a BOOL Scol object. * * The current value can be : * - TRUE * - FALSE * - nil * * \see _SETbool * * \ingroup std_bool * Prototype : fun [BOOL] I * * \param BOOL : any BOOL Scol object * * \return I : the current value or 2 if the object is nil itself **/ fun _GETbool (b)= if b == nil then 2 else b.bool_value;; /*! \brief Return if the current value for a BOOL Scol object is an * 'undefined' state. * * \see _SETbool * * \ingroup std_bool * Prototype : fun [BOOL] I * * \param BOOL : any BOOL Scol object * * \return I : TRUE if undefined else FALSE. If the object is nil itself, * nil is returned. **/ fun _ISboolUndefined (b) = if b == nil then nil else if nil == b.bool_value then TRUE else FALSE;; /*! \brief Define the 'state changed' callback for a BOOL Scol object. * It can be nil for no call (default). * * It will be called when the value changes. * * \see _CBboolTrue * \see _CBboolFalse * \see _CBboolUndefined * \see _SETbool * * \ingroup std_bool * Prototype : fun [BOOL fun [BOOL I] I] BOOL * * \param BOOL : any BOOL Scol object * \param fun [BOOL I] I : the callback. Its arguments are : * - BOOL : the BOOL Scol object * - the new value (TRUE, FALSE or nil if 'undefined') * * \return BOOL : the same BOOL Scol object **/ fun _CBboolChanged (b, cbfun) = set b.bool_cbchange = cbfun; b;; /*! \brief Define the 'state changed to true' callback for a BOOL Scol object. * It can be nil for no call (default). * * It will be called when the value becomes TRUE. * * \see _CBboolChanged * \see _CBboolFalse * \see _CBboolUndefined * \see _SETbool * * \ingroup std_bool * Prototype : fun [BOOL fun [BOOL] I] BOOL * * \param BOOL : any BOOL Scol object * \param fun [BOOL] I : the callback. Its arguments is : * - BOOL : the BOOL Scol object * * \return BOOL : the same BOOL Scol object **/ fun _CBboolTrue (b, cbfun) = set b.bool_cbtrue = cbfun; b;; /*! \brief Define the 'state changed to false' callback for a BOOL Scol object. * It can be nil for no call (default). * * It will be called when the value becomes FALSE. * * \see _CBboolChanged * \see _CBboolTrue * \see _CBboolUndefined * \see _SETbool * * \ingroup std_bool * Prototype : fun [BOOL fun [BOOL] I] BOOL * * \param BOOL : any BOOL Scol object * \param fun [BOOL] I : the callback. Its arguments is : * - BOOL : the BOOL Scol object * * \return BOOL : the same BOOL Scol object **/ fun _CBboolFalse (b, cbfun) = set b.bool_cbfalse = cbfun; b;; /*! \brief Define the 'state changed to undefined' callback for a BOOL Scol object. * It can be nil for no call (default). * * It will be called when the value becomes 'undefined'. * * \see _CBboolChanged * \see _CBboolTrue * \see _CBboolFalse * \see _SETbool * * \ingroup std_bool * Prototype : fun [BOOL fun [BOOL] I] BOOL * * \param BOOL : any BOOL Scol object * \param fun [BOOL] I : the callback. Its arguments is : * - BOOL : the BOOL Scol object * * \return BOOL : the same BOOL Scol object **/ fun _CBboolUndefined (b, cbfun) = set b.bool_cbUndefined = cbfun; b;; /*! \brief Return the logical AND operator between two integers (&&) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result (TRUE or FALSE) or nil if one of the values is nil **/ fun AND (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else if (v1 && v2) then TRUE else FALSE;; /*! \brief Return the logical OR operator between two integers (||) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result (TRUE or FALSE) or nil if one of the values is nil **/ fun OR (v1, v2) = if (v1 == nil) || (v2 == nil) then nil else if (v1 || v2) then TRUE else FALSE;; /*! \brief Return the logical NOT operator to an integer (!) * * \ingroup std_bool * Prototype : fun [I] I * * \param I : an integer * \return I : the result or nil if the value is nil **/ fun NOT (b) = if b == nil then nil else !b;; /*! \brief Return the logical IMPLICATION * * Example : If i live in French then i live in Europe. So if i don't live * in Europe then i don't live in French but if i live in Europe i can not * tell i live in French. * * - 0 0 -> 1 * - 0 1 -> 1 * - 1 0 -> 0 * - 1 1 -> 1 * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : an integer * \return I : the result or nil if the values are nil **/ fun IMP (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else (!v1) || v2;; /*! \brief Return the logical INHIBITION * * If the first item is true then the expression is true except if the * second item is also true. * * - 0 0 -> 0 * - 0 1 -> 0 * - 1 0 -> 1 * - 1 1 -> 0 * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : an integer * \return I : the result or nil if the values are nil **/ fun INH (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else v1 && (!v2);; /*! \brief Return the bit OR operator between two integers (&) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun BAND (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else v1&v2;; /*! \brief Return the bit OR operator between two integers (|) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun BOR (v1, v2) = if (v1 == nil) || (v2 == nil) then nil else v1|v2;; /*! \brief Return the bit XOR operator between two integers (^) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun BXOR (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else v1^v2;; /*! \brief Return the bit NOT operator to an integer (~) * * \ingroup std_bool * Prototype : fun [I] I * * \param I : an integer * \return I : the result or nil if the value is nil **/ fun BNOT (v)= if v == nil then nil else (~v);; /*! \brief Return the bit SHIFT LEFT operator between two integers (<<) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun SHLEFT (v1, v2) = if (v1 == nil) || (v2 == nil) then nil else v1<>) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun SHRIGHT (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else (v1>>v2)&0xfffffffe;; /*! \brief Return the EQUALITY between two value (==) * * \ingroup std_bool * Prototype : fun [u0 u0] I * * \param u0 : an integer * \param u0 : another integer * \return I : the result **/ fun EQ (v1, v2) = if (v1 == v2) then TRUE else FALSE;; /*! \brief Return the NON-EQUALITY between two value (!=) * * \ingroup std_bool * Prototype : fun [u0 u0] I * * \param u0 : an integer * \param u0 : another integer * \return I : the result **/ fun NEQ (v1, v2) = if (v1 != v2) then TRUE else FALSE;; /*! \brief Return the LOWER operator between two integers (<) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun LOWER (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else if v1 < v2 then TRUE else FALSE;; /*! \brief Return the UPPER operator between two integers (>) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun UPPER (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else if v1 > v2 then TRUE else FALSE;; /*! \brief Return the LOWER OR EQUAL operator between two integers (<=) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun LOWEREQ (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else if v1 <= v2 then TRUE else FALSE;; /*! \brief Return the UPPER OR EQUAL operator between two integers (>=) * * \ingroup std_bool * Prototype : fun [I I] I * * \param I : an integer * \param I : another integer * \return I : the result or nil if one of the values is nil **/ fun UPPEREQ (v1, v2)= if (v1 == nil) || (v2 == nil) then nil else if v1 >= v2 then TRUE else FALSE;; /*! \brief Return the NEGATION operator to an integer (-) * * \ingroup std_bool * Prototype : fun [I] I * * \param I : an integer * \return I : the result or nil if one of the values is nil **/ fun NEG (b) = if b == nil then nil else (-b);;