SCalc
|
A class representing a whole session. More...
#include <session.hh>
Public Member Functions | |
Session () | |
~Session () | |
ParserResult * | eval (const char *) |
Evaluates a string. More... | |
void | eval_and_free (const char *) |
Evaluates a string and frees the result if possible. More... | |
int | register_varname (const std::string &str) |
Registers a variable name and returns its number. More... | |
int | register_varname (const char *str) |
const char * | varname (int i) |
Returns the name of variable number i. | |
std::vector< std::string > | varnames () |
Returns an array of variable names. More... | |
int | nb_vars_defined () |
Returns the number of currently defined variables. | |
int | register_func_def (FuncDef *) |
int | replace_func_def (FuncDef *) |
int | get_func (std::string) |
Get the corresponding function number (or -1 if not defined). | |
int | get_func (const char *name) |
Get the corresponding function number (or -1 if not defined). | |
FuncDef * | get_func_def (std::string) |
Get the function definition of the given name. | |
FuncDef * | get_func_def (const char *name) |
Overloaded function for convenience. | |
int | nb_args_func (std::string) |
Returns the number of arguments of a function. | |
int | nb_args_func (const char *name) |
Overloaded for convenience. | |
int | nb_funcs () |
The number of defined functions. | |
std::vector< std::string > | func_names () |
The function names. | |
Expression * | constant (double value) |
Variables with 'default values' | |
A set of functions dealing with 'default values' of variables, that is variables which were assigned using the "a = 2" syntax, or manually using set_var(). | |
int | set_var (int, double) |
int | set_var (const char *var, double val) |
int | set_var (const std::string &var, double val) |
int | unset_var (int) |
int | unset_var (std::string varname) |
Unset a variable of the given name. | |
int | unset_var (const char *varname) |
int | nb_vars_set () |
Returns the number of variables that have a value. | |
std::set< int > | vars_set () |
void | fill_default (double *) |
int | evaluable (Expression *expr) |
Protected Attributes | |
std::vector< std::string > | variables |
The variables defined. | |
std::map< std::string, int > | variables_numbers |
The cross reference to the variables. | |
std::map< int, double > | values |
std::map< std::string, int > | functions_numbers |
std::vector< FuncDef * > | functions |
The function vector: | |
A class representing a whole session.
The very class that keeps tracks of one session, such as declared function and variables. This is your main entry point to SCalc. Typical example:
The main function you'll be interested in is eval() to evaluate a string. It returns a ParserResult object which you can further interrogate and use.
Each Session holds its own set of variables and function definitions. It is probably a very bad idea to try to share function definitions across several different Session objects.
SCalc::Session::Session | ( | ) |
Creates a new Session object and register common mathematical functions.
SCalc::Session::~Session | ( | ) |
Destroys a Session object and frees associated function definitions.
Expression* SCalc::Session::constant | ( | double | value | ) |
Creates a new Expression representing a simple constant value. Typical use:
Referenced by nb_funcs().
ParserResult* SCalc::Session::eval | ( | const char * | ) |
Evaluates a string.
eval() returns a ParserResult object, which needs to be checked and cast into the right object, using ParserResult::is_func_def(), ParserResult::is_expression() or ParserResult::is_syntax_error().
You are responsible for freeing the object if you don't need it but you must check that is it not in use (such as function definitions) using ParserResult::can_delete():
void SCalc::Session::eval_and_free | ( | const char * | ) |
Evaluates a string and frees the result if possible.
This function evaluates a string and frees the result if that is possible. Of course, as the function returns void, it is not currently possible to tell fi anything went fine, so you might as well not want to use it. It is used by the test suite to setup functions.
int SCalc::Session::evaluable | ( | Expression * | expr | ) |
Tells if we have enough default variables to evaluate this expression without any other context.
Referenced by SCalc::Expression::evaluable(), and nb_vars_set().
void SCalc::Session::fill_default | ( | double * | ) |
Fills the target array with the default values. Be careful: the argument has to have room for at least nb_vars_defined !
Referenced by nb_vars_set().
int SCalc::Session::register_func_def | ( | FuncDef * | ) |
Registers a function. Fails if a function is already registered under that name, or if the function is anonymous. All the registered functions are deleted when the Session is destroyed.
Referenced by nb_vars_set().
int SCalc::Session::register_varname | ( | const std::string & | str | ) |
Registers a variable name and returns its number.
Registers a new variable name, and returns its index in the array returned by varnames(). If the variable already exists, just return its number: no second registration is done.
Referenced by nb_vars_defined().
int SCalc::Session::replace_func_def | ( | FuncDef * | ) |
Replaces a function definition. The former occupant of the place is simply deleted. We will need to ensure at some point that no trailing references are left. If the previous definition doesn't exist, simply calls register_func_def().
Referenced by nb_vars_set().
int SCalc::Session::set_var | ( | int | , |
double | |||
) |
Sets the (default) value of a variable
Referenced by nb_vars_defined().
int SCalc::Session::unset_var | ( | int | ) |
Unset a given variable. Returns 0 if the variable wasn't previously set.
Referenced by nb_vars_defined().
|
inline |
Returns an array of variable names.
varnames() returns an array of variable names, in the position expected by SCalc::Expression::evaluate().
References variables.
std::set<int> SCalc::Session::vars_set | ( | ) |
Returns the set of the numbers of the variables which have a default value.
Referenced by nb_vars_set().
|
protected |
The currently defined functions. It works this way: The integer is the rank of the corresponding FuncDef pointe in the functions vector. This is done so that user defined functions can override to some extends the ones that were previously defined...
|
protected |
(default) values of variables, that is values that were assigned using a "a = 1.2" syntax.