HIPO  4.3.0
High Performance Output data format for experimental physics
hipo::Parser Class Reference

Mathematical expression parser and evaluator. More...

#include <parser.h>

Public Types

enum  TokenType {
  NONE , NAME , NUMBER , END ,
  PLUS ='+' , MINUS ='-' , MULTIPLY ='*' , DIVIDE ='/' ,
  ASSIGN ='=' , LHPAREN ='(' , RHPAREN =')' , COMMA =',' ,
  NOT ='!' , LT ='<' , GT ='>' , LE ,
  GE , EQ , NE , AND ,
  OR , ASSIGN_ADD , ASSIGN_SUB , ASSIGN_MUL ,
  ASSIGN_DIV
}
 Token types used by the expression lexer. More...
 

Public Member Functions

 Parser (const std::string &program)
 Construct a parser with the given expression. More...
 
const double Evaluate ()
 Evaluate the stored expression using current symbol values. More...
 
const double Evaluate (const std::string &program)
 Evaluate a new expression using current symbol values. More...
 
double & operator[] (const std::string &key)
 Access or create a symbol (variable) by name. More...
 

Public Attributes

std::map< std::string, double > symbols_
 Symbol table mapping variable names to values. Can be accessed directly. More...
 

Detailed Description

Mathematical expression parser and evaluator.

Parses and evaluates arithmetic expressions with support for variables, comparisons, logical operators, and standard math functions. Used internally by hipo::bank::rowlist::filter for expression-based filtering.

hipo::Parser p("px*px + py*py");
p["px"] = 0.5;
p["py"] = 0.3;
double result = p.Evaluate();
Mathematical expression parser and evaluator.
Definition: parser.h:43

Definition at line 42 of file parser.h.

Member Enumeration Documentation

◆ TokenType

Token types used by the expression lexer.

Represents all possible token categories including literals (NUMBER, NAME), operators (arithmetic, comparison, logical), and delimiters.

Enumerator
NONE 

No token (error state)

NAME 

Identifier or variable name.

NUMBER 

Numeric literal.

END 

End of expression.

PLUS 

Addition operator.

MINUS 

Subtraction operator.

MULTIPLY 

Multiplication operator.

DIVIDE 

Division operator.

ASSIGN 

Assignment operator.

LHPAREN 

Left parenthesis.

RHPAREN 

Right parenthesis.

COMMA 

Comma delimiter.

NOT 

Logical NOT operator.

LT 

Less than comparison.

GT 

Greater than comparison.

LE 

Less than or equal (<=)

GE 

Greater than or equal (>=)

EQ 

Equal comparison (==)

NE 

Not equal comparison (!=)

AND 

Logical AND (&&)

OR 

Logical OR (||)

ASSIGN_ADD 

Compound assignment (+=)

ASSIGN_SUB 

Compound assignment (-=)

ASSIGN_MUL 

Compound assignment (*=)

ASSIGN_DIV 

Compound assignment (/=)

Definition at line 53 of file parser.h.

Constructor & Destructor Documentation

◆ Parser()

hipo::Parser::Parser ( const std::string &  program)
inline

Construct a parser with the given expression.

Parameters
programThe mathematical expression to parse

Definition at line 100 of file parser.h.

Member Function Documentation

◆ Evaluate() [1/2]

const double hipo::Parser::Evaluate ( )

Evaluate the stored expression using current symbol values.

Returns
The result of the expression

Definition at line 653 of file parser.cpp.

◆ Evaluate() [2/2]

const double hipo::Parser::Evaluate ( const std::string &  program)

Evaluate a new expression using current symbol values.

Parameters
programExpression string to evaluate
Returns
The result of the expression

Definition at line 664 of file parser.cpp.

◆ operator[]()

double& hipo::Parser::operator[] ( const std::string &  key)
inline

Access or create a symbol (variable) by name.

Parameters
keyVariable name
Returns
Reference to the variable's value

Definition at line 119 of file parser.h.

Member Data Documentation

◆ symbols_

std::map<std::string, double> hipo::Parser::symbols_

Symbol table mapping variable names to values. Can be accessed directly.

Definition at line 122 of file parser.h.


The documentation for this class was generated from the following files: