A simple, easy to use, arithmetic parsing tool with minimal dependencies
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file arithmetic-parsing-0.0.1.post3.tar.gz
.
File metadata
- Download URL: arithmetic-parsing-0.0.1.post3.tar.gz
- Upload date:
- Size: 10.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9f087036d0081a8302e2da2d4f17668048277b47e15a37edad7d15b09b57d41 |
|
MD5 | af6f4c48fbbc5e58a8dd050fd02ec807 |
|
BLAKE2b-256 | 799a2dc1b8451ac20b50b8d042d09718e2ff2acca51bc7f16166d1713b30a09b |
File details
Details for the file arithmetic_parsing-0.0.1.post3-py3-none-any.whl
.
File metadata
- Download URL: arithmetic_parsing-0.0.1.post3-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0rc1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b129e8497c0c82ffa1b8fd2e482ef7f05d1340508dc124284d7c5a26519a63ee |
|
MD5 | 3c28223f3302dbb03d45f230e6c8cf55 |
|
BLAKE2b-256 | ff230b8d80b2a3db5f029b1e000128f4c2b1d816a15aa42e43d2f1a1c216328f |