https://github.com/zdimension/simplecalc
Simple formula parser and evaluator
https://github.com/zdimension/simplecalc
Last synced: 11 months ago
JSON representation
Simple formula parser and evaluator
- Host: GitHub
- URL: https://github.com/zdimension/simplecalc
- Owner: zdimension
- License: mit
- Created: 2020-06-28T08:43:15.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-28T08:50:22.000Z (over 5 years ago)
- Last Synced: 2025-02-04T18:52:13.777Z (about 1 year ago)
- Language: Python
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simplecalc
Simple formula parser and evaluator
## Requirements
Requires Python 3.7 (probably)
## Usage
Runs as an interactive REPL.
Displays the stringified AST, the beautified expression and either the result or the detailed error.
```
$ python3 calc.py
> 2+2
parse..BinOpNode(pos=1, op=BinOperator(symbol='+', priority=0, perform=, rtl=False), left=parse..NumberNode(pos=0, val=2), right=parse..NumberNode(pos=2, val=2))
(2 + 2)
4
> 2^2^2
parse..BinOpNode(pos=1, op=BinOperator(symbol='^', priority=2, perform=, rtl=True), left=parse..NumberNode(pos=0, val=2), right=parse..BinOpNode(pos=3, op=BinOperator(symbol='^', priority=2, perform=, rtl=True), left=parse..NumberNode(pos=2, val=2), right=parse..NumberNode(pos=4, val=2)))
(2 ^ (2 ^ 2))
16
> 2+2+2
parse..BinOpNode(pos=3, op=BinOperator(symbol='+', priority=0, perform=, rtl=False), left=parse..BinOpNode(pos=1, op=BinOperator(symbol='+', priority=0, perform=, rtl=False), left=parse..NumberNode(pos=0, val=2), right=parse..NumberNode(pos=2, val=2)), right=parse..NumberNode(pos=4, val=2))
((2 + 2) + 2)
6
> 2+2+
↑
expected token, got EOL at 4
```
## Supported operators / functions
Unary:
- `-` : negation
- `~` : binary complement
Binary:
- `+` : sum
- `-` : difference
- `*` : product
- `/` : quotient
- `^` : exponent (right-to-left associative)
Functions:
- everything from `cmath`
- everything from `math` that isn't in `cmath` (`cos` is `cmath.cos` and not `math.cos`)
Constants:
- everything from `cmath` or `math`
- `i` : imaginary unit