/*
	Copyright (C) 2022-2024 Mark Tyler

	This program 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 3 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program in the file COPYING.
*/

#include "nush_numeval.h"



void NumEval::set_mode ( int const mode )
{
	switch ( mode )
	{
	case MODE_DOUBLE:
	case MODE_FLOAT:
	case MODE_INTEGER:
	case MODE_RATIONAL:
		m_num_mode = mode;
		break;
	default:
		std::cerr << "Bad number mode - " << mode << "\n";
		return;
	}
}

void NumEval::set_number_size ( int const size )
{
	int const sz = mtkit_int_bound( size, NUMBER_SIZE_MIN, NUMBER_SIZE_MAX);
	m_num_bits = (mpfr_prec_t(1)) << sz;

	// This is needed so the result is set to the new precision for
	// printing the significant digits
	mpfr_set_default_prec ( m_num_bits );
	m_parser_float.evaluate ( "0" );
}

int NumEval::evaluate ( char const * const text )
{
	switch ( m_num_mode )
	{
	case MODE_DOUBLE:
		return evaluate ( text, m_parser_double );

	case MODE_FLOAT:
		mpfr_set_default_prec ( m_num_bits );
		return evaluate ( text, m_parser_float );

	case MODE_INTEGER:
		return evaluate ( text, m_parser_integer );

	case MODE_RATIONAL:
		return evaluate ( text, m_parser_rational );
	}

	return 1;
}

std::string NumEval::get_result () const
{
	std::string res;

	switch ( m_num_mode )
	{
	case MODE_DOUBLE:
		res = m_parser_double.result().to_string();
		break;

	case MODE_FLOAT:
		res = m_parser_float.result().to_string();
		break;

	case MODE_INTEGER:
		res = m_parser_integer.result().to_string();
		break;

	case MODE_RATIONAL:
		res = m_parser_rational.result().to_string();
		break;
	}

	res += "\n";

	return res;
}

size_t NumEval::get_float_digit_total () const
{
	return m_parser_float.result().get_str_ndigits();
}



template < typename Tparser >
int print_funcs ( Tparser & parser )
{
	std::map<std::string, std::string> map;
	char	const	* name;
	std::string	help;

	for ( int i = 0; ; i++ )
	{
		if ( parser.get_function_data ( i, &name, help ) )
		{
			break;
		}

		map[ name ] = help;
	}

	for ( auto && it : map )
	{
		std::cout << it.first << " " << it.second << "\n";
	}

	return 0;
}

int NumEval::print_funcs () const
{
	switch ( m_num_mode )
	{
	case MODE_DOUBLE:	return ::print_funcs ( m_parser_double );
	case MODE_FLOAT:	return ::print_funcs ( m_parser_float );
	case MODE_INTEGER:	return ::print_funcs ( m_parser_integer );
	case MODE_RATIONAL:	return ::print_funcs ( m_parser_rational );
	}

	return 1;
}



template < typename Tparser >
int print_vars ( Tparser & parser )
{
	auto const & vars = parser.variables ();

	for ( auto && it : vars )
	{
		std::cout << it.first << " = " << it.second.to_string() << "\n";
	}

	return 0;
}

int NumEval::print_vars () const
{
	switch ( m_num_mode )
	{
	case MODE_DOUBLE:	return ::print_vars ( m_parser_double );
	case MODE_FLOAT:	return ::print_vars ( m_parser_float );
	case MODE_INTEGER:	return ::print_vars ( m_parser_integer );
	case MODE_RATIONAL:	return ::print_vars ( m_parser_rational );
	}

	return 1;
}

