Skip to content

Compute mathematical expressions via abstract syntax tree. Built with Java and ANTLR4.

License

Notifications You must be signed in to change notification settings

nrmancuso/parseva-math

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

alt text

parseva-math


Compute and analyze expressions via abstract syntax tree.



Quality Gate Status

Introduction

parseva-math is a symbolic expression parsing tool, which can analyze and evaluate mathematical expressions. In a nutshell, parseva-math accepts a mathematical expression as a string, and first builds a heterogeneous abstract syntax tree of the expression. Then, parseva-math walks the tree to evaluate the expression. Parseva-math also supports building a homogeneous abstract syntax tree and printing the result; this way expressions can be analyzed for structure and correctness.

Getting Started

You will need to have a version of the JDK >= 17 in order to build parseva-math.

I plan to make a compiled jar once parseva-math reaches version 1.0. Until then, you must clone this repository and build it yourself:
git clone https://github.com/nmancus1/parseva-math.git && cd parseva-math

Then, to build the jar:
mvn clean compile assembly:single

Finally, you can run parseva-math:
java -jar --enable-preview target/parseva-math-<version>-SNAPSHOT-jar-with-dependencies.jar

Note that --enable-preview is required for now, since parseva-math uses JDK15 preview features.

Usage

parseva-math has two different modes of operation; interactive and command line. Mathematical functions, such as logarithmic and trigonometric functions are currently supplied by java.lang.Math.

Interactive Mode

To run parseva-math in the interactive mode, use -i or --interact:

java -jar --enable-preview target/parseva-math-<version>-SNAPSHOT-jar-with-dependencies.jar -i

This will drop you into the parseva-math terminal, where you can evaluate expressions:

> 2 + 2
= 4.000000

> sin(1)
= 0.841471

> sqrt(sin(0) + 20 + sqrt(50 - pow(5, 2)))
= 5.000000

To exit, simply press enter at an empty prompt.

Evaluate via Command Line

To evaluate one expression via command line, use -e "<expression>" or --evaluate "<expression>":

 ➜  java -jar --enable-preview target/parseva-math-<version>-SNAPSHOT-jar-with-dependencies.jar -e "atan(0.5)"
 = 0.463648

parseva-math also supports the constants pi and e.

Print Expression Tree

Using the -t <expression> or --tree <expression> allows users to tell parseva-math to construct a homogeneous abstract syntax tree using MathAstNodes, then walk the tree and print the results.

Example:

➜  java -jar --enable-preview target/parseva-math-0.1-SNAPSHOT-jar-with-dependencies.jar -t "sin(sqrt(2.2) + pow(0.5,0.5))"
'- FUNCTION -> sin
   |- LPAREN -> (
   |- OP_ADD -> +
   |  |- FUNCTION -> sqrt
   |  |  |- LPAREN -> (
   |  |  |- NUM -> 2.2
   |  |  '- RPAREN -> )
   |  '- FUNCTION -> pow
   |     |- LPAREN -> (
   |     |- NUM -> 0.5
   |     |- COMMA -> ,
   |     |- NUM -> 0.5
   |     '- RPAREN -> )
   '- RPAREN -> )

Help

Usage help is available using -h, --help, or simply providing no arguments to parseva-math.

Contributing

If you'd like to contribute, start by searching through the issues and pull requests to see whether someone else has raised a similar idea or question.

If you don't see your idea listed, and you think it fits into the goals of this guide, start by opening an issue first. That way, other people can weigh in on the discussion before you do any work.

Contribution Workflow

  1. Fork this repo on GitHub
  2. Clone the project to your own machine from YOUR fork on github. git clone "url from your fork" then change into the new directory.
  3. Set Upstream do git remote add upstream https://github.com/nmancus1/parseva-math.git
  4. Make new branch for YOUR code submission; do git checkout -b <whatever you want to name this branch>
  5. Commit changes to your new branch, with descriptive message
  6. Run mvn clean verify ; make sure this passes before pushing your changes.
  7. Push your work back up to your fork git push origin <your branch name>
  8. On Github Submit a Pull request so that your changes can be reviewed

NOTE: Be sure to merge the latest from "upstream" before making a pull request! All conflicts should be handled locally, then rebased and pushed to github.

To sync your main branch before starting to code, do:

  1. git checkout main
  2. git fetch --all
  3. git pull upstream main
  4. git push - this syncs your fork on github.
  5. Start on #4 above to begin a new contribution.

Here is a great cheatsheet for git/github.
Here is another article that describes this exact workflow, in more detail.

Ground rules & expectations

Before we get started, here are a few things we expect from you (and that you should expect from others):

  • Be kind and thoughtful in your conversations around this project. We all come from different backgrounds and projects, which means we likely have different perspectives on "how open source is done." Try to listen to others rather than convince them that your way is correct.
  • If you open a pull request, please ensure that your contribution passes all tests. If there are test failures, you will need to address them before we can merge your contribution.

Phases

To generate Jacoco report, do: mvn clean verify

The report will be generated in target/site/jacoco.

License

Distributed under the Unlicense.

Built With

Acknowledgements

About

Compute mathematical expressions via abstract syntax tree. Built with Java and ANTLR4.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published