Code Monkey home page Code Monkey logo

arithmeticparsing's Introduction

arithmeticParsing

Arithmetic parsing is a simple python library designed for parsing arithmetic expressions.
It is designed to be easy to use and to maintain, and does not focus on optimization as much as it does ease of use.

This library is designed to use minimal dependancies, and the only non-standard-library module this uses is treelib

Installation

To install, it is as easy as using pip on this git repository
I recommend using python -m pip instead of just pip, as this confirms that you are installing on the right python version

python3.9 -m pip install arithmetic-parsing

You can also install by cloning and then running setup.py

git clone https://github.com/wireboy5/arithmeticParsing
cd arithmeticParsing
python3.9 setup.py install

Usage

arithmetic-parsing was designed to be as easy as possible to use
A basic example:

import arithmetic_parsing

parseString = "(testVar1 + 2 * 6) + (testVar2 + 2 * 6)"

parser = arithmetic_parsing.Parser()
parsed = parser.parse(parseString)

print(parsed)

This will output this:

base
└── +
    ├── +
    │   ├── *
    │   │   ├── 2
    │   │   └── 6
    │   └── testVar1
    └── +
        ├── *
        │   ├── 2
        │   └── 6
        └── testVar2

Also, because this uses treelib, you can get a json result:

print(parsed.as_json())
{
    "base": {
        "children": [
            {
                "+": {
                    "children": [
                        {
                            "+": {
                                "children": [
                                    {
                                        "*": {
                                            "children": [
                                                "2",
                                                "6"
                                            ]
                                        }
                                    },
                                    "testVar1"
                                ]
                            }
                        },
                        {
                            "+": {
                                "children": [
                                    {
                                        "*": {
                                            "children": [
                                                "2",
                                                "6"
                                            ]
                                        }
                                    },
                                    "testVar2"
                                ]
                            }
                        }
                    ]
                }
            }
        ]
    }
}

Another output form is the list output

print(parsed.as_list())
[
    ['dyn', 'base_0', '+', 'testVar2', '12'], 
    ['dyn', 'base_1', '+', 'testVar1', '12'], 
    ['dyn', 'base_2', '+', 'base_1', 'base_0']
]

Lets take a look at the first item:

['dyn', 'base_0', '+', 'testVar2', '12']

And lets break it down

  • Dyn
    • This specifies that it is a dynamic value, and not a constant
  • base_0
    • This is the variable name, If you were to convert this into something like python.
  • +
    • This is the operator
  • testVar2
    • This is the first operand
  • 12
    • This is the second operand

Notice how it has 12 instead of 2 * 6?
That is because it is optimizing the result.
We can disable this optimization by setting optimize to false:

parser = arithmetic_parsing.Parser(
    optimized = False
)

Also notice that the parsed list has the values in a different order than you would expect?
The parser automatically sorts them to be as close as possible to their first reference, from the top down.
This to can be disabled:

parser = arithmetic_parsing.Parser(
    sort = False
)

We can use this to convert the value to assembly:

from arithmetic_parsing.examples import assembly

asm = assembly.listToAssembly(parsed.as_list(),parseString)

asm = "\n".join(asm)

print(asm)

This will output NASM code:

mov rbx, [testVar2]
add rbx, 12
mov rbx, [testVar1]
add rbx, 12
mov rax, rbx
add rax, rbx'

NOTE: Do not use this function for converting to assembly in an actual program.
This function is for demonstration purposes and has not been tested thouroughly

arithmeticparsing's People

Contributors

wireboy5 avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.