/* libsizzle/boolean.c - boolean functions
 
  Copyright (C) 1999, 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
 
  This is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2, or (at your option)
  any later version.
  
  This software 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 General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this package; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  Boston, MA 02111-1307, USA.  */

#if HAVE_CONFIG_H
# include <config.h>
#endif 

#if ENABLE_NLS
# include <libintl.h>
# define _(String) gettext(String)
#else
# define _(String) (String)
#endif

#include "node.h"
#include "validate.h"
#include "capi.h"
#include "boolean.h"

/*:doc
  boolean?
  (boolean? obj) => boolean
  Returns @code{#t} if @var{obj} is a boolean value, @code{#f} otherwise.
  doc:*/
#define FUNC_NAME "boolean?"
static result_t
boolean_p_func (zzz_scm_t env, zzz_scm_t obj, zzz_scm_t * result)
{
  if (bool_p (obj))
    *result = zzz_true_value;
  else
    *result = zzz_false_value;
  return RESULT_SUCCESS;
}
#undef FUNC_NAME

/*:doc
  not
  (not obj) => boolean
  Returns @code{#t} if @var{obj} is false, @code{#f} otherwise.
  doc:*/
#define FUNC_NAME "not"
static result_t
not_func (zzz_scm_t env, zzz_scm_t obj, zzz_scm_t * result)
{
  if (zzz_eq (obj, zzz_false_value))
    *result = zzz_true_value;
  else
    *result = zzz_false_value;
  return RESULT_SUCCESS;
}
#undef FUNC_NAME

/*:doc
  boolean->integer
  (boolean->integer b) => integer
  Return 0 if @var{b} is @code{#f}, 1 otherwise.
  doc:*/
#define FUNC_NAME "boolean->integer"
static result_t
boolean_to_integer_func (zzz_scm_t env, zzz_scm_t bool, zzz_scm_t * result)
{
  static char * s_func_name = FUNC_NAME;

  CHECK_TYPE (1, bool_p (bool), zzz_boolean_type_name);
  *result = zzz_make_integer ((long) bool_val (bool));
  return RESULT_SUCCESS;
}
#undef FUNC_NAME

/*:doc
  integer->boolean
  (integer->boolean i) => boolean
  Return @code{#f} if @var{i} is 0, @code{#t} otherwise.
  doc:*/
#define FUNC_NAME "integer->boolean"
static result_t
integer_to_boolean_func (zzz_scm_t env, zzz_scm_t n, zzz_scm_t * result)
{
  static char * s_func_name = FUNC_NAME;

  CHECK_TYPE (1, integer_p (n), zzz_integer_type_name);
  *result = (integer_val (n) != 0) ? zzz_true_value : zzz_false_value;
  return RESULT_SUCCESS;
}
#undef FUNC_NAME

result_t
zzz_init_boolean (void)
{
  zzz_define_function ("boolean?", (zzz_prim_t) boolean_p_func, argv_1_0_0);
  zzz_define_function ("not", (zzz_prim_t) not_func, argv_1_0_0);
  zzz_define_function ("boolean->integer",
		       (zzz_prim_t) boolean_to_integer_func, argv_1_0_0);
  zzz_define_function ("integer->boolean",
		       (zzz_prim_t) integer_to_boolean_func, argv_1_0_0);
  return RESULT_SUCCESS;
}
