Skip to content

A simple python library designed for parsing arithmetic expressions

License

Notifications You must be signed in to change notification settings

wireboy5/arithmeticParsing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A simple python library designed for parsing arithmetic expressions

Topics

Resources

License

Stars

Watchers

Forks

Languages