Expression interpreter written in Python

Overview

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

You might also like...
Structural basis for solubility in protein expression systems

Structural basis for solubility in protein expression systems Large-scale protein production for biotechnology and biopharmaceutical applications rely

PyDy, short for Python Dynamics, is a tool kit written in the Python
PyDy, short for Python Dynamics, is a tool kit written in the Python

PyDy, short for Python Dynamics, is a tool kit written in the Python programming language that utilizes an array of scientific programs to enable the study of multibody dynamics. The goal is to have a modular framework and eventually a physics abstraction layer which utilizes a variety of backends that can provide the user with their desired workflow

Simple, high-school-leveled sequence library written in Python / 간단한 고등학교 수준 수열 라이브러리 (Python)
Msgpack serialization/deserialization library for Python, written in Rust using PyO3 and rust-msgpack. Reboot of orjson. msgpack.org[Python]

ormsgpack ormsgpack is a fast msgpack library for Python. It is a fork/reboot of orjson It serializes faster than msgpack-python and deserializes a bi

Synthetik Python Mod - A save editor tool for the game Synthetik written in python

Synthetik_Python_Mod A save editor tool for the game Synthetik written in python

MiniJVM is simple java virtual machine written by python language, it can load class file from file system and run it.

MiniJVM MiniJVM是一款使用python编写的简易JVM,能够从本地加载class文件并且执行绝大多数指令。 支持的功能 1.从本地磁盘加载class并解析 2.支持绝大多数指令集的执行 3.支持虚拟机内存分区以及对象的创建 4.支持方法的调用和参数传递 5.支持静态代码块的初始化 不支

Pygments is a generic syntax highlighter written in Python

Welcome to Pygments This is the source of Pygments. It is a generic syntax highlighter written in Python that supports over 500 languages and text for

Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.

Retrying Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just

A full-featured, hackable tiling window manager written and configured in Python
A full-featured, hackable tiling window manager written and configured in Python

A full-featured, hackable tiling window manager written and configured in Python Features Simple, small and extensible. It's easy to write your own la

Comments
  • Better lexing(and scanning) and parsing

    Better lexing(and scanning) and parsing

    There is currently an issue with the lexing strategy for unary operators. Because i have only partially implemented it, constructs that uses unary operators are note exactly recognised. For example

    > -(1)
    1  # should be -1 
    

    This is problematic because it affects expressions such as to give wrong the answer

    > -1^2
    1  # should be -1
    > -(1^2)
    1  # should be -1
    

    The main reason for this is that unary operators are not actually lexed themselves, when an expression such as -1 is passed, the program checks if the immediately succeeding character is a number and immediately binds the sign to the number after it, a bracket/parentheses is not a number and thus the operator doesn't get lexed independently at all disabling the parser to not recognise have any knowledge of it.

    Also note that + and - are always initially tokenised as Token.BINARY_OPs and I believe this should change.

    see this line

    bug enhancement 
    opened by lestherll 2
  • Support for high precision floating-points

    Support for high precision floating-points

    Currently, the interpreter uses raw Python as the engine for evaluating mathematical expressions. This is good in normal cases but as you may already know, computers aren't so good at dealing with high precision floating-points. It would be good to be able to utilise perhaps some module to deal with this.

    > 9.999999800000001 * 10^15
    9999999800000002.0   # should be 9999999800000001
    
    bug enhancement 
    opened by lestherll 0
  • Create custom error codes

    Create custom error codes

    Currently, there are no custom error codes. Adding one would make the project more concrete and robust.

    Example

    # inside REPL
    > 1.1.1
    SyntaxError: Numbers can only have one decimal point
    
    enhancement 
    opened by lestherll 0
Releases(v0.1.2)
Owner
BSc Comp Sci at Newcastle University
A supercharged version of paperless: scan, index and archive all your physical documents

Paperless-ng Paperless (click me) is an application by Daniel Quinn and contributors that indexes your scanned documents and allows you to easily sear

Jonas Winkler 5.3k Jan 09, 2023
Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses

Reverse the infix string. Note that while reversing the string you must interchange left and right parentheses. Obtain the postfix expression of the infix expression Step 1.Reverse the postfix expres

