Sagui library v3.5.0
Cross-platform library which helps to develop web servers or frameworks.
Loading...
Searching...
No Matches
Data Structures | Typedefs | Enumerations | Functions
Math expression evaluator

Data Structures

struct  sg_expr_extension
 
struct  sg_expr
 
struct  sg_expr_argument
 

Typedefs

typedef double(* sg_expr_func) (void *cls, struct sg_expr_argument *args, const char *identifier)
 

Enumerations

enum  sg_expr_err_type {
  SG_EXPR_ERR_UNKNOWN , SG_EXPR_ERR_UNEXPECTED_NUMBER , SG_EXPR_ERR_UNEXPECTED_WORD , SG_EXPR_ERR_UNEXPECTED_PARENS ,
  SG_EXPR_ERR_MISSING_OPERAND , SG_EXPR_ERR_UNKNOWN_OPERATOR , SG_EXPR_ERR_INVALID_FUNC_NAME , SG_EXPR_ERR_BAD_PARENS ,
  SG_EXPR_ERR_TOO_FEW_FUNC_ARGS , SG_EXPR_ERR_FIRST_ARG_IS_NOT_VAR , SG_EXPR_ERR_BAD_VARIABLE_NAME , SG_EXPR_ERR_BAD_ASSIGNMENT
}
 

Functions

struct sg_exprsg_expr_new (void) __attribute__((malloc))
 
void sg_expr_free (struct sg_expr *expr)
 
int sg_expr_compile (struct sg_expr *expr, const char *str, size_t len, struct sg_expr_extension *extensions)
 
int sg_expr_clear (struct sg_expr *expr)
 
double sg_expr_eval (struct sg_expr *expr)
 
double sg_expr_var (struct sg_expr *expr, const char *name, size_t len)
 
int sg_expr_set_var (struct sg_expr *expr, const char *name, size_t len, double val)
 
double sg_expr_arg (struct sg_expr_argument *args, int index)
 
int sg_expr_near (struct sg_expr *expr)
 
enum sg_expr_err_type sg_expr_err (struct sg_expr *expr)
 
const char * sg_expr_strerror (struct sg_expr *expr)
 
double sg_expr_calc (const char *str, size_t len)
 

Detailed Description

Mathematical expression evaluator.

Typedef Documentation

◆ sg_expr_func

typedef double(* sg_expr_func) (void *cls, struct sg_expr_argument *args, const char *identifier)

Callback signature to specify a function at build time to be executed at run time in a mathematical expression.

Parameters
[out]clsUser-defined closure.
[out]argsFloating-point arguments passed to the function.
[out]identifierNull-terminated string to identify the function.
Returns
Floating-point value calculated at build time to be used at run time in a mathematical expression.

Enumeration Type Documentation

◆ sg_expr_err_type

Possible error types returned by the mathematical expression evaluator.

Enumerator
SG_EXPR_ERR_UNKNOWN 

Error not related to evaluation, e.g. EINVAL.

SG_EXPR_ERR_UNEXPECTED_NUMBER 

Unexpected number, e.g. "(1+2)3".

SG_EXPR_ERR_UNEXPECTED_WORD 

Unexpected word, e.g. "(1+2)x".

SG_EXPR_ERR_UNEXPECTED_PARENS 

Unexpected parenthesis, e.g. "1(2+3)".

SG_EXPR_ERR_MISSING_OPERAND 

Missing expected operand, e.g. "0^+1".

SG_EXPR_ERR_UNKNOWN_OPERATOR 

Unknown operator, e.g. "(1+2).".

SG_EXPR_ERR_INVALID_FUNC_NAME 

Invalid function name, e.g. "unknownfunc()".

SG_EXPR_ERR_BAD_PARENS 

Bad parenthesis, e.g. "(1+2".

SG_EXPR_ERR_TOO_FEW_FUNC_ARGS 

Too few arguments passed to a macro, e.g. "$()".

SG_EXPR_ERR_FIRST_ARG_IS_NOT_VAR 

First macro argument is not variable, e.g. "$(1)".

SG_EXPR_ERR_BAD_VARIABLE_NAME 

Bad variable name, e.g. "2.3.4".

SG_EXPR_ERR_BAD_ASSIGNMENT 

Bad assignment, e.g. "2=3".

Function Documentation

◆ sg_expr_new()

struct sg_expr * sg_expr_new ( void  )

