Parsing and evaluation of mathematical expressions.
More...
Go to the source code of this file.
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)
- Copyright
- Copyright (c) 2026 Caleb Buahin. OpenSWMM contributions only; original EPA SWMM credits are retained.
- License
- Apache-2.0 for OpenSWMM contributions; original EPA SWMM provenance is retained.
◆ MathExpr
◆ mathexpr_create()
| MathExpr * mathexpr_create |
( |
char * | formula, |
|
|
int(* | getVar )(char *) ) |
Compile an infix formula into an owned postfix expression list.
- Parameters
-
| [in] | formula | Non-null null-terminated input; identifiers and numeric literals must fit the parser's 255-byte token buffers and strlen must fit in int. |
| [in] | getVar | Optional 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.
◆ mathexpr_delete()
Recursively release a complete postfix MathExpr list.
- Parameters
-
| [in] | expr | Owned 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.
◆ mathexpr_eval()
| double mathexpr_eval |
( |
MathExpr * | expr, |
|
|
double(* | getVariableValue )(int) ) |
Evaluate a legacy postfix expression using a local operand stack.
- Parameters
-
| [in] | expr | Borrowed valid linked expression; NULL evaluates to zero. |
| [in] | getVariableValue | Optional 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.