Sazzad Hossen 1 Jan 04, 2022
Fixed waypoint(pose) navigation for turtlebot simulation.

Turtlebot-NavigationStack-Fixed-Waypoints fixed waypoint(pose) navigation for turtlebot simulation. Task Details Task Permformed using Navigation Stac

Shanmukha Vishnu 1 Apr 08, 2022
Export transactions for an algorand wallet to a CSV file

algorand_txn_csv_exporter - (Algorand transaction CSV exporter) This script will export transactions for an algorand wallet to a CSV file. It is inten

TeneoPython01 5 Jun 19, 2022
Results of Robot Framework 5.0 survey

Robot Framework 5.0 survey results We had a survey asking what features Robot Framework community members would like to see in the forthcoming Robot F

Pekka Klärck 2 Oct 16, 2021
Tomador de ramos UC automatico para Windows, Linux y macOS

auto-ramos v2.0 Tomador de ramos UC automatico para Windows, Linux y macOS Funcion Este script de Python tiene como principal objetivo hacer que la to

Open Source eUC 13 Jun 29, 2022
Python plugin/extra to load data files from an external source (such as AWS S3) to a local directory

Data Loader Plugin - Python Table of Content (ToC) Data Loader Plugin - Python Table of Content (ToC) Overview References Python module Python virtual

Cloud Helpers 2 Jan 10, 2022
VirtualBox Power Driver for MAAS (Metal as a Service)

vboxpower VirtualBox Power Driver for MAAS (Metal as a Service) A way to manage the power of VirtualBox virtual machines via the MAAS webhook driver.

Saeid Bostandoust 131 Dec 17, 2022
This repository contains a lot of short scripting programs implemented both in Python (Flask) and TypeScript (NodeJS).

fast-scripts This repository contains a lot of short scripting programs implemented both in Python (Flask) and TypeScript (NodeJS). In python These wi

Nahum Maurice 3 Dec 10, 2022
This repository provides a set of easy to understand and tested Python samples for using Acronis Cyber Platform API.

Base Acronis Cyber Platform API operations with Python !!! info Copyright © 2019-2021 Acronis International GmbH. This is distributed under MIT licens

Acronis International GmbH 3 Aug 11, 2022
Design-by-contract in Python3 with informative violation messages and inheritance

icontract icontract provides design-by-contract to Python3 with informative violation messages and inheritance. It also gives a base for a flourishing

275 Jan 02, 2023
Set named timers for cooking, watering plants, brewing tea and more.

Timer Set named timers for cooking, watering plants, brewing tea and more. About Use Mycroft when your hands are messy or you need more that the one t

OpenVoiceOS 3 Nov 02, 2022
Moji sends text and fun facts from different APIs wit da use of a notification deamon

Moji sends text and fun facts from different APIs wit da use of a notification deamon. Can be runned via dmenu or rofi.

kshly 2 Jan 12, 2022
A webapp for taking fast notes, designed for business, school, and collaboration with groups.

JOTS Journal of the Session A webapp for taking fast notes, designed for business, school, and collaboration with groups.

Zebadiah S. Taylor 2 Jun 10, 2022
A project to explore and provide useful code for Mango Markets

🥭 Mango Explorer A project to explore and provide useful code for Mango Markets

Blockworks Foundation 160 Dec 19, 2022
A Python tool to check ASS subtitles for common mistakes and errors.

A Python tool to check ASS subtitles for common mistakes and errors.

1 Dec 18, 2021
Blender addon that simplifies access to useful operators and adds missing functionality

Quick Menu is a Blender addon that simplifies common tasks Compatible with Blender 3.x.x Install through Edit - Preferences - Addons - Install... -

passivestar 94 Dec 27, 2022
An almost fully customizable language made in python!

Whython is a project language, the idea of it is that anyone can download and edit the language to make it suitable to what they want.

Julian 47 Nov 05, 2022
Djangoblog - A blogging site where people can make their accout and write blogs and read other author's blogs

This a blogging site where people can make their accout and write blogs and read other author's blogs.

1 Jan 26, 2022
Write complicated anonymous functions other than lambdas in Python.

lambdex allows you to write multi-line anonymous function expression (called a lambdex) in an idiomatic manner.

Xie Jingyi 71 May 19, 2022