From a2d7b6445f2287bd74e09ad4ec530ea2c2449239 Mon Sep 17 00:00:00 2001 From: goodale Date: Sun, 4 Nov 2001 23:01:58 +0000 Subject: New improved, or at least more functional, expression parser and evaluator. Now can deal with both floating point and integer values. You can use the functions from the standard C maths library which take one argument. Evaluation should be faster, 'though parsing is probably a bit slower. Now the user-supplied evaluation routine is called just once at the beginning of the evaluation, so if any of the evaluations need a global operation, these can all be done at once. The routines have now been renamed as Util_ functions and the header file cctki_Expression.h has been renamed as util_Expression.h. IMPORTANT NOTE: The above means you will need to rm configs/*/build/Cactus/util/Expression.c.d rm configs/*/build/Cactus/main/Groups.c.d after updating to get rid of stale dependency files. Tom git-svn-id: http://svn.cactuscode.org/flesh/trunk@2449 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/include/util_Expression.h | 107 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 src/include/util_Expression.h (limited to 'src/include/util_Expression.h') diff --git a/src/include/util_Expression.h b/src/include/util_Expression.h new file mode 100644 index 00000000..85ead356 --- /dev/null +++ b/src/include/util_Expression.h @@ -0,0 +1,107 @@ + /*@@ + @header util_Expression.h + @date Tue Sep 19 22:02:45 2000 + @author Tom Goodale + @desc + Header for expression stuff. + @enddesc + @version $Header$ + @@*/ + +#ifndef __UTIL_EXPRESSION_H__ +#define __UTIL_EXPRESSION_H__ 1 + +#ifdef __cplusplus +extern "C" +{ +#endif + + /* These data types are for internal use only. */ + + /* Defined operators */ +typedef enum {OP_NONE, + OP_EQUALS, + OP_LESS_THAN, + OP_GREATER_THAN, + OP_LEQUALS, + OP_GEQUALS, + OP_AND, + OP_OR, + OP_PLUS, + OP_MINUS, + OP_DIV, + OP_TIMES, + OP_POWER, + OP_ACOS, + OP_ASIN, + OP_ATAN, + OP_CEIL, + OP_COS, + OP_COSH, + OP_EXP, + OP_FABS, + OP_FLOOR, + OP_LOG, + OP_LOG10, + OP_SIN, + OP_SINH, + OP_SQRT, + OP_TAN, + OP_TANH} + uExpressionOpcode; + + /* What sort of expression types we have. */ +typedef enum {val,unary,binary} uExpressionType; + + /* RPN object. */ +typedef struct +{ + uExpressionType type; + + union + { + uExpressionOpcode opcode; + int varnum; + } token; +} uExpressionToken; + + /* Parsed expression object. */ +typedef struct +{ + int ntokens; + uExpressionToken *tokens; + int nvars; + const char **vars; +} uExpressionInternals; + +/* Beginning of externally useable objects. */ + + /* Structure to hold values. */ +typedef struct +{ + enum {rval,ival} type; + + union + { + double rval; + int ival; + } value; +} uExpressionValue; + + /* Externally visible representation of the expression. */ +typedef uExpressionInternals *uExpression; + +uExpression Util_ExpressionParse(const char *expression); + +int Util_ExpressionEvaluate(const uExpression buffer, + uExpressionValue *retval, + int (*eval)(int, const char * const *, uExpressionValue *, void *), + void *data); + +void Util_ExpressionFree(uExpression buffer); + +#ifdef __cplusplus +} +#endif + +#endif /* __UTIL_EXPRESSION_H__ */ -- cgit v1.2.3