A simple math expression parser and evaluator written in c++ that showcase an implementation of various design patterns.
To build and run the mathparser executable, use the following commands:
./scripts/build.sh mathparser_exe
./install/bin/mathparser_exeThis project intentionally uses several well-known design patterns to make the parser modular and easy to extend:
- Composite —
IExpressionandFunctionExpressionform a composite structure so expressions can contain nested sub-expressions (children are stored inFunctionExpression::arg_). - Strategy — The
BaseFunctionclass defines an interface for a function's evaluation; concrete functions (AddFunction,MultFunction,SinFunction, etc.) implementevaluateImpl, letting behavior vary independently of the expression tree. - State — The parser uses
ParserStateand concrete states (InitState,FuncState,CoefState,TransState) to manage parsing behavior depending on the current context. - Interpreter —
ParserandFunctionExpression::Evaluate()implement an interpreter-like pattern to evaluate parsed expressions. - Registry / Factory —
FunctionMapregisters function implementations and provides lookup by name or operator; this central registry simplifies function creation and lookup.
The class diagram is available in docs/uml/mathparser_core.puml (PlantUML). You can render it locally (requires PlantUML and Graphviz):
# SVG
plantuml -tsvg docs/uml/mathparser_core.puml
# PNG
plantuml -tpng docs/uml/mathparser_core.pumlSource: docs/uml/mathparser_core.puml. A PNG version is also available at docs/uml/mathparser_core.png.