Expression interpreter modeled after a calculator
Project description
Calc Interpreter
An interpreter modeled after a calculator implemented in Python 3. The program currently only supports basic mathematical expressions. The package uses Reverse Polish Notation also known as postfix expression internally to easily represent mathematical expressions. This is powerful as it allows operators to be organised in such a way that precedence is absolute meaning an operator that is encountered first will always be executed first.
However, it is obvious that postfix is not normal or rather not usually taught in schools and thus infix expression is still the way for the user to write expressions. The interpreter uses a modified Shunting-Yard algorithm to produce Abstract Syntax Trees. Evaluation of ASTs is trivial, and they are flexible making it easy to extend the grammar and functionality later on.
Installation
The package is available in PyPI and can be installed via pip.
pip install expr-calc
#or
python3 -m pip install expr-calc
Usage
The best way to run the program currently is to execute the REPL and can be done in a python file or through your terminal.
Assuming your present working directory is inside the cloned repo, you can run the following command without the comment.
# inside /clone_path/expr_calc/
python -m expr_calc
The test suite can also be ran with pytest
when inside the cloned repo
pytest # or python -m pytest
Example
Once inside the REPL, you can start evaluating expressions. Currently, only operators listed in Features are supported.
calc> 1 + 1
2
calc> 345--500
845
calc> -2
-2
calc> 123 ^ 4
228886641
calc> 32 / 1.5
21.33333333333333333333333333
calc> 123 * 456
56088
calc> 34 % 5
4
calc> 4 ^ 1/2
0.5
calc> 4 ^ (1/2)
2.0
Features
- Infix expressions
- Basic operators such as
+, -, *, /, %, ^
- Tokens created from an expression can also be fetched to be manipulated if one wanted to do so
- Expressions are transformed into m-ary Tree objects connected to each other
Features I want to add later
- variable support
- custom functions
- more mathematical functions such as
sin
,cos
,tan
, etc - and possibly a simple symbolic computation support
Resources
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for expr_calc-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6c28041aff8a3db011daaeef93e583370f089f8e50419f1af027582e456714d |
|
MD5 | 9222409cc97bd240474bdfd16a911f1c |
|
BLAKE2b-256 | b6aadd944123a4caffdc79889778c8677b733a59ee3c546af0325e01ddc0abb4 |