How mparlak/flee works
Project overview and purpose
• Flee is a library designed to parse, compile, and evaluate mathematical and logical expressions at runtime. It translates expressions into IL (Intermediate Language) using a custom compiler and code generation technique, ensuring high-performance evaluation.
Main components
a. Parsing Subsystem:
• Located in the src/Flee/Parsing directory.
• Responsible for tokenizing strings, building syntactic/grammatical representations, and processing production patterns.
• Key elements include classes like ExpressionTokenizer, ExpressionParser, RecursiveDescentParser, Token*, and related exception handling components.
b. Expression Elements:
• Found in src/Flee/ExpressionElements and its subdirectories.
• Contains various classes representing parts of an expression, such as arithmetic operations, literals (integral, real, string, boolean, etc.), binary/unary expressions, member access, casts, comparisons, and logical/bitwise operations.
• This layer forms the abstract syntax tree (AST) or expression tree used throughout the system.
c. Calculation Engine:
• Components in src/Flee/CalcEngine (for example, CalculationEngine, SimpleCalcEngine, BatchLoader) show how expression evaluations can be batched or executed in a controlled environment.
• This layer uses the expression trees built earlier and executes them, possibly taking advantage of caching and dynamic IL code generation.
d. Internal/Utility Components:
• Modules under src/Flee/InternalTypes provide additional support for IL generation (e.g., FleeILGenerator), dependency management, implicit conversions, and miscellaneous utility functions.
• They bridge between the high-level expression representation and the low-level code generation aspects.
e. Public API:
• The src/Flee/PublicTypes directory exposes the library’s surface. These types (like ExpressionContext, ExpressionImports, ExpressionOptions, VariableCollection) allow consumers to interact with the Flee engine.
• This layer is critical for external integration and is what client applications will use.
f. Resources:
• Located in src/Flee/Resources, these manage error messages, documentation comments, and resource keys.
• While not core to the evaluation engine’s function, they are essential for developer experience and debugging.
g. Testing Infrastructure:
• Found in the test directory (Flee.Test), these include tests for the CalcEngine, Expression tests, and extension methods.
• They ensure that any component interaction (e.g., between parsing and evaluation) functions as expected.
Relationships and interactions
• The Parsing Subsystem processes the raw expression strings to produce token/AST output.
• The Expression Elements component then uses this AST to create a structured object model representing the expression.
• The Calculation Engine (via public interfaces) takes these expression trees and compiles them into IL. Internal utilities ensure that IL code generation and dependency management are handled efficiently.
• The Public API wraps these inner operations, exposing methods for creating expression contexts, importing types, and running the evaluations.
• Data flow flows from input (raw strings) through parsing, expression tree construction, IL generation, and then evaluation.
• Each module is decoupled via clear interfaces, allowing unit testing and potential future substitutions (e.g., different parsers or expression element implementations).
Architectural patterns and design principles
• Layered Architecture: The project is structured in layers – from parsing, abstract syntax tree (AST) generation, intermediate IL code generation, to evaluation.
• Separation of Concerns: Distinct modules take responsibility for parsing (grammar handling), expression representation (element trees), and evaluation (calculation engines).
• Extensibility/Plugability: Public interfaces allow clients to interact and extend the evaluation process (e.g., custom operators, extension methods).