SPRAAK
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Data Structures | Typedefs | Enumerations | Functions | Variables
feval.c File Reference

Evaluation of expressions and handling of variables. More...

Data Structures

struct  SprVarlist
 

Typedefs

typedef float SprFevalReal
 

Enumerations

enum  {
  SPR_FEVAL_IS_VEC, SPR_FEVAL_IS_STR, SPR_FEVAL_IS_FUN1, SPR_FEVAL_IS_IFX1,
  SPR_FEVAL_IS_IFX2, SPR_FEVAL_IS_FUN2, SPR_FEVAL_IS_FUN3, SPR_FEVAL_IS_FUN,
  SPR_FEVAL_IS_UNDEF, SPR_FEVAL_IS_SCRATCH, SPR_FEVAL_IS_CONST, SPR_FEVAL_IS_PROTECT
}
 
enum  {
  SPR_FEVAL_VAR_UNDEF, SPR_FEVAL_VAR_VAL, SPR_FEVAL_VAR_VEC, SPR_FEVAL_VAR_STR,
  SPR_FEVAL_VAR_FUN1, SPR_FEVAL_VAR_FUN2, SPR_FEVAL_VAR_FUN3
}
 
enum  { SPR_FEVAL_NO_VEC, SPR_FEVAL_NO_IGN, SPR_FEVAL_NO_END, SPR_FEVAL_COMPILE }
 explanation: see feval More...
 
enum  {
  SPR_FEVAL_SUBST_VAR, SPR_FEVAL_SUBST_ENV, SPR_FEVAL_SUBST_ENV_BR, SPR_FEVAL_SUBST_ENV_EXT,
  SPR_FEVAL_SUBST_ENV_EXPR, SPR_FEVAL_SUBST_FREE, SPR_FEVAL_SUBST_DYNSTR, SPR_FEVAL_SUBST_KEEP,
  SPR_FEVAL_SUBST_KEEP_ERR
}
 

Functions

SprVarlistspr_feval_var_init (SprVarlist *var_ptr, const char *name)
 
SprVarlistspr_feval_var_clear (SprVarlist *var_ptr)
 
int spr_feval_result_set (SprVarlist *res_ptr, int type,...)
 
SprVarlistspr_feval_var_new (const char *name, int type,...)
 
SprVarlistspr_feval_var_add (SprVarlist *varlist, SprVarlist *argptr)
 Add an item to the list of known variables. More...
 
void spr_feval_var_free (SprVarlist *var_ptr)
 
SprVarlistspr_feval_var_rm (SprVarlist *varlist, SprVarlist *var_ptr)
 
SprVarlistspr_feval_varlist_free (SprVarlist *varlist, SprVarlist *upto)
 
SprVarlistspr_feval_var_get (SprVarlist *varlist, const char *name, int length)
 
int spr_feval_var_set (SprVarlist *var_ptr, int type,...)
 
SprVarlistspr_feval_var_assign (SprVarlist *varlist, const char *name, int type,...)
 
void spr_feval_whos (SprStream *fd, SprVarlist *varlist, const char *name)
 
void spr_feval_var_print (SprStream *fd, const SprVarlist *var_ptr, const char *rm)
 Print a variable. More...
 
char * spr_feval (const char *eval_str, const char *eval_end, SprVarlist *varlist, SprVarlist *res, int flags)
 
SprFevalReal spr_feval_num (const char *eval_str, const char *eval_end, SprVarlist *varlist, int *err_ret)
 
char * spr_feval_var_substitute (const char *str, SprVarlist *list, int flags)
 

Variables

const SprVarlist spr_feval_empty_res
 
SprVarlistspr_feval_global_var
 

Detailed Description

Evaluation of expressions and handling of variables.

The feval module is an expandable expression evaluator for both scalars and vectors. Next to the expression evaluator, routines are provided to faciliate the use of the expression evaluator in existing programs, and to handle the variable lists used by the expression evaluator.

Except for some vector extensions, the syntax of the expressions is the same as in the program language C. Also the C-convention for indexing is used (the first element has index 0). Strings are stored and handled as vectors. Functions are treated as variables. The only allowable operators for functions are function evaluations (e.g. "f(x)") and assignements (e.g. "f=sin").

The following operators are specific for vector operations:

[vec1,vec2] or [vec1 vec2]
Concatenates the two vectors.
vec:step:rep
Repeat the given vector rep times. Each repitition of vec is augmented with an offset step w.r.t. the previous repitition. The step value can be omitted, it defaults to 1.
Examples:
  1:10          ==   [1,2,3,4,5,6,7,8,9,10]
  0:-1:10       ==   [0,-1,-2,-3,-4,-5,-6,-7,-8,-9]
  1:0:10        ==   [1,1,1,1,1,1,1,1,1,1]
  [0,8]:2:4     ==   [0,8,2,10,4,12,6,14]

All other operators are expanded to work with vectors.

Examples: (assume x=[0,1,2,4], y=[9,1,0,4] and z=[1,2])
  x+y           ==   [9,2,2,8]
  x+1           ==   [1,2,3,5]
  x+z           results in an error
  x == y        ==   [0,1,0,1]
  x == z        results in an error
  x[z] = y[z]   set x[1] to y[1] and x[2] to y[2]
  x[z] = 1      results in an error
  sqrt(y)       ==   [3,1,0,2]
  "abcd"[z]     ==   "bc"

The priority of the operands is:

  brackets and vectors              (), []
  functions and indexing            (), []
  unary operators                   -, ~, !
  power                             ^
  multiplicative operations         *, /, %
  additive operations               +, -
  matrix step and repeat operator   :
  relational operators              ==, !=, >=, <= ,> ,<
  binary and                        &
  binary or                         |
  logical and                       &&
  logical or                        ||
  matrix concatenation operator     ,
  function argument separator       ,
  assignment operators              =, ^=, *=, /=, %=, +=, -=, &=, |=
  left to rigth evaluation          ,

Assignments and unary operators are evaluated from right to left, all other operators are evaluated from left to right.

At this moment, the following constants and functions are defined:

 - E                          base of natural logarithms
 - PI                         3.1415926535897...
 - EPS                        floating point relative accuracy
 - MIN                        minimal floating point value
 - MAX                        maximal floating point value
 - sin, cos, tan              triogoniometric functions
 - asin, acos, atan           inv. triogoniometric functions
 - exp, log, log10            exponent and logarithms
 - sqrt                       square root
 - floor, ceil, round         conversion to integer values
 - abs                        absolute value
 - sum, prod                  sum/product of a vector
 - cumsum, cumprod            cumulative sum/product
 - norm                       euclidian length of a vector
 - max, min, imax, imin       find maximum or minimum
 - sort, isort                sort the elements in a vector
 - size                       the size of a vector
 - isscalar, isvec, isvar,    type/attributes of a variable
   isstr, isfun, isundef
 - strcmp(str1,str2)          compare two strings
Date
5 Oct 1994
Author
Kris Demuynck
Revision History:
10/02/95 - KD
Added functionality for vector operations.
03/03/95 - KD
Added functionality for functions (user defined).