OpenSWMM Engine  6.0.0-alpha.4
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.4)
Loading...
Searching...
No Matches
mathexpr.c File Reference

Parsing and evaluation of mathematical expressions. More...

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mathexpr.h"
Include dependency graph for mathexpr.c:

Classes

struct  TreeNode
 

Macros

#define _CRT_SECURE_NO_DEPRECATE
 
#define MAX_STACK_SIZE   1024
 

Typedefs

typedef struct TreeNode ExprTree
 

Functions

double mathexpr_eval (MathExpr *expr, double(*getVariableValue)(int))
 Evaluate a legacy postfix expression using a local operand stack.
 
void mathexpr_delete (MathExpr *expr)
 Recursively release a complete postfix MathExpr list.
 
MathExpr * mathexpr_create (char *formula, int(*getVar)(char *))
 Compile an infix formula into an owned postfix expression list.
 

Variables

char * MathFunc []
 

Detailed Description

Parsing and evaluation of mathematical expressions.

Author
Caleb Buahin caleb.nosp@m..bua.nosp@m.hin@g.nosp@m.mail.nosp@m..com (OpenSWMM maintenance and extensions)
License
Apache-2.0 for OpenSWMM contributions; original EPA SWMM provenance is retained.

Macro Definition Documentation

◆ _CRT_SECURE_NO_DEPRECATE

#define _CRT_SECURE_NO_DEPRECATE

◆ MAX_STACK_SIZE

#define MAX_STACK_SIZE   1024

Typedef Documentation

◆ ExprTree

typedef struct TreeNode ExprTree

Function Documentation

◆ mathexpr_create()

MathExpr * mathexpr_create ( char * formula,
int(* getVar )(char *) )

Compile an infix formula into an owned postfix expression list.

Parameters
[in]formulaNon-null null-terminated input; identifiers and numeric literals must fit the parser's 255-byte token buffers and strlen must fit in int.
[in]getVarOptional synchronous variable-name-to-index callback; negative indices reject names. The temporary token pointer must not be retained.
Returns
Owned expression head to release with mathexpr_delete(), or NULL for detected syntax/parenthesis errors or an empty result.
Note
The compiled list stores values/indices and evaluates independently of the input/callback. Parsing uses shared globals and is not reentrant or thread-safe; allocation failures are not comprehensively recovered.
Here is the caller graph for this function:

◆ mathexpr_delete()

void mathexpr_delete ( MathExpr * expr)

Recursively release a complete postfix MathExpr list.

Parameters
[in]exprOwned list head, or NULL for no action.
Postcondition
All reachable next-linked nodes are freed. The caller must discard its original pointer; deep expressions consume recursive call-stack space.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mathexpr_eval()

double mathexpr_eval ( MathExpr * expr,
double(* getVariableValue )(int) )

Evaluate a legacy postfix expression using a local operand stack.

Parameters
[in]exprBorrowed valid linked expression; NULL evaluates to zero.
[in]getVariableValueOptional synchronous index-to-value callback; NULL supplies zero for every variable. The callback is not retained.
Returns
Final expression value; malformed stack underflow and NaN results are mapped to zero. Infinite results are not changed.
Precondition
Expression operand counts and peak stack occupancy fit the fixed MAX_STACK_SIZE array; stack overflow is not safely recovered by this routine.
Note
The local stack permits recursive expression evaluation. Domain behavior varies by opcode: negative sqrt and nonpositive log become zero, while other illegal math may become NaN and then zero at the final check.
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ MathFunc

char* MathFunc[]
Initial value:
= {"COS", "SIN", "TAN", "COT", "ABS", "SGN", "SQRT", "LOG", "EXP", "ASIN",
"ACOS", "ATAN", "ACOT", "SINH", "COSH", "TANH", "COTH", "LOG10", "STEP", NULL}