OpenSWMM Engine  6.0.0-alpha.1
Data-oriented, plugin-extensible SWMM Engine (6.0.0-alpha.1)
Loading...
Searching...
No Matches
MathExpr.hpp
Go to the documentation of this file.
1
22#ifndef OPENSWMM_MATHEXPR_HPP
23#define OPENSWMM_MATHEXPR_HPP
24
25#include <string>
26#include <vector>
27#include <functional>
28
29namespace openswmm {
30namespace mathexpr {
31
32// ============================================================================
33// Token types
34// ============================================================================
35
36enum class TokenType : int {
37 NUMBER = 0,
38 VARIABLE = 1,
39 ADD = 2,
40 SUB = 3,
41 MUL = 4,
42 DIV = 5,
43 POW = 6,
44 NEG = 7,
45 LPAREN = 8,
46 RPAREN = 9,
47 FUNC_ABS = 10,
48 FUNC_SGN = 11,
49 FUNC_SQRT = 12,
50 FUNC_LOG = 13,
51 FUNC_EXP = 14,
52 FUNC_SIN = 15,
53 FUNC_COS = 16,
54 FUNC_TAN = 17,
55 FUNC_ASIN = 18,
56 FUNC_ACOS = 19,
57 FUNC_ATAN = 20,
58 FUNC_STEP = 21,
59 FUNC_MIN = 22,
60 FUNC_MAX = 23,
61 COMMA = 24
62};
63
64struct Token {
66 double value = 0.0;
67 int var_idx = -1;
68 std::string var_name;
69};
70
71// ============================================================================
72// Compiled expression (postfix token list)
73// ============================================================================
74
75struct Expression {
76 std::vector<Token> postfix;
77 bool valid = false;
78};
79
80// ============================================================================
81// Parser (Shunting-yard)
82// ============================================================================
83
91int parse(const std::string& expr_str, Expression& result);
92
93// ============================================================================
94// Evaluator
95// ============================================================================
96
104double evaluate(const Expression& expr,
105 const std::function<double(const std::string&)>& var_lookup);
106
115double evaluate(const Expression& expr, const double* vars, int n_vars);
116
117} // namespace mathexpr
118} // namespace openswmm
119
120#endif // OPENSWMM_MATHEXPR_HPP
int parse(const std::string &expr_str, Expression &result)
Parse an infix expression string into a postfix Expression.
Definition MathExpr.cpp:128
TokenType
Definition MathExpr.hpp:36
double evaluate(const Expression &expr, const std::function< double(const std::string &)> &var_lookup)
Evaluate a compiled expression with named variable lookup.
Definition MathExpr.cpp:183
Definition Controls.cpp:24
Definition MathExpr.hpp:75
std::vector< Token > postfix
Postfix (RPN) token sequence.
Definition MathExpr.hpp:76
bool valid
Definition MathExpr.hpp:77
Definition MathExpr.hpp:64
TokenType type
Definition MathExpr.hpp:65
std::string var_name
For VARIABLE tokens (name for lookup)
Definition MathExpr.hpp:68
int var_idx
For VARIABLE tokens (index into variable table)
Definition MathExpr.hpp:67
double value
For NUMBER tokens.
Definition MathExpr.hpp:66