HPCToolkit
ExprEval.hpp
Go to the documentation of this file.
1
// This file is a modified version from Expression Evaluator published at
2
// https://www.strchr.com/expression_evaluator
3
4
#ifndef __ExprEval_H__
5
#define __ExprEval_H__
6
7
#include <
lib/support/BaseVarMap.hpp
>
// basic var map class
8
9
// Error codes enumeration
10
enum
EXPR_EVAL_ERR
{
11
EEE_NO_ERROR
= 0,
12
EEE_PARENTHESIS
= 1,
13
EEE_WRONG_CHAR
= 2,
14
EEE_DIVIDE_BY_ZERO
= 3,
15
EEE_INCORRECT_VAR
= 4
16
};
17
18
//typedef char EVAL_CHAR;
19
#define EVAL_CHAR char
20
21
22
// Parser class to evaluate math expression
23
// The math expression has to be simple operators:
24
// +,-,*, /, ( and )
25
// Other than those, it will emit error code
26
class
ExprEval
{
27
private
:
28
EXPR_EVAL_ERR
_err
;
29
EVAL_CHAR
*
_err_pos
;
30
int
_paren_count
;
31
32
// variable maps for value substitution
33
BaseVarMap
*
_var_map
;
34
35
// Parse a number or an expression in parenthesis
36
double
ParseAtom
(
EVAL_CHAR
*& expr) ;
37
38
// Parse multiplication and division
39
double
ParseFactors
(
EVAL_CHAR
*& expr) ;
40
//
41
// parse a sum or substraction
42
double
ParseSummands
(
EVAL_CHAR
*& expr) ;
43
44
public
:
45
// main method to evaluate a math expression
46
double
Eval
(
EVAL_CHAR
* expr,
BaseVarMap
*var_map);
47
48
// get the error code
49
EXPR_EVAL_ERR
GetErr
();
50
51
// the character position of the error
52
EVAL_CHAR
*
GetErrPos
();
53
54
};
55
56
#endif
ExprEval::_var_map
BaseVarMap * _var_map
Definition:
ExprEval.hpp:33
ExprEval::ParseAtom
double ParseAtom(EVAL_CHAR *&expr)
Definition:
ExprEval.cpp:17
EVAL_CHAR
#define EVAL_CHAR
Definition:
ExprEval.hpp:19
ExprEval::Eval
double Eval(EVAL_CHAR *expr, BaseVarMap *var_map)
Definition:
ExprEval.cpp:134
BaseVarMap.hpp
ExprEval::_err
EXPR_EVAL_ERR _err
Definition:
ExprEval.hpp:28
ExprEval
Definition:
ExprEval.hpp:26
EEE_WRONG_CHAR
Definition:
ExprEval.hpp:13
ExprEval::GetErrPos
EVAL_CHAR * GetErrPos()
Definition:
ExprEval.cpp:161
ExprEval::_err_pos
EVAL_CHAR * _err_pos
Definition:
ExprEval.hpp:29
EEE_PARENTHESIS
Definition:
ExprEval.hpp:12
EEE_NO_ERROR
Definition:
ExprEval.hpp:11
BaseVarMap
Definition:
BaseVarMap.hpp:50
ExprEval::ParseSummands
double ParseSummands(EVAL_CHAR *&expr)
Definition:
ExprEval.cpp:114
EEE_INCORRECT_VAR
Definition:
ExprEval.hpp:15
EEE_DIVIDE_BY_ZERO
Definition:
ExprEval.hpp:14
ExprEval::ParseFactors
double ParseFactors(EVAL_CHAR *&expr)
Definition:
ExprEval.cpp:83
ExprEval::GetErr
EXPR_EVAL_ERR GetErr()
Definition:
ExprEval.cpp:156
EXPR_EVAL_ERR
EXPR_EVAL_ERR
Definition:
ExprEval.hpp:10
ExprEval::_paren_count
int _paren_count
Definition:
ExprEval.hpp:30
src
lib
support
ExprEval.hpp
Generated by
1.8.13