A flexible parser and calculator engine developed using pure C++17 and the raw Win32 API. This project demonstrates compiler front-end mechanisms without relying on any external libraries (such as Qt, MFC, C#, etc.).
To eliminate the time-consuming use of complex parentheses (e.g., Shift+8/9), this calculator is designed for parenthesis-free (linear input) entry. Users input expressions in a single line, and the system automatically manages operator logic in the background.
In addition to standard operators (+, -, *, /), a custom p (Percentage) operator has been added to facilitate financial calculations and speed up workflows.
- Formula:
a p b→a * (b / 100) - Example: The expression
1000 + 50 p 10automatically calculates50 p 10 = 5first (due to operator precedence) and then produces the result1000 + 5 = 1005.
Instead of using hardcoded pixel values for the interface, a VerticalLayout engine is implemented; it tracks element positions in memory and arranges them dynamically. This ensures that all elements automatically realign if the window size changes. ---
The project is structured into three main modules in accordance with software engineering principles: main.cpp (Interface & Event Loop), parser.h (Declarations), and parser.cpp (Implementation).
- Lexer (Scanner): Scans the source text character-by-character using
std::wstring_viewwith zero-allocation semantics. It employs an LL(1) / One-Character Lookahead architecture based onpeek()andadvance()methods, requiring no backtracking. - Parser (Interface / State Machine): Validates inputs using the
ParserStateenumeration (e.g.,EXPECT_OPERAND,EXPECT_OPERATOR). It resolves token precedence usingstd::variantand a type-safestd::stackapproach (Shunting-Yard-style State-Machine Parser).
In the current architecture, the eval() function operates like an interpreter, calculating results immediately. However, the front-end is designed to be fully modular. The infrastructure is ready to evolve into a compiler; by integrating a code generator into the eval() function in future stages, these simplified expressions can be directly converted into raw x64 Assembly or Opcode (Machine Code).
- Open any C++17-compatible compiler (Visual Studio MSVC is recommended). 2. Include
main.cpp,parser.cpp, andparser.hin your project. - Compile by selecting the Windows (
/SUBSYSTEM:WINDOWS) subsystem setting.