Creates a new mathematical expression evaluator handle.

Returns
New mathematical expression evaluator handle.
Return values
NULLIf no memory space is available and set the errno to ENOMEM.
Examples
example_expr_full.c.

◆ sg_expr_free()

void sg_expr_free ( struct sg_expr expr)

Frees the mathematical expression evaluator handle previously allocated by sg_expr_new().

Parameters
[in]exprExpression evaluator handle.
Examples
example_expr_full.c.

◆ sg_expr_compile()

int sg_expr_compile ( struct sg_expr expr,
const char *  str,
size_t  len,
struct sg_expr_extension extensions 
)

Compiles a mathematical expression allowing to declare variables, macros and extensions.

Parameters
[in]exprMathematical expression instance.
[in]strNull-terminated string with the mathematical expression to be compiled.
[in]lenLength of the mathematical expression to be compiled.
[in]extensionsArray of extensions to extend the evaluator.
Note
The extension array must be terminated by a zeroed item.
Return values
0Success.
EINVALInvalid argument.
ENOENTMissing a needed extenssion.
EALREADYMathematical expression already compiled.
ENOMEMOut of memory.
Examples
example_expr_full.c.

◆ sg_expr_clear()

int sg_expr_clear ( struct sg_expr expr)

Clears a mathematical expression instance.

Parameters
[in]exprMathematical expression instance.
Return values
0Success.
EINVALInvalid argument.

◆ sg_expr_eval()

double sg_expr_eval ( struct sg_expr expr)

Evaluates a compiled mathematical expression.

Parameters
[in]exprCompiled mathematical expression.
Returns
Floating-point value of the evaluated mathematical expression.
Return values
NANIf the expr is null and set the errno to EINVAL.
Examples
example_expr_full.c.

◆ sg_expr_var()

double sg_expr_var ( struct sg_expr expr,
const char *  name,
size_t  len 
)

Gets the value of a declared variable.

Parameters
[in]exprMathematical expression instance.
[in]nameName of the declared variable.
[in]lenLength of the variable name.
Return values
NAN
  • If expr or name is null, or len is less than one, and set the errno to EINVAL.
  • If no memory space is available and set the errno to ENOMEM.

◆ sg_expr_set_var()

int sg_expr_set_var ( struct sg_expr expr,
const char *  name,
size_t  len,
double  val 
)

Sets a variable to the mathematical expression.

Parameters
[in]exprMathematical expression instance.
[in]nameName for the variable.
[in]lenLength of the variable name.
[in]valValue for the variable.
Return values
0Success.
EINVALInvalid argument.
ENOMEMOut of memory.

◆ sg_expr_arg()

double sg_expr_arg ( struct sg_expr_argument args,
int  index 
)

Gets a function argument by its index.

Parameters
[in]argsArguments list.
[in]indexArgument index.
Return values
NANIf the args is null or index is less than zero and set the errno to EINVAL.
Examples
example_expr_full.c.

◆ sg_expr_near()

int sg_expr_near ( struct sg_expr expr)

Returns the nearby position of an error in the mathematical expression.

Parameters
[in]exprCompiled mathematical expression.
Return values
0Success.
EINVALInvalid argument.
Examples
example_expr_full.c.

◆ sg_expr_err()

enum sg_expr_err_type sg_expr_err ( struct sg_expr expr)

Returns the type of an error in the mathematical expression.

Parameters
[in]exprCompiled mathematical expression.
Returns
Enumerator representing the error type.
Return values
SG_EXPR_ERR_UNKNOWNIf the expr is null or an unknown error occurred and set the errno to EINVAL.

◆ sg_expr_strerror()

const char * sg_expr_strerror ( struct sg_expr expr)

Returns the description of an error in the mathematical expression.

Parameters
[in]exprCompiled mathematical expression.
Returns
Description of the error as a null-terminated string.
Return values
NULLIf the expr is null and set the errno to EINVAL.
Examples
example_expr_full.c.

◆ sg_expr_calc()

double sg_expr_calc ( const char *  str,
size_t  len 
)

Returns the evaluated value of a mathematical expression.

Parameters
[in]strNull-terminated string with the mathematical expression to be evaluated.
[in]lenLength of the mathematical expression to be evaluated.
Return values
NANIf str is null, or len is less than one, and set the errno to EINVAL.
Examples
example_expr_basic.c.