Skip to main content

Expression interpreter modeled after a calculator

Project description

Calc Interpreter

Python Package other branches Python Version Python Implementation

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

expr_calc-0.1.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

expr_calc-0.1.2-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file expr_calc-0.1.2.tar.gz.

File metadata

  • Download URL: expr_calc-0.1.2.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.2 Linux/5.8.0-1040-azure

File hashes

Hashes for expr_calc-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2a9770f943e753614555d8930a8328750ec263bad70e65536161a2457e5fd8c0
MD5 d5a146d11dbfd984f84e811221e2179c
BLAKE2b-256 32f5e21be551435751d6881211d1f8a2ff04b054bffc86dd79650a98357d550d

See more details on using hashes here.

File details

Details for the file expr_calc-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: expr_calc-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.8 CPython/3.8.2 Linux/5.8.0-1040-azure

File hashes

Hashes for expr_calc-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e6c28041aff8a3db011daaeef93e583370f089f8e50419f1af027582e456714d
MD5 9222409cc97bd240474bdfd16a911f1c
BLAKE2b-256 b6aadd944123a4caffdc79889778c8677b733a59ee3c546af0325e01ddc0abb4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page