https://github.com/jayu/arithmetic-parser
https://github.com/jayu/arithmetic-parser
Last synced: over 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/jayu/arithmetic-parser
- Owner: jayu
- Created: 2020-02-23T12:15:54.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-10T16:57:36.000Z (about 6 years ago)
- Last Synced: 2025-03-18T05:12:27.295Z (over 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Arithmetic parser
The goal of this project was to train some regular expressions and create parser for arithmetic expressions.
Initially I wanted to create simple programing language, but I wanted to start with something easy.
After I managed to implement this parser, I started looking on how parsers and compilers are build.
It turned out that building programming language parser just with regexps is not a the best idea due to low maintainability and issues with nested recursive expressions. I decided to give up on further development of this parser, and hopefully starting a new project of simple programing language parser.
However I think the work that was done here was quite successful. My parser can:
- parse expressions with subtraction, addition, multiplication, division and parenthesis.
- compile equation to three languages: JavaScript, Python and Rust.
Of course, any of the languages can execute this kind of equation just as it is, but I wanted to check how actually code is interpreted by the machine, without looking on any tutorials at first.
## Example
```sh
node index.js (1+3)/4+7-1*2
```
will output "compiled" source code, and print the result.
```sh
Code
_____
const i7 = 1.0 + 3.0;
const i3 = i7 / 4.0;
const i1 = i3 + 7.0;
const i2 = 1.0 * 2.0;
const i0 = i1 - i2;
console.log(i0)
_____
Result
6
```
## Missing features
- unary operators for negative numbers (be able to parse `-5`)
- power operator (`3^2`, `2^2^2`)