Home| general |src.lib|evallib| Eval Index

Eval

Syntax
int Eval(char *sum,double *ptr,int (*dvar)(char *ptr,double *val,void *data),void *vdata,int (*dfun)(char *ptr,int argnum,double *argptr,double *val,void *data),void *fdata);
Header
general/reval.h
Library
reval
Description

The Eval function evaluates a mathematical expression.

The zero terminated string pointed to by the argument sum contains the expression to evaluate. The result is stored at the location pointed to by the argument result.

The argument dvar is a pointer to a function of the form:

int (*dvar) (char *str,double *val,void *data);

This function returns the value of each operand in the expression. The zero terminated string pointed to by str contains the operand. If the operand is a simple numeric value, the function should evaluate it and store the value at the location pointed to by val. If the operand is a variable name the function should set val to the value of this variable.

The vdata argument of the eval function is passed directly as the data argument of the dvar function.

If the operand is successfully evaluated the function should return zero, otherwise a non-zero value should be returned.

The argument dfun is a pointer to a function of the form:

int (*dfun) (char *str,int argnum,double *argptr,double *val,void *data);

This function evaluates a function call within an expression. The zero terminated string pointed to by str contains the name of the function. The argument argnum gives the number of arguments passed to the function, and the argument argptr contains an array of pointers to the actual arguments. The function should be evaluated and the value returned stored at the location pointed to by val.

The fdata argument of the eval function is passed directly as the data argument of the dfun function.

If the function is successfully evaluated then zero should be returned, otherwise a non-zero value should be returned.

Returns
Returns zero on success. On error, a non-zero value indicating the error is returned.
Errors

On error, a non-zero value indicating the error is returned:

Return ValueMeaning
0Success.
2Mismatched parentheses.
3Unrecognized operand or variable.
4Unrecognize function.
Example

Source Code: Eval.c

/* Eval.c
   ====== 
   Author: R.J.Barnes
 Copyright (c) 2012 The Johns Hopkins University/Applied Physics Laboratory

This file is part of the Radar Software Toolkit (RST).

RST 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 3 of the License, or
(at your option) any later version.

This program 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 program. If not, see <https://www.gnu.org/licenses/>.

Modifications:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "reval.h"

char *sum="cos(2.5)*3.2+x/2.0";
double x=1.0;

int decode_value(char *ptr,double *value,void *data) {
   if ((isdigit(ptr[0])) || (ptr[0]=='.')) {
     *value=atof(ptr);
     return 0;
   }

   if (strcmp(ptr,"x")==0) *value=x;
   else return -1;
   return 0;
}

int decode_function(char *ptr,int argnum,double *argptr,
                    double *value,void *data) {
  if (strcmp(ptr,"cos")==0) {
    *value=cos(argptr[0]);
  }
  return 0;
}

int main(int argc,char *argv[]) {
  int s;
  double value;

  s=Eval(sum,&value,decode_value,NULL,decode_function,NULL);
  if (s == -1)
  {
      fprintf(stderr, "Warning: Eval returned a negative, may be an error'n");
  }
  fprintf(stdout,"%s=%g'n",sum,value);

  return 0;

}