My Project
programmer's documentation
Functions
mei_evaluate.c File Reference

Build an interpreter for a mathematical expression. More...

#include "cs_defs.h"
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "bft_mem.h"
#include "bft_printf.h"
#include "bft_error.h"
#include "mei_node.h"
#include "mei_parser_glob.h"
#include "mei_parser.h"
#include "mei_evaluate.h"
Include dependency graph for mei_evaluate.c:

Functions

mei_tree_t * mei_tree_new (const char *expr)
 Returns a new interpreter. The node member is empty, the string member contains the mathematical expression and the symbol table is initialize with the standard symbols. More...
 
mei_tree_t * mei_tree_new_with_shared_symbols (const char *const expr, hash_table_t *const symbol_table)
 Returns a new interpreter. The node member is empty, the string member contains the mathematical expression and the symbol table is initialize with a existing table, which is shared by multiple interpreter. More...
 
hash_table_t * mei_table_symbols_new (void)
 Returns a new table of symbols. The table contains standard mathematical symbols. More...
 
int mei_tree_builder (mei_tree_t *ev)
 Call the yacc parser. Return 0 if the parsing is ok, or the number of errors, if errors occurs. More...
 
void mei_tree_insert (mei_tree_t *ev, const char *str, const double value)
 Inserts a constant (label and value) in the table of symbols associated to an interpreter. More...
 
void mei_symbol_table_insert (hash_table_t *symbol_table, const char *str, const double value)
 Inserts a constant (label and value) in a table of symbols. More...
 
int mei_tree_find_symbol (mei_tree_t *ev, const char *str)
 Check if the symbol str exists in the expression stored in ev. More...
 
int mei_tree_find_symbols (mei_tree_t *ev, const int size, const char **symbol)
 Check if the symbol str from a list exists in the expression. The list of missing symbols is stored in ev->labels. More...
 
double mei_tree_lookup (mei_tree_t *ev, const char *str)
 Returns a value of the str symbol (variable or constant) from table of symbols of ev interpreter. More...
 
double mei_evaluate (mei_tree_t *ev)
 Evaluates the expression ev : 1) computes all values for variables inside the expression 2) returns a value from the intrepreted expression. More...
 
void mei_tree_destroy (mei_tree_t *ev)
 Free memory and return NULL. More...
 

Detailed Description

Build an interpreter for a mathematical expression.

Function Documentation

◆ mei_evaluate()

double mei_evaluate ( mei_tree_t *  ev)

Evaluates the expression ev : 1) computes all values for variables inside the expression 2) returns a value from the intrepreted expression.

Parameters
[in]evinterpreter
Returns
value from the intrepreted expression

◆ mei_symbol_table_insert()

void mei_symbol_table_insert ( hash_table_t *  symbol_table,
const char *  str,
const double  value 
)

Inserts a constant (label and value) in a table of symbols.

Parameters
[in]symbol_tabletable of symbols
[in]strlabel of the constant
[in]valuevalue associated to the constant

◆ mei_table_symbols_new()

hash_table_t* mei_table_symbols_new ( void  )

Returns a new table of symbols. The table contains standard mathematical symbols.

Returns
table of symbols

◆ mei_tree_builder()

int mei_tree_builder ( mei_tree_t *  ev)

Call the yacc parser. Return 0 if the parsing is ok, or the number of errors, if errors occurs.

Parameters
[in]evinterpreter
Returns
number of parsing errors

◆ mei_tree_destroy()

void mei_tree_destroy ( mei_tree_t *  ev)

Free memory and return NULL.

Parameters
[in]evinterpreter

◆ mei_tree_find_symbol()

int mei_tree_find_symbol ( mei_tree_t *  ev,
const char *  str 
)

Check if the symbol str exists in the expression stored in ev.

Parameters
[in]evinterpreter in which we want to know if str exists
[in]strsymbol to find
Returns
0 if the symbol exists in the symbol table, 1 if not

◆ mei_tree_find_symbols()

int mei_tree_find_symbols ( mei_tree_t *  ev,
const int  size,
const char **  symbol 
)

Check if the symbol str from a list exists in the expression. The list of missing symbols is stored in ev->labels.

Parameters
[in]evinterpreter
[in]sizenumber of symbols to check
[in]symbollist of symbols
Returns
0 if all symbols exist, otherwise return the number of errors

◆ mei_tree_insert()

void mei_tree_insert ( mei_tree_t *  ev,
const char *  str,
const double  value 
)

Inserts a constant (label and value) in the table of symbols associated to an interpreter.

Parameters
[in]evinterpreter
[in]strlabel of the constant
[in]valuevalue associated to the constant

◆ mei_tree_lookup()

double mei_tree_lookup ( mei_tree_t *  ev,
const char *  str 
)

Returns a value of the str symbol (variable or constant) from table of symbols of ev interpreter.

Parameters
[in]evinterpreter
[in]strname of the symbol
Returns
value of a symbol

◆ mei_tree_new()

mei_tree_t* mei_tree_new ( const char *  expr)

Returns a new interpreter. The node member is empty, the string member contains the mathematical expression and the symbol table is initialize with the standard symbols.

Parameters
[in]exprstring characters of the mathematical expression
Returns
new empty interpreter

◆ mei_tree_new_with_shared_symbols()

mei_tree_t* mei_tree_new_with_shared_symbols ( const char *const  expr,
hash_table_t *const  symbol_table 
)

Returns a new interpreter. The node member is empty, the string member contains the mathematical expression and the symbol table is initialize with a existing table, which is shared by multiple interpreter.

Parameters
[in]exprstring characters of the mathematical expression
[in]symbol_tableshared table of symbols
Returns
new empty interpreter