/* * The Spar Library - modular math parser * Copyright (C) 2000,2001 Davide Angelocola * * This library 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.1 of the License. * * This library 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 library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * */ /** * @file sl_parser.h * @brief Parser interface * * This file contains all definitions to deal with the parser, however is * still incomplete. * * @todo Add a function that print the result to a string */ #ifndef _sl_parser_h_ #define _sl_parser_h_ #include #include typedef enum { SL_FUNCTION_WITH_ONE_ARG = 1, SL_FUNCTION_WITH_TWO_ARG, SL_FUNCTION_WITH_VAR_ARG, SL_FUNCTION_WITH_COUNTER } sl_function_args_t; typedef enum { SL_FUNCTION_CLASS_TRIG, SL_FUNCTION_CLASS_ITRIG, SL_FUNCTION_CLASS_EXP, SL_FUNCTION_CLASS_LOG, SL_FUNCTION_CLASS_HYPER, SL_FUNCTION_CLASS_SPECIAL, SL_FUNCTION_CLASS_USER, SL_FUNCTION_CLASS_COUNTER } sl_function_class_t; typedef double (*sl_function_p) (double); typedef double (*sl_function_tp) (double, double); struct sl_function_s { sl_identifier (name); sl_message (math); sl_function_args_t args; sl_function_class_t type; sl_function_p func; sl_function_tp func2; }; typedef struct sl_function_s sl_function; struct sl_function_help_s { sl_identifier (name); sl_message (math); sl_message (type); sl_function_args_t args; }; typedef struct sl_function_help_s sl_function_help; /** * Constant structure */ struct sl_constant_s { sl_identifier (name); double value; sl_message (msg); }; typedef struct sl_constant_s sl_constant; struct variable_s { sl_identifier (name); /* variable name */ sl_message (comment); /* comment */ double value; /* value */ double old_value; /* old_value */ struct variable_s *l; struct variable_s *r; }; typedef struct variable_s sl_variable; __BEGIN_DECLS /** * @fn double sl_evaluate_to_double (const char *str) * @brief Evaluate the string str * @param str The string to evaluate * @return Returns the string's value on success * * This function returns directly the string value and should be used * is the most of cases. If there are any error, returns an intermediate * result and prints an error message (@see parser_throw_error()) */ double sl_parser_evaluate_to_double (const char *str); /** * @fn sl_error sl_parser_evaluate (const char *str) * @brief Evaluate the string str * @param str The string to evaluate * @return Returns the value NO_ERROR on success, otherwise the error code * * Evaluate a string and returns the error code (@see sl_error) * If there are any error, returns an intermediate * result and prints an error message (@see sl_error_throw()) */ sl_error sl_parser_evaluate (const char *str); /** * Gets the constant "name" and puts his value into "*value". * Returns SL_SUCCESS on success, otherwise SL_ERROR. */ int sl_constant_get (const char *name, double *value); /** * Prints the constants table using the table sublibs. */ void sl_constant_print_all (void); /** * Sort the constants table to make possible a binary search. * Returns SL_SUCCESS on success, otherwise SL_ERROR. */ int sl_constant_init (void); /* * Variable table */ void sl_variable_edit_comment (sl_variable * entry, const char *comment); void sl_variable_edit_name (sl_variable * entry, const char *name); void sl_variable_edit_value (sl_variable * entry, double value); void sl_variable_old_value (sl_variable * entry); int sl_variable_comment (const char name[], const char comment[]); int sl_variable_init (void); int sl_variable_remove_all (void); int sl_variable_remove (const char name[]); int sl_variable_old (const char name[]); int sl_variable_get (const char name[], double *r); int sl_variable_set (const char name[], double value); void sl_variable_print_all (void); /* * Function table * */ int sl_function_init (void); double sl_function_run (const char *name, double argv[]); sl_function_args_t sl_function_get_argc (int); const char *sl_function_get_name (int code); int sl_function_check (const char *name); void sl_function_print_all (void); int sl_function_make_help (const char *name, sl_function_help * tmp); __END_DECLS #endif /* _parser_h_ */