A basic calculator that supports the 4 base operations and parentheses.
It is written as a simple interpreter with hand-written lexer, Pratt parser and Tree Walk interpreter.
No compiler-compiler tools are used, following the spirit of books such as Crafting Interpreters and Writing an Interpreter in Go. The goal is twofold: leave no space for "magic" in the implementation, and learn C++.
Please, note that this is a toy project with educational purpose and not something that will ever go in production anywhere.
It comes with a cmake recipe and a small Google Test suite, having the same goal in mind of familiarizing with such technologies.
Forking this repo to add more features and improve its codebase is strongly encouraged. I am releasing it to share this learning project with anybody that may need it.
git clone the project and in the main folder run
$ cmake -S . -B build && cmake --build build
the executables will be under the build
folder.
Try the following to make practice with C++ and interpreters:
- Improve error messages (currently only partly implemented in the Lexer)
- Use
ans
to refer to the latest expression result - Add built/in functions, for instance trigonometric functions
- Add possibility of user-defined functions
- Add vector or matrix arythmetic
The more functionalities added and the more the resulting language looks like an actual programming language.
$ ./calc "(2 + 3) * 7"
35
This project has to be considered "public domain" and is released with double license MIT and Apache. Reuse it with the license you find more appropriate.