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
null
sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character.

ꦱꦮ sawa (ꦱꦮ) is an open source programming language, an interpreter to be precise, where you can write python code using javanese character. sawa iku

Rony Lantip 307 Jan 7, 2023
One line Brainfuck interpreter in Python

One line Brainfuck interpreter in Python

null 16 Dec 21, 2022
Recreate the joys of Office Assistant from the comfort of the Python interpreter

Recreate the joys of Office Assistant from the comfort of the Python interpreter.

Louis Sven Goulet 3 May 21, 2022
Ked interpreter built with Lex, Yacc and Python

Ked Ked is the first programming language known to hail from The People's Republic of Cork. It was first discovered and partially described by Adam Ly

Eoin O'Brien 1 Feb 8, 2022
An implementation of an interpreter for the Brainfuck esoteric language in Python

Brainfuck Interpreter in Python An implementation of an interpreter for the Brainfuck esoteric language in Python. ?? The Brainfuck Language Created i

Carlos Santos 0 Feb 1, 2022
Learning with Peter Norvig's lis.py interpreter

Learning with lis.py This repository contains variations of Peter Norvig's lis.py interpreter for a subset of Scheme, described in (How to Write a (Li

Fluent Python 170 Dec 15, 2022
The Official interpreter for the Pix programming language.

The official interpreter for the Pix programming language. Pix Pix is a programming language dedicated to readable syntax and usability Q) Is Pix the

Pix 6 Sep 25, 2022
A subleq VM/interpreter created by me for no reason

What is Dumbleq? Dumbleq is a dumb Subleq VM/interpreter implementation created by me for absolutely no reason at all. What is Subleq? If you haven't

Phu Minh 2 Nov 13, 2022
Compiler Final Project - Lisp Interpreter

Compiler Final Project - Lisp Interpreter

null 2 Jan 23, 2022
emoji-math computes the given python expression and returns either the value or the nearest 5 emojis as measured by cosine similarity.

emoji-math computes the given python expression and returns either the value or the nearest 5 emojis as measured by cosine similarity.

Andrew White 13 Dec 11, 2022